14
\$\begingroup\$

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.

\$\endgroup\$
9
  • \$\begingroup\$ Does anyone golf in Tikz? \$\endgroup\$
    – Pavel
    Feb 7, 2017 at 20:43
  • \$\begingroup\$ @Pavel I do. \$\endgroup\$
    – Wheat Wizard
    Feb 7, 2017 at 20:44
  • 4
    \$\begingroup\$ @Serg There are a good deal of tips questions of similar content on the main site. These are generally considered to be on-topic. \$\endgroup\$
    – Wheat Wizard
    Feb 7, 2017 at 22:49
  • 2
    \$\begingroup\$ 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 \$\endgroup\$
    – 0 '
    Feb 11, 2017 at 15:02
  • 1
    \$\begingroup\$ @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. \$\endgroup\$
    – Wheat Wizard
    May 31, 2017 at 17:51

4 Answers 4

5
\$\begingroup\$

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);
\$\endgroup\$
1
  • \$\begingroup\$ 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"... \$\endgroup\$ Feb 18, 2020 at 21:38
4
\$\begingroup\$

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}

Example

Credit to WheatWizard for figuring the multiline use of \tikz

\$\endgroup\$
4
\$\begingroup\$

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.

Thus, instead of

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

one can write

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

to save 5 bytes.

\$\endgroup\$
1
  • \$\begingroup\$ Nice find! Every byte we can shave off of the boiler plate is great! \$\endgroup\$
    – Wheat Wizard
    Apr 18, 2017 at 19:22
1
\$\begingroup\$

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:

enter image description here

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.