Yesterday I found my old thesis and found out that there is a new version of minted (2.0+) which somehow don't want to work with my old thesis since all the listings were not highlighted.

Unfortunately there is in my opinion a problem which depends on the location of the \tikzexternalize command. If this command is located before \usepackage{minted} my listings won't get highlighted at all. Putting \usepackage{minted} before the \tikzexternalize works though. Using the old \usepackage{minted1} also works fine. So it seems to be a problem with minted 2.0+ only.

Here is a MWE for the problem (see commented lines):

\documentclass[11pt,a4paper]{article}
\usepackage[T1]{fontenc}

\usepackage{pgf,tikz}
\usetikzlibrary{external}
\tikzset{external/system call={pdflatex --shell-escape -halt-on-error -interaction=batchmode -jobname "\image" "\texsource"}}

%\usepackage{minted}

\tikzexternalize

\usepackage{minted}

\begin{document}

\begin{tikzpicture}
\draw (0,0) --(1,2) -- (2,3) -- (1,0);
\end{tikzpicture}

\begin{minted}[frame=none,fontsize=\small,linenos=true]{c}
int main(int argc, char*argv[])
{
(void) printf("Hello World\n");

return EXIT_SUCCESS;
}
\end{minted}

\end{document}


So my question: Is this behaviour normal or intended?

Some information about the setup I am using:

MikTeX version:   miktex-2.9.5721
Minted version:   2015/09/09 v2.1 Yet another Pygments shim for LaTeX
Python version:   Python 3.5.0
Pygments version: Pygments version 2.0.2, (c) 2006-2014 by Georg Brandl.


And for building my thesis I use (under windows 8)

pdflatex --shell-escape --enable-write18 mwe

• I narrowed the issue down to this addition of minted: github.com/gpoore/minted/issues/73. If I comment this lines for detecting the tikzexternalize job everything works again. The \tikzexternalrealjob is probably set directly after \tikzexternalize and not only when the figures are externalized? – patrickvogt Oct 1 '15 at 10:52
• I think you should file this as a bug report for minted. – Konrad Rudolph Oct 1 '15 at 17:48
• Geoffrey already reopened the above issue and will have a look at it. A workaround is to set \tikzexternalize as last command in the preamble. I also tried to find a permanent fix by not just looking if \tikzexternalrealjob is defined but also look into its content. But I wasn't successful yet. I think at least it is good that this problem can be found by using google now very easy. Unfortunately it took me one to two hours to find the cause for the non-highlighting problem in my two year old thesis template. – patrickvogt Oct 2 '15 at 21:40
• – patrickvogt Oct 2 '15 at 23:23

I think I found a fix for the issue:

Simply exchange (in the minted.sty-file)

\ifcsname tikzexternalrealjob\endcsname
\minted@drafttrue
\minted@cachefalse
\else
\fi


with:

\ifcsname tikzifexternalizing\endcsname
% if \tikzexternalize is set before \usepackage{minted} this branch is used
% the true branch of the \tikzifexternalizing will only be used if the tikz externalizing job is running at the moment
\tikzifexternalizing{\minted@drafttrue\minted@cachefalse}{}
\else
% else this old code will be used which will definitely work in all other cases (when \tikzexternalize is set after \usepackage{minted})
\ifcsname tikzexternalrealjob\endcsname
\minted@drafttrue
\minted@cachefalse
\else
\fi
\fi


\tikzifexternalizing will only process the true-branch if the externalization job runs (according to the package documentation).

Maybe a fix can be achived by a more beautiful and more elegant coding, but it does the trick for me at the moment.