6

I just cannot debug this code. After typesetting it keeps on giving the error of: "Undefined Control Sequence" When individually coding rectangles for each set of coordinates the code works though, but it is nonetheless cumbersome... What am I missing with \foreach and \draw ???

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepackage{tikz}
\pgfplotsset{compat=1.11}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        xmin = 0,
        xmax = 10,
        ymin = 0,
        ymax = 10,
        ]
        \foreach \linker / \regter in {{(1,1) / (2,2)}, {(2,2) / (3,3)},{(3,3)/(4,4)}} {
                \edef\temp{\draw[thick] \linker rectangle \regter;
                }
                \temp
            }
    \end{axis}
\end{tikzpicture}
\end{document}
0

3 Answers 3

7

You can move the \draw outside of the \foreach and then you do not need to use the expansion:

enter image description here

Notes:

  • Normally one would need to use axis cs: coordinate system within an axis environment. But, as Manuel commented, in pgfplots version 1.11 this is now the default coordinate system. So, if using a version prior to 1.11 each coordinate needs to be specified as

        \foreach \linker / \regter in {
            {(axis cs:1,1) / (axis cs:2,2)}, 
            {(axis cs:2,2) / (axis cs:3,3)},
            {(axis cs:3,3) / (axis cs:4,4)}} 
                {
                    \linker rectangle \regter
                };
    

Code:

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepackage{tikz}
\pgfplotsset{compat=1.11}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        xmin = -1,
        xmax = 10,
        ymin = -1,
        ymax = 10,
        ]
        \draw[thick,blue] 
            \foreach \linker / \regter in {
                {(1,1) / (2,2)}, 
                {(2,2) / (3,3)},
                {(3,3) / (4,4)} } 
                    {
                        \linker rectangle \regter
                    };
    \end{axis}
\end{tikzpicture}
\end{document}
2
  • I'm not sure of this, but I think “for some reason it does not seem to be necessary in this case” is because that's how it works in the last version. I'm out TikZ/pgfplots world, but I think I read that now you don't need axis cs because it's the new way it works.
    – Manuel
    Nov 17, 2014 at 17:41
  • @Manuel: Thanks. You are correct, that is indeed the default coordinate system within the axis environment with pgfplots 1.11. Nov 17, 2014 at 17:49
6

I guess your problem was commas. Commas separate tuples, so if you have a comma in one parameter, you should surround it by curly brackets. For me this code works.

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepackage{tikz}
% \pgfplotsset{compat=1.11}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    xmin = 0,
    xmax = 10,
    ymin = 0,
    ymax = 10,
    ]
  \end{axis}
  \foreach \linker / \regter in {%
    {(1,1)}/{(2,2)}, %
    {(2,2)}/{(3,3)}, %
    {(3,3)}/{(4,4)}%
  } { \draw[thick] \linker rectangle \regter; }
\end{tikzpicture}
\end{document}

result

3
  • My image is quite different.
    – egreg
    Nov 17, 2014 at 17:22
  • Moving the \draw outside of the axis environment changes things considerably. Try, for instance, changing xmin = -2 and you will see that the rectangles have not moved at all, even though the coordinates have. So, even though the drawing almost looks correct in this case, it is not drawn at the desired coordinates. Nov 17, 2014 at 17:52
  • 1
    From the commented line I'm guessing your pgfplots version is different.
    – percusse
    Nov 17, 2014 at 18:00
5

The problem is in passing \draw to \edef; precede it with \noexpand:

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepackage{tikz}
\pgfplotsset{compat=1.11}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
  xmin = 0,
  xmax = 10,
  ymin = 0,
  ymax = 10,
]
\foreach \linker / \regter in {{(1,1) / (2,2)}, {(2,2) / (3,3)},{(3,3)/(4,4)}} {
   \edef\temp{\noexpand\draw[thick] \linker rectangle \regter;}\temp
}
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

3
  • I don't see why the \edef was necessary. Shouldn't you be able to do the \draw directly? Also, I think that the axis cs coordinate system should be specified when accessing coordinates within the axis environment. Nov 17, 2014 at 17:25
  • @PeterGrill Compare with the other answer.
    – egreg
    Nov 17, 2014 at 17:26
  • @PeterGrill If you don't use \edef pgfplots collects four copies of \draw[thick] \linker rectangle \regter; and expands when the visualization phase kicks in. And all instances expand to the same thing. TikZ on the other hand draws a path immediately instead of accumulating until the drawing commands are collected.
    – percusse
    Nov 17, 2014 at 18:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .