1

Consider this MWE:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usetikzlibrary{calc}

\pgfplotstableread[col sep=space,row sep=newline,header=true]{
ts   val
0.0  0.0
1.0  1.0
1.1  4.9
2.0  2.0
2.2  4.9
4.8  0.2
}\mytable

\def\drawLines{%
  \pgfplotstableforeachcolumnelement{[index]0}\of\mytable\as\cx{%
    % \node{I have now cell element ‘\cx’ at row index ‘\pgfplotstablerow’;\par};
    \edef\temp{ %
      \noexpand\draw[%
        draw=black,%
        fill=none,%
        opacity=0.7,%
      ] let \noexpand\p1=({axis cs:\cx,0}), \noexpand\p2=({rel axis cs:0,0}),
        \noexpand\p3=({rel axis cs:1,1}) in
        (\noexpand\x1,\noexpand\y2) -- (\noexpand\x1,\noexpand\y3)
      ; %
    }
    \temp
  }
}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[extra x ticks={3.5}]
\addplot[smooth] table {\mytable};
\addplot[only marks,mark=*,mark options={color=blue}] table {\mytable};
\draw[red] let \p1=(rel axis cs:0,0), \p2=(rel axis cs:1,1), \p3=(axis cs:3.5,0)
  in (\x3,\y1) -- (\x3,\y2);
\drawLines % passes no problem
\end{axis}
\drawLines % fails w/ "! Missing \endcsname inserted." \pgfcrdmth@x
\end{tikzpicture}
\end{document}

As it is, it will fail with:

! Missing \endcsname inserted.
<to be read again> 
                   \pgfcrdmth@x 
l.42 \drawLines
                % fails w/ "! Missing \endcsname inserted." \pgfcrdmth@x

... but if you comment the line, marked with % fails... - then the document compiles successfully, and as expected? So obviously, the \drawLines function, which calls \draw via \pgfplotstableforeachcolumnelement, does work - but only from inside an {axis}, not outside it?

Why does this happen - and how could I get this macro to execute outside of {axis}, while still in {tikzpicture}? How can I otherwise know, which code can be ran from where?

PS: As it is, this looks like Why doesn't a \foreach variable work in \draw in an \axis?, but I'm guessing that question could have been answered with the \edef\temp...\temp thing that I'm using here; which is why this problem puzzles me...

sdaau
  • 17,079
  • 1
    rel axis is not defined outside the axis environment. – percusse Jul 05 '14 at 11:24
  • Thanks @percusse - had no idea about that! Anyways, just tried replacing all instances of rel axis to axis in the code above, and it still fails with same message as above - so there is possibly something else here as well? – sdaau Jul 05 '14 at 16:28
  • 1
    you are mixing pgfplots syntax with TikZ. TikZ doesn't know anything about axis environment. So you need to put coordinates either in after end axis or you need to refer to coordinates that are created in the axis environment. – percusse Jul 05 '14 at 16:44
  • @percusse Do you want to write up an answer? – Torbjørn T. Sep 05 '15 at 20:41

1 Answers1

3

The axis related commands of pgfplots only make sense inside the axis environment. To be able to access nontrivial parts of a plot, you need to mark them with coordinates or nodes within the axis environment and then those can be accessed from outside in the same TikZ picture.

For corners of the axis and similar rather obvious parts you label the axis and refer to its corners just as you do with regular TikZ nodes.

\begin{tikzpicture}
\begin{axis}[name=myaxis]
  ....
\end{axis}
\draw[red,thick] (myaxis.south) -- (myaxis.north);
\end{tikzpicture}

enter image description here

percusse
  • 157,807