9

I would like to reference some plot lines in text/captions. It is possible by defining a label for the plot in the corresponding axis environment. However, I'm using externalization and only want to distribute the PDF files (not the tikzpicture files), but still want to reference plot lines in the text. How to separately add labels for the plot line without having to include the whole tikzpicture?

As a workaround, I include dummy plots and label them, but it results in appearing a dot in the document. Any way to make this tikzpicture hidden and taking zero space would also be useful.

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
This is how I reference to \ref{hwplot1} and \ref{hwplot2}. But the following 'tikzpicture' results in ploting a dot


%%% This is neccessary for drawing plot lines in the text
\begin{tikzpicture}
    \begin{axis}[hide axis]
        \addplot [
        color=red,
        solid,
        line width=0.9pt,
        forget plot
        ]
        (0,0);\label{hwplot1}
        \addplot [
        color=black,
        dashed,
        line width=1.2pt,
        forget plot
        ]
        (0,0);\label{hwplot2}
    \end{axis}
\end{tikzpicture}%
\end{document} 
M.Reza
  • 3,237

3 Answers3

6
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}

\newcommand{\hwplotA}{\raisebox{2pt}{\tikz{\draw[red,solid,line width=0.9pt](0,0) -- (5mm,0);}}}
\newcommand{\hwplotB}{\raisebox{2pt}{\tikz{\draw[black,dashed,line width=1.2pt](0,0) -- (5mm,0);}}}

This is how I reference to {\hwplotA} and {\hwplotB}. 

\end{document} 

inserted lines


When in doubt, use a savebox.

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}

\newcommand{\hwplotA}{\raisebox{2pt}{\tikz{\draw[red,solid,line width=0.9pt](0,0) -- (5mm,0);}}}
\newcommand{\hwplotB}{\raisebox{2pt}{\tikz{\draw[black,dashed,line width=1.2pt](0,0) -- (5mm,0);}}}

\begin{figure}[hp]
\sbox0{\hwplotA}\sbox1{\hwplotB}%
\caption{This is how I reference to \usebox0 and \usebox1.}
\end{figure}

\end{document} 
John Kormylo
  • 79,712
  • 3
  • 50
  • 120
5

This doesn't eliminate the tikz picture, but it scales it to zero size. Here, it is placed in the middle of the last word "nothing".

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
This is how I reference to \ref{hwplot1} and \ref{hwplot2}. But the following
  'tikzpicture' results in plotting no%
%%% This is neccessary for drawing plot lines in the text
\scalebox{0}{%
\begin{tikzpicture}
    \begin{axis}[hide axis]
        \addplot [
        color=red,
        solid,
        line width=0.9pt,
        forget plot
        ]
        (0,0);\label{hwplot1}
        \addplot [
        color=black,
        dashed,
        line width=1.2pt,
        forget plot
        ]
        (0,0);\label{hwplot2}
    \end{axis}
\end{tikzpicture}%
}%
thing
\end{document} 

enter image description here

2

For my and others future reference, I have taken the excellent example by @steven-b-segletes and generalised it for the automated creation of labels from a cycle list. This is great for reusing labels where you have the legend in a caption, or are using the animate package and dont want 300 repeated references

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\newcommand*{\graphlinewidth}{1}
\pgfplotscreateplotcyclelist{mylistNoMarkDarkColour}{%
  {red!80!black,    solid,      line width=\graphlinewidth, mark=none},
  {green!80!black,  solid,      line width=\graphlinewidth, mark=none},
  {blue,    solid,          line width=\graphlinewidth, mark=none},
  {purple,  solid,          line width=\graphlinewidth, mark=none},
  {orange,  solid,          line width=\graphlinewidth, mark=none},
}

% Generate the label references for legends in captions from the given cyle list and reference.
% Also avoids issues with TiKz externalize
% 1: Cycle list name, 2: Reference prefix, 3: Number to produce
\newcommand{\pgfplotsReferenceGenerator}[3]{%
\scalebox{0}{%
\begin{tikzpicture}
\begin{axis}[hide axis, cycle list name = #1]
  \foreach \i   [evaluate=\i] in {1,...,#2}{
    \addplot (0,0); \label{#3\i}
  }
\end{axis}
\end{tikzpicture}%
}%
}

\begin{document}

\pgfplotsReferenceGenerator{mylistNoMarkDarkColour}{5}{dark}
This is great for reusing labels where you have the legend in a caption,
or are using the animate package and dont want 300 repeated references (i.e one \ref{dark1} per frame)
like \ref{dark2} and \ref{dark3} with \ref{dark4} finally \ref{dark5}

\end{document}

What I have not been able to do and would appreciate feedback on:

  1. Automatically detect the length of the cycle list
  2. Correctly disable TiKz externalisation for the graph (I couldn't make it work...) enter image description here
SpmP
  • 1,201