2

I'm trying to figure out why the externalization of tikzpictures does not work any more (used to).

I have the following in the preamble:

\usepgfplotslibrary{external}
% \tikzexternalize[prefix=fig/ext/]

If I uncomment the second (to externalize all the plots and figures) line all the figures fail the externalization.

If the line is commented out (no externalization), the final pdf is generated without errors.

Unfortunately, I cannot recognize useful elements in the externalized log files.

Which is the best way to troubleshoot this? I suspect packages incompatibilities.

Can't post a MWE right away (will write one as soon as I am able to),

Here's a not-so-minimal non WE, reproducing the behavior described above:

\RequirePackage[hyphens]{url}
\RequirePackage[final]{graphicx}
\documentclass[a4paper,twoside,openright,final]{memoir}
\usepackage{filecontents,lipsum}

\begin{filecontents*}{packages.tex}
    \usepackage{calc}
    \usepackage{ifoddpage}
    \usepackage[osf]{mathpazo}
    \linespread{1.1}
    \usepackage[scaled]{helvet}
    \usepackage{courier}
    \normalfont

    \usepackage[utf8]{inputenc}
    \usepackage[T1]{fontenc}
    \usepackage[italian,english]{babel}
    \usepackage[obeyFinal]{todonotes}

    \usepackage{pgfplots,tikzscale}
    \pgfplotsset{compat=newest}
    \usepgfplotslibrary{units}
    \usetikzlibrary{plotmarks,positioning,chains,fit,shapes,calc}

    \usepackage{array,booktabs,tabularx}
    \usepackage{mdframed,colortbl}
    \usepackage{multirow}
    \usepackage{pdflscape,rotating}

    \usepackage[shortcuts,noredefwarn,toc,symbols,acronym,nonumberlist]{glossaries}

    \usepackage{amsfonts,amsmath,amssymb,amsthm,bm,bigints}
    \usepackage{dsfont}
    \usepackage{siunitx}
    \usepackage{xspace}
    \usepackage{csquotes}
    \usepackage{soul}

    \usepackage[vlined,ruled,commentsnumbered,linesnumbered]{algorithm2e}
    \usepackage[%
        bibstyle     = ieee,
        citestyle    = numeric,
        isbn         = true,
        doi          = false,
        sorting      = nty,
        % sorting     = none,
        % sorting     = debug,
        url          = false,
        defernumbers = true,
        bibencoding  = utf8,
        backend      = biber
    ]{biblatex}

    % % do not externalize todonotes' graphics
    \usepackage{letltxmacro} % https://tex.stackexchange.com/a/115095/177

    \LetLtxMacro{\oldmissingfigure}{\missingfigure}
    \renewcommand{\missingfigure}[2][]{\tikzexternaldisable\oldmissingfigure[{#1}]{#2}\tikzexternalenable}
    \LetLtxMacro{\oldtodo}{\todo}
    \renewcommand{\todo}[2][]{\tikzexternaldisable\oldtodo[#1]{#2}\tikzexternalenable}

    \usepackage[pdfusetitle,final]{hyperref}
    \hypersetup{
        % bookmarksopen=true
        hidelinks,
        % colorlinks,
        pdfinfo={
            % Author={Alessandro Cuttin},
            Subject={PhD Thesis},
            % Title={Title to be defined},
       }
    }

    \usepackage[open,openlevel=1]{bookmark}
    \usepackage[noabbrev,capitalise]{cleveref}
\end{filecontents*}

\begin{filecontents*}{other-commands.tex}
    \hyphenation{a-chieved mil-li-me-ter-wave na-no-sat-el-lite}
    \newcommand{\Rho}{\textrm{P}}
    \newcommand{\matlab}{Matlab\textsuperscript\textregistered\xspace}
    \newcommand{\logand}{$\wedge$\xspace}
    \newcommand{\logor}{$\vee$\xspace}
    \newcolumntype{m}{>{$}c <{$}}
    % adapted from https://tex.stackexchange.com/a/36003/177
    \usepackage{ifmtarg}% http://ctan.org/pkg/ifmtarg
    \makeatletter
    \newcommand{\algtoprule}{\hrule height.8pt depth0pt \kern2pt} % Caption top horizontal rule+skip
    \newcommand{\algmidrule}{\kern2pt\hrule\kern2pt} % Caption bottom (or mid) horizontal rule+skip
    \newcommand{\algbottomrule}{\kern2pt\hrule\relax}% Algorithm bottom rule
    \newcommand{\algcaption}[2][]{%
      \refstepcounter{algorithm}%
      \@ifmtarg{#1}
        {\addcontentsline{loa}{figure}{\protect\numberline{\thealgorithm}{\ignorespaces #2}}}
        {\addcontentsline{loa}{figure}{\protect\numberline{\thealgorithm}{\ignorespaces #1}}}%
      \algtoprule
      \textbf{\fname@algorithm~\thealgorithm}\ #2\par % Caption
      \algmidrule
    }
    \makeatother
    % https://tex.stackexchange.com/a/68310/177 and https://tex.stackexchange.com/a/10131/177
    \makeatletter
    % \let\runauthor\@author
    \let\runtitle\@title
    \makeatother
    \DeclareMathOperator\erf{erf}
    \DeclareMathOperator\erfc{erfc}
    \newtheorem{theorem}{Proposition}
    \newtheorem{corollary}{Corollary}[theorem]
\end{filecontents*}

\begin{filecontents*}{lit-rev.tex}
    \chapter{Modern random access methods}
    \label{sec:mod-ran-acc-met}
    \lipsum
    \begin{figure}[tb]
        \centering
        \subbottom[ \label{fig:aloha-max-thr}]{%
            \includegraphics[width=0.475\textwidth]{aloha-throughput.tikz}
        }
        \caption{The Aloha System: \subcaptionref{fig:aloha-pac-sch} schematic of the Aloha communication multiplexing \cite{Abramson1970}, and \subcaptionref{fig:aloha-max-thr} network throughput.}
        \label{fig:lit-rev-alo}
    \end{figure}
\end{filecontents*}

\begin{filecontents*}{aloha-throughput.tikz}
    \begin{tikzpicture}
        \begin{axis}[
                ymax = 0.2,
                ymin = 0,
                xmin = 0,
                xmax = 2,
                xlabel={$G$},
                ylabel={$S$},
                ytick distance = 0.1,
                grid                            = major
            ]
            % throughput upper bound of Aloha
            \addplot [
                domain=0:2,
                samples=40,
            ]
            {x * exp(-2*x};
        \end{axis}
    \end{tikzpicture}
\end{filecontents*}
\input{packages}
\input{other-commands}
% \input{set/tufte-like-plots}

% toggle the following lines to prevent tikz plots compilation every time - always use after loading pgfplots package
\usepgfplotslibrary{external}
\tikzexternalize

% memoir settings
\setsecnumdepth{subsection}
\settocdepth{subsection}
\newsubfloat{figure}% Create subfloat in figure environment

\author{myname}
\title{mytitle}

\loadglsentries{example-glossaries-brief.tex}
\makeglossaries
\glstoctrue

\ifdraftdoc
% special things for a draft document
    \usepackage{showkeys}
    \linespread{1.3}        % more linespread for showkeys
    \makeevenfoot{simple}{}{\texttt{Draft: \today}}{ }
    \makeoddfoot{simple}{}{\texttt{Draft: \today}}{}
    % \usepackage{showframe}
\else
% perhaps special things for a non-draft document
\fi

% \includeonly{con/annotations,con/lit-rev,con/introduction,con/jsac}
\begin{document}
\pagestyle{simple}
\frontmatter
\cleardoublepage
\tableofcontents
\glsunsetall
\cleardoublepage
\listoffigures
\cleardoublepage
\listoftables
\printglossaries
\glsresetall
\mainmatter
\part{Background}
\include{lit-rev}
\end{document}

It seems that externalized figures are not created. I suspect a package incompatibility, and will try to comment out the packages one by one.

  • 5
    It shouldn't be hard to make an MNWE if any figure at all causes the failure. What error do you get? I.e. exact message? This will direct you to a per-figure log file. What does that log say? Did you remember shell-escape? Does the appropriate directory hierarchy exist? – cfr Dec 27 '17 at 01:28
  • 4
    Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See minimal working example (MWE). – Henri Menke Dec 27 '17 at 02:07
  • @HenriMenke I can't make a MNWE right away, will do it asap. I know that without a M(N)WE it is harder to figure out an answer – Alessandro Cuttin Dec 27 '17 at 08:53
  • @cfr I posted the error I get in the first per-figure log file. Shell escape is ok, and directories are in place – Alessandro Cuttin Dec 27 '17 at 08:55
  • @AlessandroCuttin Then why did you post a question when you don't even know what to ask? – Henri Menke Dec 27 '17 at 08:59
  • 1
    @HenriMenke Are you going to answer the question ? – percusse Dec 27 '17 at 09:06
  • hi @HenriMenke, let me explain: according to Murphy's Law, the externalization stopped working very close to the document's delivery deadline. That's why I cannot provide you guys with a MWE right now. Anyway, I asked for direction on how to troubleshoot this. Seems a reasonable question, to me – Alessandro Cuttin Dec 27 '17 at 09:06
  • @percusse “my code doesn't work” is not a question. – Henri Menke Dec 27 '17 at 09:07
  • @AlessandroCuttin How to troubleshoot LaTeX: (1) Create a MWE. – Henri Menke Dec 27 '17 at 09:10
  • 1
    @HenriMenke You are not the judge of that. If you are not going to answer the question don't engage and possibly annoy people with unnecessary policing. – percusse Dec 27 '17 at 09:11
  • @AlessandroCuttin You need to take your \end{document} and place it randomly in your document. If compiles move it further, otherwise move it up until you find where it chokes. Get that bit and make an example out of it. You will probably solve it before you bring it over here. – percusse Dec 27 '17 at 09:13
  • @percusse I'd love to answer but can't without a MWE. – Henri Menke Dec 27 '17 at 09:13
  • thanks @percusse, it will probably happen as you say. Or I'll probably revert to a working version, and we'll never know. Anyway, I don't want to cause unnecessary arguments. If I still can't figure out what's going on in a while, I'll delete the question myself – Alessandro Cuttin Dec 27 '17 at 09:21
  • this may be related, though https://tex.stackexchange.com/q/57434/177 – Alessandro Cuttin Dec 27 '17 at 13:42
  • 2
    You asked what is the best way to troubleshoot. Well in my opinion the most important this is no to waste too much time with random searches and speculation. You can google for 30 minutes hoping to find the red button but then you should stop and switch to a systematic debugging. And the first step of systematic debugging in tex is creating an mwe. Beside this I agree with Henri that your question as it is can't be answered, the Runaway argument? is certainly a problem and should be corrected but it is unclear if is also the source of your other problem. – Ulrike Fischer Dec 27 '17 at 15:59
  • Each externalized image receives its own .log file. Do these log files exist? They contain the clue on why the externalization failed. If there are no such log files, you have to open your main .log file and search for any related warnings. Perhaps the -shell-escape feature is disabled or disallowed the operation. – Christian Feuersänger Dec 28 '17 at 11:23
  • added a MNWE; I double-checked my editor (sublime text), and -shell-escape is enabled in the settings: "build_arguments": ["-shell-escape"], – Alessandro Cuttin Dec 28 '17 at 20:58
  • I found the same issue on another question: https://tex.stackexchange.com/questions/114764/calc-and-settocdepth-break-tikzexternalize Even though a lot of effort was put in answering this question (thanks @UlrikeFischer) I found that the incompatibility of \tikzexternalize with the calc package and/or the \settocdepth command was the problem. – Alessandro Cuttin Feb 13 '18 at 18:27

2 Answers2

5

It is not a pgfplots problem and also not directly cause by pdflscape. Imho it is a problem of tikzscale.

tikzexternalshared.code.tex calls at the end in \AtBeginDocument (after tikzscale has added all its code) if it detects pdflscape this here:

\tikzset{
            external/.cd,
            optimize command away=\landscape,
            optimize command away=\endlandscape
        }%

The problem is that this code not only adds the new commands to the internal code of optimize command away but also reapplies older code and as \includegraphics is in the list it breaks.

This document reproduce the problem (\jobname-figure0.pdf shouldn't exist):

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{testinput.tikz}
 \begin{tikzpicture}
  \draw (0,0) rectangle (1,1);
 \end{tikzpicture}
\end{filecontents*}


\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize

\usepackage{tikzscale}


%breaks:
\AtBeginDocument{
        \tikzset{%           
           external/optimize command away=\blub,
        }%
        }%      

%  work around, uncomment to avoid the error:
% \tikzset{%
%         external/optimize/install/.code={},
%         }

\begin{document}

\includegraphics[width=0.475\textwidth]{testinput.tikz}

\end{document}

A work around is to disable the optimization or at least to remove \includegraphics from the list.

Ulrike Fischer
  • 327,261
2

Comment out \usepackage{pdflscape}. Sorry, I don't have an explanation.

Henri Menke
  • 109,596