TikZ diagram - Prompt 大师 | JR Academy

Drawing TikZ diagram

TL;DR(中文)

Background

This prompt tests an LLM's code generation capabilities by prompting it to draw an object in TikZ. The model is expected to generate LaTeX code that can then be compiled by the user.

How to Apply(中文)

把这个任务当成 “spec → code → compile → feedback → refine” 的循环:

  1. 定义对象的关键部件(body/head/legs/horn…)与大致几何关系
  2. 让模型输出可编译的 TikZ 代码(最好包含 \\begin{tikzpicture}
  3. 编译后给出具体反馈(坐标、比例、缺少元素)

How to Iterate(中文)

  1. 限制坐标范围(例如 x/y 在 [-5,5]),避免图形跑飞
  2. 要求分层输出:先画轮廓,再加细节
  3. 提供失败反馈:贴出编译报错或截图描述(但不要放进 code block 里)
  4. 让模型输出 “parameters” 方便调整(例如 scale、line width)

Self-check rubric(中文)

Practice(中文)

练习:把对象换成你工作里常见的图:

要求:每个 node 有 label,布局不重叠,并输出可编译代码。

Prompt

Draw a unicorn in TikZ

Code / API

OpenAI (Python)

from openai import OpenAI

client = OpenAI()

response = client.chat.completions.create(
    model="gpt-4",
    messages=[
        {
            "role": "user",
            "content": "Draw a unicorn in TikZ",
        }
    ],
    temperature=1,
    max_tokens=1000,
    top_p=1,
    frequency_penalty=0,
    presence_penalty=0,
)

Fireworks (Python)

import fireworks.client

fireworks.client.api_key = "<FIREWORKS_API_KEY>"

completion = fireworks.client.ChatCompletion.create(
    model="accounts/fireworks/models/mixtral-8x7b-instruct",
    messages=[
        {
            "role": "user",
            "content": "Draw a unicorn in TikZ",
        }
    ],
    stop=["<|im_start|>", "<|im_end|>", "<|endoftext|>"],
    stream=True,
    n=1,
    top_p=1,
    top_k=40,
    presence_penalty=0,
    frequency_penalty=0,
    prompt_truncate_len=1024,
    context_length_exceeded_behavior="truncate",
    temperature=0.9,
    max_tokens=4000,
)

Reference