2

I got a weird bug when I want to define and name a list of coordinates

\tikz{
\foreach \name/\pos in {{a/(1,0)}, {b/(0,1)}} 
  \coordinate (\name) at \pos ;
}

The error is

{/usr/local/texlive/2023/texmf-var/fonts/map/pdftex/updmap/pdftex.map}] [2] [3]
Runaway argument?
 \pos ;\pgffor@endhook \ifx \pgffor@assign@after@code \pgfutil@empty \ETC.
! Paragraph ended before \tikz@@coordinate@@at was complete.
<to be read again> 
                   \par 
l.44 

The manual expansion of the list works. I can also use foreach with the following syntax which seems to me less natural

\tikz{
      \foreach \name/\pos in {{a/(1,0)}, {b/(0,1)}} 
      \draw \pos node (\name) {\name};
      \draw (a)--(b);
}

A full example with swapped variables does not work either.

\documentclass[tikz,border=5pt]{standalone}

\usepackage[utf8]{inputenc}
\usetikzlibrary{scopes, arrows, decorations.markings, calc, intersections}

\begin{document}

\begin{tikzpicture}
    \foreach \pos/\name in {{(1,0)}/a, {(0,1)}/b}
      \coordinate (\name) at \pos;
    \draw (a)--(b);
\end{tikzpicture}

\end{document}
4
  • 6
    Swap the variables: \pos/\name in {(1,0)/a, …}. The , in the coordinate is tripping you up. If the entry starts with ( special parsing is activated. Alternatively, put the coordinate in braces, i.e. a/{(1,0)}. Your workaround does similar things but on a broader scope. Commented Sep 17, 2023 at 17:07
  • 2
    Please provide complete code to reproduce the error you show. (Generally - I think you have an answer here already.)
    – cfr
    Commented Sep 18, 2023 at 5:04
  • 1
    your code works well! Please update your TeX distribution.
    – Black Mild
    Commented Sep 18, 2023 at 6:03
  • 1
    Ah, I think I've missed something with my first comment – didn't notice the braces in the first code. But @BlackMild is correct. With an uptodate TikZ, this shouldn't be a problem. Anyway, a workaround might be \coordinate[at/.expanded=\pos](\name);. Commented Sep 18, 2023 at 21:10

0

You must log in to answer this question.

Browse other questions tagged .