Has the \for loop command in Latex (pgffor package) been deprecated?

Viewed 926

I'm not able to get the \for command to work in TexWorks. \foreach works fine and \usepackage{pgffor} is declared. Is it possible that \for has been deprecated? I'm using TexLive / TexWorks 2021 edition. Thanks in advance afk

    \documentclass{article}
    \usepackage{tikz}
    \usepackage{pgffor}
    \usetikzlibrary{math}
    \begin{document}
    \begin{tikzpicture}
    %\foreach \n in {1,2,3,4} {$\n$}; This works."1234" displays as expected.
    \for \n in {1,2,3,4} {$\n$}; This reports "Undefined Control Sequence"
    \draw (0,0)--(10,0);   This works also.
    \end{tikzpicture}
    \end{document}
1 Answers

This is the non-working code I submitted in the original post above.

\documentclass{article}
    \usepackage{tikz}
    \usepackage{pgffor}
    \usetikzlibrary{math}
    \begin{document}
    \begin{tikzpicture}
    %\foreach \n in {1,2,3,4} {$\n$}; This works."1234" displays as expected.
    \for \n in {1,2,3,4} {$\n$}; This reports "Undefined Control Sequence"
    \draw (0,0)--(10,0);   This works also.
    \end{tikzpicture}
    \end{document}

Below is the code corrected so it compiles and runs:

- usepackage{pgffor} is removed (See comment from samcarter)
- \tikzmath environment is invoked. 
- The correct loop command is actually "for"  NOT "\for"
- Note that the command argument of the for-loop requires nested braces.
- The advantage I found in "for" as opposed to "\foreach" is that variables
    can be modified with no extra code. This is shown in the second for-loop.

\documentclass{article}
\usepackage{tikz}
\usepackage{pgffor}
\usetikzlibrary{math}
\begin{document}
\begin{tikzpicture}
\tikzmath{
for \n in {1,2,3,4}{{($n =\n $);};}; %  <----double braces here. . .
for \n in {2,3,4,5}{\m =log10(\n); {($Log \n =\m$;};}; % <---. . .and here.
};
\end{tikzpicture}
\end{document}
Related