# pdfpages-PDF appears in background of pgfplots-plot

I'm using pgfplots with the externalize feature, which creates external pdfs for all my plots. But there seems to be a strange interaction with pdfpages when externalize is used. The pdf I'm including appears in the backgrounds of my plots: There is no error message when typesetting the whole ordeal.

I include pgfplots with

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

\pgfplotstableset{col sep=comma}
\usepgfplotslibrary{units}
\usepgfplotslibrary{external}
\tikzexternalize[optimize=false, prefix=external/, optimize command away=\includepdf]


You can see, I already tried something with optimize command away=\includepdf but to no avail.

Pdfpages simply by

\usepackage{pdfpages}


The titlepage:

\begin{document}
\includepdf[pagecommand={\thispagestyle{empty}},offset=0cm 0cm]{TitlePageMasterarbeitIME.pdf}


I couldn't find an answer to this by googling so maybe one of you has experienced something similiar and knows a way to work around this problem.

Edit: MWE: My PDF file here

\documentclass[DIV12,a4paper,headsepline,titlepage,ngerman]{scrreprt}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{pdfpages}

\usepackage{tikz}
\usepackage{pgfplots}

\usepgfplotslibrary{external}
\tikzexternalize[optimize=false, prefix=external/, optimize command away=\includepdf]

\begin{document}
\includepdf[pagecommand={\thispagestyle{empty}},offset=0cm 0cm]{TitlePageMasterarbeitIME.pdf}
\begin{figure}[htb]
\centering
\begin{tikzpicture}
\begin{axis}[
name=VentileMassenstromAusschnitt,
width=0.87\textwidth, % Scale the plot to \linewidth
minor y tick num = 1,
minor x tick num = 1,
scale only axis,
grid=both,
grid style={dashed,gray!30},
]
(5,5)};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

• Please provide a fully compilable minimal example that the rest of us can copy and test. When people have to add stuff to make it compile, they are less likely to help – daleif Jul 3 '15 at 13:51
• Hey, thanks for your input. I try to create one that shows the problem. – UsYer Jul 3 '15 at 14:09
• Very curious: it seems that \includepdf remembers forever to add the title page. – egreg Jul 3 '15 at 17:39
• yeah, it seems as if it may be a problem in pdlatex itself, which would make fixing it harder... – UsYer Jul 4 '15 at 8:29
• I've encountered exactly the same problem, and the solution given here has worked, but only when I've put \tikzexternalenable before, and \tikzexternaldisable after the \includepdf command. – MarcinKonowalczyk May 26 '16 at 16:25

It seems that the title page made its way into the externalized figure (I opened it and saw it there).

In order to solve it, we have to disable the title page during the image externalization.

In fact, changing optimize=false to optimize=true does this -- and the pdf appears to be fine. If there is a particular reason for optimize=false? This key actually disables your carefully placed optimize command away=\includepdf. If you really need optimize=false, you could still use \tikzifexternalizing{<true case>}{<false case>} in order to suppress the title page.

\documentclass[DIV12,a4paper,headsepline,titlepage,ngerman]{scrreprt}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{pdfpages}

\usepackage{tikz}
\usepackage{pgfplots}

\usepgfplotslibrary{external}
\tikzexternalize[optimize=true, optimize command away=\includepdf]

\begin{document}
\includepdf[pagecommand={\thispagestyle{empty}},offset=0cm 0cm]{TitlePageMasterarbeitIME.pdf}
\begin{figure}[htb]
\centering
\begin{tikzpicture}
\begin{axis}[
name=VentileMassenstromAusschnitt,
width=0.87\textwidth, % Scale the plot to \linewidth
minor y tick num = 1,
minor x tick num = 1,
scale only axis,
grid=both,
grid style={dashed,gray!30},
]