# Line width keywords seem to cause a TikZ error

I am attempting a simple format, that works well until I use a line width keyword in the \draw command below. The keyword could be thick or thin or line width=2pt. Running the code with thick gives this:

and TikZ says, for example: No shape named S-3 is known.

Commenting-out thick produces the expected graphic, sort of:

Yes, I know I could have used |- for the drawing, but for simplicity and to maximize M of MWE I chose something more direct.

\documentclass{article}

\usepackage[]{geometry}
\usepackage{xparse}
\usepackage{tikz}

\usetikzlibrary{positioning}

%% |=====8><----| %%

\newcounter{stepnum}
\newlength{\myxincr}
\newlength{\myyincr}

%% |=====8><-----| %%

\NewDocumentEnvironment{test}{m}{%
\begin{tikzpicture}
\coordinate (A) at (0,0);
\setlength{\myxincr}{\dimexpr0.5in/#1\relax}
\setlength{\myyincr}{\dimexpr-5in/#1\relax}
\pgfmathsetmacro{\incr}{1/#1}
\pgfmathparse{#1-1}
\draw[
%% Uncomment the next line and this fails -- any line width keyword causes failure
% thick, %% <<<---
red %% color is ok, seemingly
] (A) foreach \ss in {1,...,\pgfmathresult}
{ -- node[fill=white,name=S-\ss]{\ss} ++(0,\myyincr) -- ++(\myxincr,0)}
--node[fill=white,name=S-#1]{#1} ++(0,\myyincr) -- ++(\myxincr,0);
}{%
\end{tikzpicture}
}

\NewDocumentCommand{\foo}{+m}{%
\stepcounter{stepnum}
\node[right=6pt of S-\thestepnum] {%
#1
};%
}

\parindent=0pt

\begin{document}

\begin{test}{4}
\foo{First}
\foo{Second}
\foo{Third}
\foo{Fourth}
\end{test}

\end{document}

• My guess is that thick does math calculations and overwrites \pgfmathresult which should be saved somewhere else right after having done the calculation... Jan 18 at 13:17
• Bravo! Make it an answer and I'll accept. At one point I wondered if I was wise in using \pgfmathparse...\pgfmathresult in this way. Thank you. Jan 18 at 13:30

The \pgfmathresult macro is temporarily set to the result of a calculation and cannot be trusted to keep its value after calling other macros.

For example:

\pgfmathparse{1-1}
\mymacro
\pgfmathresult


here \pgfmathresult will be 0 only if \mymacro does not perform calculations.

For your MWE, you can fix the issue by saving the result with a \let:

\documentclass{article}

\usepackage[]{geometry}
\usepackage{xparse}
\usepackage{tikz}

\usetikzlibrary{positioning}

%% |=====8><----| %%

\newcounter{stepnum}
\newlength{\myxincr}
\newlength{\myyincr}

%% |=====8><-----| %%

\NewDocumentEnvironment{test}{m}{%
\begin{tikzpicture}
\coordinate (A) at (0,0);
\setlength{\myxincr}{\dimexpr0.5in/#1\relax}
\setlength{\myyincr}{\dimexpr-5in/#1\relax}
\pgfmathsetmacro{\incr}{1/#1}
\pgfmathparse{#1-1}
\let\RES\pgfmathresult % Save result in \RES
\draw[
%% Now this is fine
thick, %% <<<---
red %% color is ok, seemingly
] (A) foreach \ss in {1,...,\RES}
{ -- node[fill=white,name=S-\ss]{\ss} ++(0,\myyincr) -- ++(\myxincr,0)}
--node[fill=white,name=S-#1]{#1} ++(0,\myyincr) -- ++(\myxincr,0);
}{%
\end{tikzpicture}
}

\NewDocumentCommand{\foo}{+m}{%
\stepcounter{stepnum}
\node[right=6pt of S-\thestepnum] {%
#1
};%
}

\parindent=0pt

\begin{document}

\begin{test}{4}
\foo{First}
\foo{Second}
\foo{Third}
\foo{Fourth}
\end{test}

\end{document}


An equivalent way of achieving the \pgfmathparse/\let combo is using \pgfmathsetmacro{\RES}{expression}.

• Nice answer, I'll delete mine! Jan 18 at 13:39