# How is text indented and glued within TikZ node contents?

This is my MWE:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[draw=red,very thin, align=left,every node/.style={draw,rectangle}]
\node (unaligned) {
\ttfamily
parameters: a\\
\phantom{parameters:} b};

\node[node font=\ttfamily, right=of unaligned] {
parameters: a\\
\phantom{parameters:} b};

\end{tikzpicture}
\end{document}


The difference is obvious, a and b are not aligned in the first node. Apparently, the \phantom{...} is not typeset using the \ttfamily, hence it's size is different. I could have used the incantation font=\ttfamily, which would work just as fine as node font=\ttfamily.

The PGF manual only mentions this a bit vaguely:

Naturally, you can just use a font command like \small or \rm at the beginning of a node. However, the following two options make it easier to set the font used in nodes on a general basis.

and

The keyword node is typically followed by some options and then some text between curly braces. This text is put inside a normal TEX box (if the node specification directly follows a coordinate, which is usually the case, TikZ is able to perform some magic so that it is even possible to use verbatim text inside the boxes)

Apparently I cannot "just use a font command". Why? Is this connected to the aforementioned "magic" in any way? Also, what is a "normal TeX box"? I am aware of \parbox, \mbox and \minipage, and they all behave a little bit differently with respect to indentation and similar things.

• To my knowledge this is one of the main reasons why one should use font or node font. (The first scenario can also be achieved with \node[execute at begin node=\ttfamily,....)
– user194703
Nov 20, 2019 at 3:34

That's because if you omit text width TikZ will try to figure out the text width for you by splitting lines at \\ and packing them into an \halign. That is also documented under /tikz/align in the PGF manual.

Essentially these two are equivalent:

\documentclass{article}
\pagestyle{empty}
\usepackage{tikz}
\begin{document}
\noindent \tikz \node[align=left,inner sep=0pt] {
\ttfamily parameters: a\\
\phantom{parameters:} b
};

\noindent \halign{#\hfil\cr
\hbox{\ttfamily parameters: a}\cr
\hbox{\phantom{parameters:} b}\cr
}
\end{document}