0

The following code does not compile with "pdflatex file.tex", and I am not sure how to fix it. The 9th line is the problem. Specifically \MV.

\documentclass[tikz]{standalone}
\usepackage{tikz}
\newcommand\MV{\foreach\item in {2}{3}}
\newcommand\YV{2}
\begin{document}
\begin{tikzpicture}
\draw (1,0) node[below=3pt] {\MV}; % works
\draw (0,0) -- (2,\YV);            % works
\draw (0,0) -- (2,\MV);            % fails
\end{tikzpicture}
\end{document}

I get this error:

! Undefined control sequence.
\foreach ...reach \let \pgffor@assign@before@code 
                                                  =\pgfutil@empty \let \pgff...

1 Answers1

1

For what I understand, this is not due to expandability, but to a misuse of \foreach.

Just try to replace your \MV by its definition: it makes nonsense in line 9. It works flawless in line 7 since the command is for the node content, not the position.

I slightly tweak your example

\documentclass[tikz]{standalone}
\usepackage{tikz}
\newcommand\MV{\foreach\item in {2,...,4}{\item+3}}
\newcommand\YV{2}
\begin{document}
  \begin{tikzpicture}
    \draw (1,0) node[below=3pt] {\MV}; % works
    \draw (0,0) -- (2,\YV);            % works
%    \draw (0,0) -- (2,\MV);           % fails
%    \draw (0,0) -- (2,\foreach\item in {2,...,4}{\item+3});  % fails
    \foreach \item in {2,...,4}{
      \draw (0,0) -- (2,\item) node[below=3pt] {\item};       % works
    }
  \end{tikzpicture}
\end{document}

enter image description here

NBur
  • 4,326
  • 10
  • 27