# Text doesn't show in boxes shipped-out from inside the TikZ source code

I'm trying to understand a certain aspect of the TikZ language by inspecting its source code. The code that I'm interested in is in the file <tex installation directory>/tex/generic/pgf/frontendlayer/tikz/tikz.code.tex.

For debugging purposes, I would like to export some Tex boxes that are created by the code. In light of the answer to this question, I've sprinkled a dash of \shipout commands throughout the code. I've added auxiliary text inside the boxes to help with debugging. However, the additional text doesn't show on the page.

Consider the following simplified example. Write \shipout\hbox{Hello, world!} as the first line inside the \tikz@finish macro. This is the macro that renders the picture on the page, so it is always called. Now create a new file with the following simple TikZ picture of a straight line:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\tikz \draw (0,0) --(1,0);
\end{document}


The resulting pdf file does not contain the phrase 'Hello, world!' anywhere!

• TikZ resets the font to \nullfont. Try adding \normalfont inside the \hbox. – Heiko Oberdiek Aug 5 '17 at 12:56
• @HeikoOberdiek: Like this: \shipout\hbox{\normalfont Hello, world!}? It doesn't work. – Evan Aad Aug 5 '17 at 14:20
• @HeikoOberdiek: However, this works: \shipout\hbox{\font\myfont=ccr9\myfont Hello, world!}. Thanks! Are the effects of the font change limited to the enclosing TeX scope? I don't want this to affect the surrounding TikZ code. – Evan Aad Aug 5 '17 at 14:36
• @EvanAad there is no need to use a font declaration of that form, just \normalfont would have been Ok, but font setting are always local in tex (although the font declaration \font\myfont you used there has some global effects as fonts are only loaded once – David Carlisle Aug 5 '17 at 16:41

TikZ resets the font to \nullfont inside its environment. Also it redefines \selectfont to choose \nullfont. Therefore, the font needs to be restored, if you want to output a box inside environment tikzpicture and friends.

The environment pgfinterruptpicture can be used as wrapper of the box contents to restore the font:

\documentclass{article}
\usepackage{tikz}
\usepackage{etoolbox}
\makeatletter
\pretocmd\tikz@finish{%
\shipout\hbox{%
\begin{pgfinterruptpicture}%
Hello, world!%
\end{pgfinterruptpicture}%
}%
}
\makeatother

\begin{document}
\tikz \draw (0,0) --(1,0);
\end{document}


First page: