3

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: incorrect

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

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

correct

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}
2
  • 3
    My guess is that thick does math calculations and overwrites \pgfmathresult which should be saved somewhere else right after having done the calculation...
    – Bordaigorl
    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.
    – sgmoye
    Jan 18 at 13:30

1 Answer 1

5

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}.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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