# A question on tikz/pgf externalization, standalone, and includeonly for compiling large document

I have a goal to build an efficient compiling environment to build large documents. The requirements are these:

1. Use tikz/pgf to draw many figures;
2. Use \includeonly to build only a portion of the large document at a time.
3. Make the compilation to only rebuild the modified tikz/pgf figures.

I know how to make tikz/pgf figures to be built using the externalized mode. However, the externalized mode does not work with \includeonly.

Moreover, I have seen some posts on rebuilding the modified tikz/pgf figures using the standalone package. Some posts even said that it is better not to use tikz/pgf externalized mode and the standalone package together. I am quite confused on this claim. Can anyone do a clarification on the joint use of tikz/pgf externalized mode and the standalone package?

I would like to ask the experts for existing tutorials or examples on making tikz/pgf externalized mode to work with \includeonly.

Below is a minimal working example.

main.tex

\documentclass[\myopts]{article}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{tikzscale}

\usetikzlibrary{external}
\tikzexternalize
%\tikzset{external/mode=list and make}
%\tikzset{external/check=diff}
\tikzset{external/force remake}
%\tikzsetexternalprefix{figure-build/, up to date check=md5, force remake}

\pgfplotsset{compat=1.8}
%\pgfplotsset{compat=newest} % necessary for new features

\usepackage{ifpdf}

\ifpdf
\tikzset{external/system call={%
pdflatex \tikzexternalcheckshellescape -halt-on-error -interaction=batchmode
-jobname "\image" "\string\def\string\myopts{\myopts}\texsource"}}
\else
\tikzset{external/system call={latex \tikzexternalcheckshellescape -halt-on-error -interaction=batchmode -jobname "\image" "\string\def\string\myopts{\myopts}\texsource"; dvips -E -o "\image".eps "\image".dvi; ps2pdf "\image".eps "\image".pdf;}}
\fi

\usepackage{import}
\usepackage{filecontents}

\begin{filecontents}{datafile}
-0.000632
0.004692
0.001407
0.005768
0.003117
0.001605
0.001742
-0.000874
-0.000973
-0.000436
-0.001578
-0.001953
-0.004275
-0.001585
-0.000957
-0.000819
-0.000924
-0.003559
0.001949
-0.000300
-0.003085
-0.002050
-0.000493
-0.001378
-0.000970
0.000912
0.001667
-0.000974
-0.000592
-0.000865
-0.000933
0.000435
-0.001278
0.001508
-0.000566
-0.003608
0.002768
0.005676
0.000331
-0.000527
-0.000589
-0.000751
-0.000503
-0.000718
-0.000950
-0.005616
-0.004347
-0.001818
-0.000503
0.001063
0.002109
-0.004156
-0.002612
-0.000773
-0.000635
-0.000218
-0.001041
-0.001247
-0.000283
-0.004434
-0.000370
0.002669
0.001302
0.001351
0.001618
0.002028
0.001291
0.004341
0.001141
0.001058
-0.004537
-0.000123
-0.000052
0.000010
-0.000643
0.001403
-0.000751
-0.000201
-0.001639
-0.000316
-0.000757
-0.000872
0.000530
-0.000969
-0.000826
-0.001076
-0.000808
0.001687
0.001356
0.001870
0.001016
0.002165
0.005372
0.001128
0.002269
0.001878
0.000839
-0.000798
-0.000846
0.000146
\end{filecontents}

\begin{filecontents}{histogram.tikz}
\tikzsetnextfilename{Fig-Histogram}
\begin{tikzpicture}
\begin{axis}[
xmode=linear,
ymode=linear,
axis x line*=bottom,
axis y line*=left,
tick label style={font=\small},
grid=both,
tick align=outside,
tickpos=left,
xlabel=Bins,
ylabel=Count,
width=0.45\textwidth,
height=0.4\textwidth,
]
\addplot+[raw gnuplot, ybar, bar width=0.002, color=red] gnuplot {
binwidth=0.005;
bin(x,bw)=bw*floor(x/bw);
plot "datafile.tex" using (bin(\$1,binwidth)):(1.0) smooth freq with boxes;
};
\end{axis}
\end{tikzpicture}
\end{filecontents}

\begin{filecontents}{mainbody.tex}
\begin{figure}[bth]
\begin{tabular}{p{3in}}
\includegraphics[]{histogram}
\end{tabular}
\caption{\small{Histogram.}}
\end{figure}
\end{filecontents}

\begin{filecontents}{doc1.tex}
\begin{figure}[bth]
\begin{tabular}{p{3in}}
\includegraphics[]{histogram}
\end{tabular}
\caption{\small{Histogram.}}
\end{figure}
\end{filecontents}

\begin{filecontents}{doc2.tex}
\begin{figure}[bth]
\begin{tabular}{p{3in}}
\includegraphics[]{histogram}
\end{tabular}
\caption{\small{Histogram.}}
\end{figure}
\end{filecontents}

\includeonly{\myfilename}

\begin{document}
\setcounter{page}{1}
\subincludefrom{./}{mainbody}
\setcounter{page}{1}
\subincludefrom{./}{doc1}
\setcounter{page}{1}
\subincludefrom{./}{doc2}
\end{document}


I created a Makefile as below.

allinone:
pdflatex -shell-escape "\def\myopts{11pt}\def\myfilename{mainbody,doc1,doc2}\input{main}"

doc1:
pdflatex -shell-escape "\def\myopts{11pt}\def\myfilename{doc1}\input{main}"

doc2:
pdflatex -shell-escape "\def\myopts{11pt}\def\myfilename{doc2}\input{main}"

mainbody:
pdflatex -shell-escape "\def\myopts{11pt}\def\myfilename{mainbody}\input{main}"


If all components are compiled, then run

make allinone


If only a single component is compiled, the run

make doc1
make doc2
make maninbody


-
Why not using \include and commenting out the irrelevant ones? – percusse Sep 3 '13 at 20:52
Using \include and commenting out the irrelevant ones are too brute force. I believe there is an elegant solution. – Novice User Sep 3 '13 at 21:14
you place % to the line start instead of typing into \includeonly. What is brute in that? – percusse Sep 3 '13 at 21:28
My make environment uses \includeonly to build each piece of a large document on demand. For example, when writing a proposal, there are many components of the proposal. The submission web site requires me to upload the components piece-by-piece in different input windows. Anyway, I need to use \includeonly in many cases in my work. – Novice User Sep 3 '13 at 21:34

I know I'm a little bit late, but I have encountered the same problem.

The easiest solution is to set a different filename prefix for each part you want to standalone-compile. Hence, the counter used for numbering the externalized figures will be reset at each new part.

Here's an example:

\includeonly{Chapter1,Chapter2} % or whatever

{\tikzsetfigurename{External-TikZ/Chap1_}
\include{Chapter1}
}
{\tikzsetfigurename{External-TikZ/Chap2_}
\include{Chapter2}
}
{\tikzsetfigurename{External-TikZ/Chap3_}
\include{Chapter3}
}


All options for the externalization procedure are detailed in the PGF manual (pp. 615-625) Regards.

-
Welcome to TeX.SX! Unfortunately this does not seem to answer the question, as the poster asks to use \includeonly not \include. – Andrew Swann Sep 1 '14 at 12:34
Thank you! \include and \includeonly are supposed to work together, since \includeonly just neutralize all following \include commands, except that specified (but it keeps the cross-references right, while commenting out the irrelevant lines won't). – Dorian Sep 1 '14 at 13:40