# TikZ/external: LaTeX Error: Missing \begin{document}

Why do I get this error of missing \begin{document} in the log file therect.log of the externally built image threct.pdf?

P.S. I am sorry for not fully cleaning the code of repeated package loading and unnecessary packages.


\PassOptionsToPackage{table}{xcolor}

\begin{filecontents*}[overwrite]{rect.tikz}
\begin{tikzpicture}[x=1em, y=1em,blue]
\draw (0,0) rectangle node{Diaa} (4,4);
\end{tikzpicture}
\end{filecontents*}

\documentclass[
]{exam}

\usepackage{catchfile,lipsum,graphbox,array,ragged2e,booktabs,fmtcount,multicol,multirow,calc,tikz}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\makeatletter
\def\tikz@external@watch@store@aux#1#2{%
\immediate\write\@mainaux{\string\expandafter\xdef\noexpand\csname pgfk@/tikz/external/watch/stored/#1\string\endcsname{#2}}}%
\def\tikz@external@watch@get@from@aux#1#2{%
\ifcsname pgfk@/tikz/external/watch/stored/#1\endcsname
\edef#2{\csname pgfk@/tikz/external/watch/stored/#1\endcsname}%
\else
\edef#2{0}%
\fi}
\tikzset{external/watch/.cd,store/.code 2 args={%
\tikz@external@watch@store@aux{#1}{#2}},
get/.code 2 args={\tikz@external@watch@get@from@aux{#1}{#2}},
font size/.code={\tikzset{external/watch/get={font size}{\pgfutil@tempa},
external/watch/store={font size}{\f@size}}%
\unless\ifnum\pgfutil@tempa=\f@size
\tikzset{/tikz/external/force remake}%
\fi
}}
\makeatother

\usetikzlibrary{external}
% https://tex.stackexchange.com/a/482560/2288
% https://tex.stackexchange.com/q/576077/2288
\NewDocumentCommand{\inputtikz}{ m m }{%
\IfFileExists{#1}{%
\tikzset{external/export=true}%
\tikzsetnextfilename{#2}%
\input{#1}%
\tikzset{external/export=false}%
}{% https://tex.stackexchange.com/a/383190/2288
\typeout{Error: Missing file: #1}
\ExitDueToUndefinedControlSequence%
}%
}

\usepackage[american,siunitx]{circuitikz}
\usetikzlibrary{arrows, shapes, calc, positioning, patterns, decorations, decorations.markings, quotes, fpu, patterns, decorations.pathmorphing}

% for DC Motor Circuit Drawing
\tikzset{rotarrow/.pic={
\draw[thin,->] (-0.2,-0.2)  to [out=-60,in=60, looseness=4] ++(0,0.4) node [above=1mm] {\tikzpictext};
},
}

\usepackage[xparse,breakable,raster,skins]{tcolorbox}% xparse,breakable
\tcbset{shield externalize}
\tcbuselibrary{fitting}

%https://tex.stackexchange.com/a/331068/2288
\newcommand\blankpage{%
\clearpage
\begingroup
\null
\thispagestyle{empty}%
\hypersetup{pageanchor=false}%
\clearpage
\endgroup
}

\setlength{\parindent}{0cm}

\tikzexternalize
\begin{document}
\tikzset{external/.cd,
watch/font size,
export = false,
}

\inputtikz{rect.tikz}{therect}

\end{document}

• You are loading in your example more than 40 packages, and most of them are probably unrelated. Dec 23, 2020 at 13:55
• @UlrikeFischer When I reduced my document before, I couldn't manage to get the error reproduced. So, I started with this messy version hoping to reduce it properly without losing the error.
– Diaa
Dec 23, 2020 at 14:15
• @UlrikeFischer Please, check my updated version.
– Diaa
Dec 23, 2020 at 14:24
• sorry it is not reasonable to post that much obscure code and ask people to run with --shell-escape shell escape is disabled for a reason. (that said if someone does trust you enough to run the posted example, it runs without error) Dec 23, 2020 at 14:42
• actually I think it is worth answering as I have to admit the error message here is completely impossible to understand the guess of trying it without a space in the name comes from experience not anything in the log. Others may be helped to see that, you could self answer. Dec 23, 2020 at 15:08

Furthermore, when dealing with the external library of tikz spaces are so notorious and explosive (when used in the names of main file,\tikzsetnextfilename, *.tikz file) and give completely incomprehensible errors as the one in my question.