logo
P
Prompt Master

Prompt 大师

掌握和 AI 对话的艺术

TikZ Diagram

Generate TikZ code prompt example

TL;DR

  • This is a code generation + "visual result verification" task: the model generates TikZ (LaTeX) code, and you compile it locally to see if the image matches expectations.
  • The key technique is iterative prompting: get a rough version first, then refine with specific feedback (proportions, positions, details).
  • Production tip: break the visual into primitives (circle/line/rectangle), constrain the coordinate system and scale, and require output to be a minimal compilable example.

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

Treat this task as a "spec → code → compile → feedback → refine" loop:

  1. Define the object's key parts (body/head/legs/horn...) and rough geometric relationships
  2. Have the model output compilable TikZ code (ideally including \begin{tikzpicture})
  3. After compiling, give specific feedback (coordinates, proportions, missing elements)

How to Iterate

  1. Constrain the coordinate range (e.g., x/y within [-5,5]) to prevent the figure from flying off
  2. Require layered output: draw the outline first, then add details
  3. Provide failure feedback: paste the compile error or describe the screenshot (but don't put it in a code block)
  4. Have the model output "parameters" for easy tweaking (e.g., scale, line width)

Self-check Rubric

  • Is the code compilable (LaTeX/TikZ syntax correct)?
  • Does it meet the prompt's key requirements (unicorn recognizability)?
  • Is it easy to iterate on (clear structure, adjustable parameters)?

Practice

Exercise: replace the object with diagrams you commonly need at work:

  • flowchart
  • system architecture diagram
  • mind map

Requirements: each node has a label, layout doesn't overlap, and output is compilable code.

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