Typewriter in TikZ node

For some reason I can't really well understand, in a \node the parameters \spaceskip and \xspaceskip are set to non zero values; when \spaceskip is non zero, TeX uses it for the interword space instead of the default stored in the current font information.

Try the following example

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\tikz{\node[text width=3cm]{\ttfamily
totot\\
t t t\\
\the\spaceskip\ \the\xspaceskip\\
\spaceskip=0pt totot\\t t t};}
\end{document}

and see that setting \spaceskip=0pt cures the problem.

enter image description here

The option to set is text badly ragged (apparently TikZ doesn't like normal ragged right and adds to the LaTeX definition also the Plain TeX one when calling text ragged which is the default).

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\node[text width=1cm,font=\ttfamily,text badly ragged]{totot\\t t t};
\node[font=\ttfamily] at (1.5,0){\parbox{1cm}{totot\\t t t}};
\end{tikzpicture}
\end{document}

enter image description here

If an option for deciding about the \spaceskip doesn't exist in TikZ, it should.


In a parbox the text is justified and in a node with textwidth specified, the text is raggedright aligned. You can justify the text inside node.

\documentclass{standalone}
\usepackage{tikz}
\usepackage{ragged2e}
\begin{document}
\tikz{\node[text width=1cm]{\justifying\tt totot\\t t t};}
\tikz{\node{\parbox{1cm}{\tt totot\\t t t}};}
\end{document}

enter image description here

Hence it is not a bug IMO.

Not related, but you can consider using \ttfamily instead of two letter \tt.