Background package with tikz externalization

I have a follow up question of the thumb index presented in Show current chapter number on each page margin. When I use it alone it works ok. However, when I use it in combination with tikz externalization, the externalization routine starts by generating one empty pdf file per page on the document and ends with the error

Package tikz Error: Sorry, the system call 'pdflatex -shell-escape -halt-on-e.

You can see in the MWE below that I have added \tikzexternalizedisable in several places but it does not seem to work.
Any idea what am I doing wrong?

%%MEW
% !TeX encoding = UTF-8
\documentclass[b5paper]{book}
\usepackage[english]{babel}
\usepackage{graphicx}       % Graphics
\usepackage{lipsum}

\usepackage{tikz}
\usetikzlibrary{external}
\usepackage{pgfplots}
\usepgfplotslibrary{external}

%

%% THUMB INDEX
\usepackage{tikzpagenodes}
\usepackage{totcount}
\usepackage[contents={},opacity=1,scale=1,color=white]{background}

\newif\ifMaterial

\newlength\LabelSize
\setlength\LabelSize{1cm}

\AtBeginDocument{%
\regtotcounter{chapter}%
}
\makeatletter
\tikzexternaldisable
\Materialtrue
\tikzexternaldisable
\ifMaterial
\ifodd\value{page}%
\tikzexternaldisable
\backgroundsetup{
angle=0,
position={current page.east|-current page text area.north east},
vshift=-45-(\thechapter-1)*90pt,
hshift=0*\LabelSize/2,
contents={%
\tikz\node[ch label, inner xsep=\LabelSize/2] {\hspace*{-\LabelSize}\thechapter};
}%
}%
\tikzexternalenable
\else
\tikzexternaldisable
\backgroundsetup{
angle=0,
position={current page.west|-current page text area.north west},
vshift=-45-(\thechapter-1)*90pt,
hshift=0*\LabelSize/2,
contents={%
\tikz\node[ch label, inner xsep=\LabelSize/2] {\thechapter\hspace*{-\LabelSize}\null}; %
}%
\tikzexternalenable
}%
\fi
\BgMaterial
\else\relax\fi
}%
}
\newcommand\RemoveLabels{\Materialfalse}
\tikzexternalenable
\makeatother

\tikzset{
ch label/.style={fill=black,anchor=west,text width=\LabelSize, align=center,minimum height=\LabelSize,minimum width=\LabelSize*2,inner sep=0em,text=white,font=\sffamily\fontsize{15pt}{0pt}\selectfont}
}

%\tikzexternalize[prefix=FiguresTikz/] % uncomment to cause the error

\begin{document}
\tikzexternaldisable
\tikzexternalenable
\chapter{Test 1}
\lipsum[1]
\begin{figure}
\tikzsetnextfilename{test_figure}
\centering
\begin{tikzpicture}[draw=black,scale=1, trim left]
\draw[fill=blue!50] (0,0) rectangle (2, 1) node[midway,font=\tiny,align=center] at (0,0) {test figure};
\end{tikzpicture}
\caption{test figure}
\end{figure}
%
%
\chapter{Test 2}
\lipsum[2]
\end{document}


You can set the external/only named key, e.g. by changing \tikzexternalize[prefix=FiguresTikz/] to \tikzexternalize[prefix=FiguresTikz/,only named] to only externalize pictures that have explicitly been named.

• Thanks. With the suggested changes in the MWE it does not generate the pdf of the dummy figure. However, if as you suggested I add this "\tikzexternalize[prefix=FiguresTikz/,only named]" it works like charm now. I am going to try to integrate it with my document – Robinson Medina Aug 21 '18 at 9:23
• @RobinsonMedina I can't seem to get it to work with the \tikzset{external/export=false} key. I will have to limit my answer to the only named key :( – Max Aug 21 '18 at 9:41