# pgf/tikz externalization + tabu: makefile and legend to name problems

I am having troubles with the combination of tabu and pgf/tikz exernalization. Please consider the code snippet below which I try to compile using lualatex or pdflatex using texlive 2018.

\documentclass{book}

\usepackage{filecontents}
\newcommand{\legendname}{defaultlegendname}

\begin{filecontents*}{test.tikz}
\tikz{\begin{axis}[
width=4cm,height=4cm,
]
table[row sep=crcr] {%
1 1\\
2 4\\
};
\label{testAAA}
\end{axis}
\begin{axis}[
legend to name=\legendname,
width=4cm,height=4cm,
at={(4.1cm,0cm)}
]
%
table[row sep=crcr] {%
1 2\\
5 10\\
};
\end{axis}}

\end{filecontents*}

\usepackage{tikz,pgfplots,pgfplotstable,subcaption,tabu}
\usetikzlibrary{external}
\tikzexternalize[mode=list and make]
\begin{document}
\begin{figure}
\begin{tabu} to \linewidth {X}
\renewcommand{\legendname}{fig1-legend}\ref{\legendname}\\
\tikzsetnextfilename{fig1}\subcaptionbox{}{\input{test.tikz}}\\
\end{tabu}
\end{figure}
\end{document}


My compilation procedure is as follows: 1) Lualatex which gives me the heads-up that there are figures to be updated.

The figfile and makefile are given as:

% cat test.figlist
fig1
% cat test.makefile
fig1.pdf:
lualatex -shell-escape -halt-on-error -interaction=batchmode -jobname "fig1" "\def\tikzexternalrealjob{test}\input{test}"

fig1.pdf: fig1.md5


2) make -f test.makefile which completes without errors

3) 2 times lualatex. Now there is no heads-up for figures to be updated (I would have expected so, because the legend is not yet externalized). Instead, I get the following updated makefile

% cat test.makefile
fig1.pdf:
lualatex -shell-escape -halt-on-error -interaction=batchmode -jobname "fig1" "\def\tikzexternalrealjob{test}\input{test}"

fig1.pdf: fig1.md5
fig1.pdf: FORCEREMAKE


4) I run the makefile again, to let latex know that there are references which are not yet up-to-date, but it gives me

% make -f test.makefile
make: *** No rule to make target 'FORCEREMAKE', needed by 'fig1.pdf'.  Stop.


5) Due to the bogus makefile, I run the command in the makefile manually. That works without problems.

6) I run lualatex again, to see whether he noticed that the legend still needs to be build. But still he doesn't notices and, hence, also doesn't update figlist or makefile

Consequently, I also see the actual figure with question marks for the legend. Outside/without the tabu environment, it seems to work fine. But I have several multi-panel figures where tabu is very handy.

What am I doing wrong here?

This seems to work:

I put the legend inside the tikzpicture, inspired by Stefan Pinnow's answer to a similar question.

Because the label's information is stored in the main .aux file, you have to compile first, then do make fig1.pdf, compile again, do make fig1.pdf again to get generate the right image, and finally compile again to get the right image in your document. Note that I'm on Windows and thus cannot directly test the makefile, but I simply copied its command and executed in the command prompt.

This also seems to work with fine with tabu, but I believe I heard/read somewhere that tabu is considered deprecated and buggy.

\documentclass{book}

\usepackage{tikz,pgfplots,pgfplotstable,subcaption,tabu}
\usetikzlibrary{
matrix,
pgfplots.external,
}
\tikzexternalize[mode=list and make]

\pgfplotsset{compat=1.3}

\usepackage{filecontents}
\newcommand{\legendname}{defaultlegendname}

\begin{filecontents*}{test.tikz}
\tikzsetnextfilename{fig1}
\begin{tikzpicture}
\begin{axis}[
width=4cm,
height=4cm,
]
\addplot +[] table[row sep=crcr] {%
1 1\\
2 4\\
};
\label{plots:AAA}
\coordinate (top) at (rel axis cs:0,1);% coordinate at top of the first plot
\end{axis}
\begin{axis}[
width=4cm,
height=4cm,
at={(4.1cm,0cm)}
]
\addplot +[draw=red] table[row sep=crcr] {%
1 2\\
5 10\\
};
\label{plots:BBB}
\coordinate (bot) at (rel axis cs:1,0);% coordinate at bottom of the last plot
\end{axis}

\path (top|-current bounding box.north)--
coordinate(legendpos)
(bot|-current bounding box.north);

\matrix[
matrix of nodes,
anchor=south,
draw,
inner sep=0.2em,
] at ([yshift=1ex]legendpos) {
\ref{plots:AAA} & AAA \\
\ref{plots:BBB} & BBB \\
};
\end{tikzpicture}
\end{filecontents*}

\begin{document}
\begin{figure}
\begin{tabu} to \linewidth {X}
\subcaptionbox{}{\input{test.tikz}}\\
\end{tabu}
\end{figure}
\end{document}

• (+1) for 'tabu is ... buggy'. It is actually worse than this: at some point, it will be updated and that update will be backwards incompatible. That is, all existing code will break. No compatibility option will be offered. It would be better if it were deprecated (as well as buggy). – cfr Aug 23 '18 at 23:49
• @Max thanks, but for me its unsatisfying work-around. The reason why I used legend to name in the first place, is that I don't want to fiddle with the legend position in the first place and leave it to the table to put it nicely. – bonanza Aug 26 '18 at 15:30
• @cfr, for me, the problem does not only occur with tabu but also with tabularx. – bonanza Aug 26 '18 at 15:30
• @bonanza The problem is with externalizing the labels, and referring to them in the main text. I cannot find a solution for that, but it has nothing to do with tabu. – Max Aug 26 '18 at 18:21
• @Max the normal referencing in the text works with \ref and legend to name as in the MWE. But the problem is that tabu and tabularx seem to break that (just removing the tabu environment compiles properly) . – bonanza Aug 26 '18 at 18:48