TikZ diagram
generate TikZ code prompt example
#TL;DR(中文)
- 这是一个 code generation + “可视化结果验证”的任务:模型生成 (LaTeX)代码,你在本地编译看图是否符合预期。code
TikZ - 关键技巧是 iterative prompting:先要一个粗版本,再用具体 feedback 修正(比例、位置、细节)。
- 落地建议:把画面拆成 primitives(circle/line/rectangle)、约束坐标系与比例,并要求输出可编译的最小示例。
#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” 的循环:
- 定义对象的关键部件(body/head/legs/horn…)与大致几何关系
- 让模型输出可编译的 TikZ 代码(最好包含 )code
\\begin{tikzpicture} - 编译后给出具体反馈(坐标、比例、缺少元素)
#How to Iterate(中文)
- 限制坐标范围(例如 x/y 在 [-5,5]),避免图形跑飞
- 要求分层输出:先画轮廓,再加细节
- 提供失败反馈:贴出编译报错或截图描述(但不要放进 code block 里)
- 让模型输出 “parameters” 方便调整(例如 scale、line width)
#Self-check rubric(中文)
- 代码是否可编译(LaTeX/TikZ syntax correct)?
- 是否符合 prompt 的关键要求(unicorn 的辨识度)?
- 是否易于迭代(结构清晰、参数可调)?
#Practice(中文)
练习:把对象换成你工作里常见的图:
- flowchart
- system architecture diagram
- mind map
要求:每个 node 有 label,布局不重叠,并输出可编译代码。
#Prompt
textDraw a unicorn in TikZ
#Code / API
#OpenAI (Python)
pythonfrom 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)
pythonimport 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, )