0

I am trying to do something that I thought was simple -- take a comma-separated list and put the elements of the list each in their own node using \foreach:

\documentclass{article}

\usepackage{tikz}
\usepackage{xparse}

\usetikzlibrary{positioning,calc}

\NewDocumentEnvironment{footest}{m}{%
\begin{tikzpicture}
    \node[inner sep=0pt] (A0) at (0,0) {};
    \foreach \n [count=\ingnum from 1] in {#1}
        {%
        \pgfmathsetmacro{\ntmp}{\ingnum-1}
        %% This at least does not give an error, but is not correct:
        % \node[below=10pt of A\ntmp,draw,anchor=north east] (A\ingnum)
        \node[below=10pt of A\ntmp.south west,draw,anchor=north east] (A\ingnum)
            {\parbox{1in}{\strut\n\strut}};
        }%
}{\end{tikzpicture}}

\begin{document}

\begin{footest}{1,2,3,4 bla bla bla bla bla blba,5}\end{footest}

\end{document}

The error is Package PGF Math Error: Unknown operators' or so' (in '0.south west'). I've tried enclosing A\ntmp.south west in various combinations of parentheses and braces all to no avail. If I remove the .south west then the code typesets but vertically large boxes (4 in the example) will overprint the next node below. Examples in the TikZ documentation (version 3.1.5b, page 241 for example) seem to suggest that this should be possible. Probably overlooking something basic...

0

2 Answers 2

2

If you outcomment the node line and add \typeout{\ntmp} then the log lists

0.0
1.0
2.0
3.0
4.0

Try \pgfmathsetmacro{\ntmp}{int(\ingnum-1)} instead or use

\pgfmathtruncatemacro{\ntmp}{\ingnum-1}
1
  • Works like a charm. Thanks.
    – sgmoye
    Jan 28, 2020 at 13:21
3

@daleif explained the problem well. Here is another way to write the fixed code, using the \foreach ... [..., evaluate=... as ... using ...] in {...} construct and the /tikz/text width node option instead of the \parbox{1in} (as far as I understand it, /tikz/text width should work even in documents using a non-LaTeX format, such as plain TeX or ConTeXt):

\documentclass{article}
\usepackage{xparse}
\usepackage{tikz}
\usetikzlibrary{positioning}

\NewDocumentEnvironment{footest}{m}
  {%
    \begin{tikzpicture}
      \node[inner sep=0pt] (A0) at (0,0) {};
      \foreach \n [count=\ingnum from 1,
                   evaluate=\ingnum as \ntmp using int(\ingnum-1)] in {#1}
        {
          \node[below=10pt of A\ntmp.south west, draw, anchor=north east,
                text width=1in] (A\ingnum) {\strut\n\strut};
        }
  }
  {\end{tikzpicture}}

\begin{document}

\begin{footest}{1, 2, 3, 4 bla bla bla bla bla bla, 5}
\end{footest}

\end{document}

screenshot

5
  • Very nice indeed. Would not have thought of that...
    – sgmoye
    Jan 28, 2020 at 14:14
  • Well, except the /tikz/text width, this is just syntactic sugar. :-)
    – frougon
    Jan 28, 2020 at 14:16
  • 1
    Yes, but like all sugar, it is enjoyably sweet ;-)
    – sgmoye
    Jan 28, 2020 at 14:28
  • There are always smarter tikz stuff that I keep missing
    – daleif
    Jan 28, 2020 at 15:38
  • @daleif With a 1300+ pages long manual (not counting pgfplots, pgfplotstable, forest, tikzcd, etc.), there will always be smart TikZ things I don't know, or forgot. Oh well...
    – frougon
    Jan 28, 2020 at 15:42

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.