3

My minimal example looks like this:

\documentclass{article}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\draw (0,0) rectangle
    %\ifnum 0=1 node{My Label} \fi  % Works correctly when the value = 0
    \ifnum 1=1 node{My Label} \fi   % Fails to draw
    %node{My label}                 % What I expect when the value passed in = 1
    (4,4);
\end{tikzpicture}
\end{document}

This previous thread suggests it should work: Conditional within TikZ path?

I notice this similar question, which differs in that the question there wants to make entire parts conditional, whereas I'm looking to make only a part of a \draw line conditional: Numerical conditional within tikz keys?

The reported error looks like this:

! Package tikz Error: Cannot parse this coordinate.

See the tikz package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                              
l.10 \ifnum 1=1 n
                 ode{My Label} \fi  % Fails to draw
This error message was generated by an \errmessage
command, so I can't give any explicit help.
Pretend that you're Hercule Poirot: Examine all clues,
and deduce the truth by order and method.


! Package tikz Error: Giving up on this path. Did you forget a semicolon?.

See the tikz package documentation for explanation.
Type  H <return>  for immediate help.

I'm using MiKTeX-pdfTeX 2.9.7338 (1.40.21) (MiKTeX 2.9.7400 64-bit) on Windows 10, inside TeXStudio 2.12.22, if that is relevant.

The command to start the build is pdflatex.exe -synctex=1 -interaction=nonstopmode "tikz_error".tex

1
  • it doesn't work, your \ifnum interrupts the parsing process. You can use the \edef method in the linked question. Nov 2, 2020 at 11:39

2 Answers 2

3

Generally you can use \ifnum etc. in paths. There are, however, cases in which TikZ expects some explicit character, or so. In this case you can just add the \ifnum stuff to the node contents to make TikZ happy.

\documentclass{article}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\draw (0,0) rectangle
    %node{\ifnum0=1\relax My Label\fi}  % Works correctly when the value = 0
    node{\ifnum1=1\relax My Label\fi}   % Works correctly
    (4,4);
\end{tikzpicture}
\end{document}

enter image description here

2
  • Beautiful! Out of interest, what does \relax do?
    – Arkady
    Nov 2, 2020 at 17:11
  • 1
    @Arkady "Nothing". To be more precise, in this context it terminates the \ifnum comparison. While here it is not necessary (except perhaps to suppress spurious spaces) you can use it in constructions like \ifnum1=1\relax0 ... to make sure the the 0 does not get appended to 1 and the comparison is 1=10, which is false.)
    – user227987
    Nov 2, 2020 at 17:19
3

You can expand the \ifnum prior to executing the \draw, to avoid the parsing issue:

\documentclass{article}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\def\tmp{\draw (0,0) rectangle}
  \expandafter\tmp
    \ifnum 1=1 node{My Label} \fi   % Draws label
    (4,4);
  \expandafter\tmp
    \ifnum 1=2 node{My Alt Label} \fi   % Omits label
    (-4,-4);
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .