# How to label a TiKz shape for referencing its legend in the caption

How can I label a TiKz line style for further referencing in figure caption. For \addplot the label just follows it. However, I don't know how to do it for \draw.

MWE:

\documentclass{book}
\usepackage{lipsum}
\usepackage{caption}
\usepackage{pgfplots}
\pgfplotsset{compat=1.3}
\tikzset{pointille/.style={dash pattern = on 2pt off 2pt on 6pt off 2pt}}
\tikzset{points/.style={dash pattern = on 1pt off 1pt}}
\tikzset{tirets/.style={dash pattern = on 5pt off 5pt}}

\begin{document}
\lipsum[1]
\begin{figure}[!ht]
\centering
\captionsetup{width=9.5cm}
\pgfplotsset{every axis plot post/.append style={mark=none,line width=1.5pt}}
\begin{tikzpicture}
\begin{axis}[height=7cm,width=9cm,grid=major,xlabel=$x$,ylabel=$f(x)$,
tick label style={font=\footnotesize},label style={font=\small},max space between ticks=45, major tick length=0cm,minor tick length=0cm,enlargelimits=false,]
\end{axis}

\draw [dashed,red,thick] (1,1) rectangle (2,2); \label{p7}
\draw [solid,blue,thick] (1,1)-- (2,2); \label{p8}
\end{tikzpicture}
\caption[Caption in ToC]{This is a plot about colored curves: $f(x)=2 x$ (\ref{p4}),  $f(x)=0.5 x^2$ (\ref{p5}), and $f(x)=-0.125 x^3$ (\ref{p6}), (\ref{p7}), and (\ref{p8})}
\end{figure}
\lipsum[1]
\end{document}


• Side note: If you want circles instead of rectangles for your points style, you can set \tikzset{points/.style={line cap=round, dash pattern = on 0pt off 3pt}} (see tex.stackexchange.com/a/101263/2552) – Jake Sep 24 '13 at 7:35

That \label/\ref functionality is provided by PGFPlots (not TikZ), and only works for plots, not arbitrary \draw commands.

You could achieve the desired result by using an \addplot command for drawing the objects: You can replace \draw [dashed,red,thick] (1,1) rectangle (2,2); with \addplot [dashed,red,thick] coordinates {(1,1)} rectangle (2,2);. You only need to provide the first coordinate of your path in the coordinates list, after that you can use normal TikZ path commands like rectangle or --. Note that the coordinate system is scaled inside the axis environment, so your best bet is to work with (axis cs:<x>,<y>) coordinates instead of trying to find the right values using "normal" coordinates.

\documentclass{book}
\usepackage{lipsum}
\usepackage{caption}
\usepackage{pgfplots}
\pgfplotsset{compat=1.3}
\tikzset{pointille/.style={dash pattern = on 2pt off 2pt on 6pt off 2pt}}
\tikzset{points/.style={dash pattern = on 1pt off 1pt}}
\tikzset{tirets/.style={dash pattern = on 5pt off 5pt}}

\begin{document}
\lipsum[1]
\begin{figure}[!ht]
\centering
\captionsetup{width=9.5cm}
\pgfplotsset{every axis plot post/.append style={mark=none,line width=1.5pt}}
\begin{tikzpicture}
\begin{axis}[height=7cm,width=9cm,grid=major,xlabel=$x$,ylabel=$f(x)$,
tick label style={font=\footnotesize},label style={font=\small},max space between ticks=45, major tick length=0cm,minor tick length=0cm,enlargelimits=false,]
\addplot [dashed,red,thick] coordinates {(-4,-10)} rectangle (axis cs:-2,-3); \label{p7}
\addplot [solid,blue,thick] coordinates {(-4, -10)} -- (axis cs:-2,-3); \label{p8}
\end{axis}

\end{tikzpicture}
\caption[Caption in ToC]{This is a plot about colored curves: $f(x)=2 x$ (\ref{p4}),  $f(x)=0.5 x^2$ (\ref{p5}), and $f(x)=-0.125 x^3$ (\ref{p6}), (\ref{p7}), and (\ref{p8})}
\end{figure}
\lipsum[1]
\end{document}

• Thanks Jake, I have no idea why this doesn't work for PGFplots while I could use it for \draw: \addplot [red,thick,dashed,forget plot] coordinates {({axis cs:1.0,0}|-{rel axis cs:0,1})} -- ({axis cs:1.0,0}|-{rel axis cs:0,0.0});\label{vertline} – M.Reza Sep 24 '13 at 7:58
• @M.Reza: The coordinates provided in \addplot coordinates {...} must be a simple list of coordinates: No axis cs (they're already in the axis coordinate system), no path commands. You only need (and should) provide the first coordinate in that list, so you could say \addplot [red,thick,dashed,forget plot] coordinates {(1,1)} -- ({axis cs:1.0,0}|-{rel axis cs:0,0.0}). That coordinate expression is a bit weird, though: Why are you using the orthogonal (|-) expression here? (1,0|-0,1) is equivalent to (1,1). What are you trying to do? – Jake Sep 24 '13 at 8:04
• I'm trying to plot a vertical line on a plot by using: \draw [color=red,thick,dashed]({axis cs:1.0,0}|-{rel axis cs:0,1}) -- ({axis cs:1.0,0}|-{rel axis cs:0,0.0}); \end{axis}. The reason I chose this is that, I can use the axis coordinates the first part {axis cs:1.0,0} (like specific point on x axis), but I don't need to calculate the y coordinates for the second part |-{rel axis cs:0,1}) as rel axis cs:0,1 places the point at the top of the plot regardless of y coordinates. I am not sure though if I'm using it correctly! – M.Reza Sep 24 '13 at 8:18
• @M.Reza: Ah, oops, I missed the rel bit. Yeah, that makes sense. In that case, I would use \makeatletter \addplot [red,thick,dashed,forget plot] coordinates {(\pgfplots@data@xmin,\pgfplots@data@ymin)} ({axis cs:1.0,0}|-{rel axis cs:0,1}) -- ({axis cs:1.0,0}|-{rel axis cs:0,0.0});. That way, the coordinate supplied in \addplot coordinates {...} doesn't impact the axis limits. – Jake Sep 24 '13 at 8:26