Tips for golfing in TikZ

TikZ is a Latex package used for drawing precise images in Latex documents. It has a massive specification and a boatload of features that may be useful for style questions.

What tips do people have for golfing in TikZ? As always, tips should be specific to to TikZ (e.g. "Remove comments" is not an answer), and stick to a single tip per answer.

• Does anyone golf in Tikz? – Pavel Feb 7 '17 at 20:43
• @Pavel I do. – Wheat Wizard Feb 7 '17 at 20:44
• @Serg There are a good deal of tips questions of similar content on the main site. These are generally considered to be on-topic. – Wheat Wizard Feb 7 '17 at 22:49
• For those interested, there is a tikz/pgf manual (its where I found the information for my answers). Version 3.0.1a, Version 2.10 – 0 ' Feb 11 '17 at 15:02
• @ShreevatsaR I think perhaps a new question should be made for TeX/LaTeX, all of the tips here are currently specific to Tikz, and I can see some benefit in keeping them separate. – Wheat Wizard May 31 '17 at 17:51

Use \def

\def is an incredibly powerful tool when it comes to golfing. \def allows you to assign something to a variable.

This can be used simply with the to save a number you may use a bunch of times for instance

\def\x{1456}


Will define 1456 as \x for future use much like saving a variable might in a programming language.

However \def is much more powerful than that, because \def doesn't define a variable it defines a snippet of code to be substituted into the program whenever it is called.

For example say you want to draw some rectangles using \draw you might write the following code:

\draw(0,0)rectangle(3,4)rectangle(8,0);\draw(2,2)rectangle(3,3);


Using \def this could be written as:

\def\x{)rectangle(}\draw(0,0\x3,4\x8,0);\draw(2,2\x3,3);

• This is brilliant in golfing point of view but also very useful to more deeply understand these important commands...you know when they ask you "what is the difference between \def and \newcommand"... – MattAllegro Feb 18 '20 at 21:38

Use \tikz instead of the tikzpicture environment

Instead of creating a tikzpicture environment (36 bytes) you can use the \tikz command (7 bytes)

Global options can be set in square brackets in using the tikz command as such \tikz[options...]{...}. If the tikz code is one line long the curly braces can be omitted saving an additional two bytes.

Example:

Both of the following programs output the image at the bottom

\documentclass{standalone}\input tikz\begin{document}\tikz{\draw[thick,rounded corners=8pt](0,0)--(0,2)--(1,3.25)--(2,2)--(2,0)--(0,2)--(2,2)--(0,0)--(2,0);\draw(-1.5,0)--(0,1.5);}\end{document}

\documentclass{standalone}\input tikz\begin{document}\begin{tikzpicture}\draw[thick,rounded corners=8pt](0,0)--(0,2)--(1,3.25)--(2,2)--(2,0)--(0,2)--(2,2)--(0,0)--(2,0);\draw(-1.5,0)--(0,1.5);\end{tikzpicture}\end{document}


Credit to WheatWizard for figuring the multiline use of \tikz

Use \documentclass[tikz]{standalone}

By chance I found the following in the manual of the standalone package:

For pictures drawn with TikZ a dedicated tikz option is provided which loads the tikz package and also configures the tikzpicture environment to create a single cropped page.

\documentclass{standalone}\input tikz\begin{document} ...


one can write

\documentclass[tikz]{standalone}\begin{document} ...


to save 5 bytes.

• Nice find! Every byte we can shave off of the boiler plate is great! – Wheat Wizard Apr 18 '17 at 19:22

You can sometimes refrain from using co-ordinate (0,0) completely.
That's an easy one.
Here's a more intricate one:
(,) is the same as (1,1).
Found out that one rather by accident.
Here's a minimal example:

\documentclass[tikz,border=9]{standalone}\def~{document}\begin~\tikz{\draw grid(,);}\end~


Output: