Tips for golfing in TikZ

13

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.

Post Rock Garf Hunter

Posted 2017-02-07T19:30:29.610

Reputation: 55 382

Does anyone golf in Tikz? – Pavel – 2017-02-07T20:43:51.790

@Pavel I do.

– Post Rock Garf Hunter – 2017-02-07T20:44:33.267

Shouldn't this be a question for meta ? – Sergiy Kolodyazhnyy – 2017-02-07T22:21:12.867

@WheatWizard It seems like a question about "how to approach answering a question" , rather than a programming puzzle itself. I see there's a tips tag, so I assume it's on-topic on the main site. Still learning the ropes on this site, so questions about how to approach answering/reducing answers seems more suitable for meta site rather than main one. – Sergiy Kolodyazhnyy – 2017-02-07T22:28:32.753

4@Serg There are a good deal of [tag:tips] questions of similar content on the main site. These are generally considered to be on-topic. – Post Rock Garf Hunter – 2017-02-07T22:49:48.167

2

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 ' – 2017-02-11T15:02:12.737

Do you think this question is worth generalizing/editing to "Tips for golfing in TeX/LaTeX", or should I create a new question? Things like \def are not specific to TikZ, and I have some more answers I'd like to add (but none are TikZ-related). – ShreevatsaR – 2017-05-31T17:49:42.057

1@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. – Post Rock Garf Hunter – 2017-05-31T17:51:34.697

@WheatWizard Ok makes sense; I've created a new question here.

– ShreevatsaR – 2017-06-01T05:46:06.033

Answers

5

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);

Post Rock Garf Hunter

Posted 2017-02-07T19:30:29.610

Reputation: 55 382

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 – 2020-02-18T21:38:51.303

5

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.

Laikoni

Posted 2017-02-07T19:30:29.610

Reputation: 23 676

Nice find! Every byte we can shave off of the boiler plate is great! – Post Rock Garf Hunter – 2017-04-18T19:22:11.970

4

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

0 '

Posted 2017-02-07T19:30:29.610

Reputation: 3 439