[Tex/LaTex] When does TikZ require semicolons

tikz-pgf

More fully than the title: When does TikZ either require or forbid a semicolon at the end of a command?

Learning TikZ I naturally go on line and copy instructions for drawings like the ones I want to make. A lot of these examples have a semicolon after each command, which seems to be what the TikZ manual does.

But many other examples, with many command in them, use no semicolons, and still work fine for me. I tried adding a semicolon at the end of each command in one of these it still worked. But can adding a semicolon at the end of a command sometimes cause trouble?

What is the general rule?

As the comment requests here is a TikZ drawing that works for me, with no semicolons:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{tkz-euclide}
\usetikzlibrary{calc,through,intersections}
\usetkzobj{all}

\begin{document}
\begin{tikzpicture}[scale=1.5]
\tkzDefPoint(2,2){A}
\tkzDefPoint(5,-2){B}
\tkzDefPoint(1,-2){C}
\tkzDefCircle[in](A,B,C)
\tkzGetPoint{I} \tkzGetLength{rIN}
\tkzDefCircle[circum](A,B,C)
\tkzGetPoint{K} \tkzGetLength{rCI}
\tkzDrawPoints(A,B,C,I,K)
\tkzDrawCircle[R,blue](I,\rIN pt)
\tkzDrawCircle[R,red](K,\rCI pt)
\tkzLabelPoints[below](B,C)
\tkzLabelPoints[above left](A,I,K)
\tkzDrawPolygon(A,B,C)
\end{tikzpicture}
\end{document} 

Best Answer

The simple answer to your question is "always". Every tikz command is delimited by a semicolon. However, as you have shown in your sample code, it's possible to construct code which goes inside a tikzpicture environment which does not require a semicolon.

The reason for this is that all of the commands you have shown are macros which have been defined with the semicolons already included in the macro definition. This was a design choice by the author of the tkz-euclide package. I won't comment on whether this was a good design or not. It clearly simplifies the interface but at the expense of making the commands behave differently from regular tikz commands.

For example, a simplified version of the internal command that the \tkzDrawCircle command uses is the following: (I've simplified the actual code to make the example clearer.)

\def\tkz@DrawCircle[#1](#2){
\begingroup
... set some keys for DrawCircle/.cd using #2
\draw[/DrawCircle/.cd,line style,#1]%
   (tkzPointResult) circle (\tkzLengthResult pt);%      
\endgroup
}

So the actual tikz command \draw is delimited by a semicolon inside the definition.

As cfr notes in her comment, it's also the case that the low level pgf commands that are the underlying layer of code for TikZ also don't require semicolons. But as a regular end user of TikZ you are unlikely to use or see these commands, since they would normally only be used within macro definitions or \tikzset commands.


One benefit of the semicolon syntax is that since there is a semicolon to terminate the current command you can introduce more flexibility to the order of arguments etc. and TikZ can parse it as well as it can. This is not possible with a fixed structure e.g., if you happen to forget to remove a space in

\tkzDefPoint (1,-2) {C}

it would lead to an error saying that the macro couldn't function because it couldn't find its argument although it's only one more character away.

However, consider below both valid node specifications:

\node[draw] (n) at (2,1)           {a} ;
\node       at (2,1) [draw] (n)    {a} ;

There is a limit to this flexibility (notice the node content is always given at the end etc.) but as long as we behave, this is much more flexible and without the semicolon would have required a very strict order and syntax as in PGF level or the tkz-euclide examples you have given.

Related Question