# \label \ref pair in pgfplots does not work on running latex command second time

I am preparing a long document with Latex on Linux. The document uses lots of plots including 3D plots. Without externalizing the plots, latex gives me a message of memory problem. After much searching on forums and reading about the pgfplots document (http://pgfplots.sourceforge.net/pgfplots.pdf) on Page 464, I realized that the problem is due to the \label \ref pair in my plots as I use \label inside the pgfplot code, and \ref in the caption to refer to the marker line. The Latex document compiles successfully on first run and creates all the necessary files such as .dpth .spl .eps .dvi .log on the first latex run. But when I run latex for the second time, I get this error:

! Package tikz Error: Sorry, the system call 'latex -shell-escape -halt-on-erro r -interaction=batchmode -jobname "elsarticle-template-1a-num-figure_crossref0" "\def\tikzexternalrealjob{elsarticle-template-1a-num}\input{elsarticle-templat e-1a-num}" && dvips -o "elsarticle-template-1a-num-figure_crossref0".eps "elsar ticle-template-1a-num-figure_crossref0".dvi' did NOT result in a usable output file 'elsarticle-template-1a-num-figure_crossref0' (expected one of .epsi:.eps: .ps:). Please verify that you have enabled system calls. For pdflatex, this is 'pdflatex -shell-escape'. Sometimes it is also named 'write 18' or something li ke that. Or maybe the command simply failed? Error messages can be found in 'el sarticle-template-1a-num-figure_crossref0.log'. If you continue now, I'll try t o typeset the picture.

I am using the following latex commands two times:

latex -shell-escape elsarticle-template-1a-num.tex
latex -shell-escape elsarticle-template-1a-num.tex


This is how my document looks:

    \documentclass[preprint,12pt]{elsarticle}

\usepackage[usenames,dvipsnames]{pstricks}
\usepackage{epsfig}
\usepackage{pst-grad} % For gradients
\usepackage{pst-plot} % For axes
\usepackage{pstricks-add}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage[lined,boxed,commentsnumbered]{algorithm2e}
\usepackage{subfigure}
\usepackage{pgfplots}
\usetikzlibrary{patterns}
\usepackage{tikz}
\usepgfplotslibrary{external}
\tikzexternalize
\tikzset{external/system call={latex \tikzexternalcheckshellescape -halt-on-error -interaction=batchmode -jobname "\image" "\texsource" && dvips -o "\image".eps "\image".dvi}}
\begin{document}
Lots of text content here. Also some pstricks images.
%Plots begin here. Here I use \label \ref
\begin{table}[!htb]
\begin{minipage}{.5\linewidth}
\input{alpha_0_vary_results_1.txt}
\end{minipage}%
\begin{minipage}{.5\linewidth}
\input{alpha_0_vary_results_2.txt}
\end{minipage}
\begin{minipage}{.5\linewidth}
\input{alpha_0_vary_results_3.txt}
\end{minipage}%
\begin{minipage}{.5\linewidth}
\input{alpha_0_vary_results_4.txt}
\end{minipage}
\caption{Values of $$\alpha$$ from [0,1] in different data. The line denoted by \ref{abc} marker is the \texttt{abc}. The line with \ref{new_one} marker is the \texttt{New One} scheme.}
\label{alpha_0_vary_results_figure}
\end{table}
\bibliographystyle{abbrv}
\bibliography{references.bib}

\end{document}


This is one of the pgfplots files. Showing alpha_0_vary_results_1.txt file.

\begin{tikzpicture}[scale=0.8]
\begin{axis}[title=Data 1,xlabel=$$\alpha$$,ylabel=Measure]
\addplot[color=black,mark=x , mark size=4] coordinates {
(10,100)
(20,150)
(40,225)
(80,340)
(160,510)
(320,765)
(640,1150)
};\label{abc}
\addplot+[color=black,mark=o , mark size=4] coordinates {
(10,130)
(20,110)
(40,155)
(80,130)
(160,210)
(320,735)
(640,950)
};\label{new_one}
\end{axis}
\end{tikzpicture}


I get no errors in running latex two times using the above commands when I remove all the plots from my document. So this means the problem is related to the plots.

• I am using pgfplots version 2.10 – puser May 7 '14 at 1:39

## 1 Answer

I could find out the solution to my problem after much searching and testing. This is what I did now. This is how the beginning of the document looks now:

\usepgfplotslibrary{external}
\tikzexternalize
\pgfkeys{
/tikz/external/mode=list and make
}
\tikzset{external/system call={latex \tikzexternalcheckshellescape -halt-on-error -interaction=batchmode -jobname "\image" "\texsource" && dvips -o "\image".eps "\image".dvi}}


Then I made some changes to the way I compile latex.

latex -shell-escape file_name.tex

bibtex file_name.aux
bibtex file_name.aux

latex -shell-escape file_name.tex

make -j 4 -B -f *.makefile

latex -shell-escape file_name.tex
latex -shell-escape file_name.tex

bibtex file_name.aux
make -j 4 -f *.makefile
latex -shell-escape file_name.tex


I have noted that if I don't follow the above compilation sequence, there are some problems in the final PDF file generated. For example, in the caption area I don't see the marker along with its line, only "??" sign.