# Why does text width affect space widths and how can I stop it?

What's making row 1 below different from the others? Space width is reduced. (This seems to be enabled by the text width=something style; if I remove it, the compression isn't there, but then I get centered text.) And is there any way to fix it but force the node width? (e.g. for drawing the node border)

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{}
\begin{document}
\begin{tikzpicture}
\tikzset{
bytesbox/.style={minimum width = 10cm, text width=10cm, align=left},
}

\node[bytesbox, anchor=west] at (0,1cm) {\tt 1 AB CD EF GH IJ KL};
\node[bytesbox, anchor=west] at (0,0.5cm) {\tt 2.AB.CD.EF.GH.IJ.KL};
\node[anchor=west] at (0,0) {\tt 3 AB CD EF GH IJ KL};
\node[anchor=west] at (0,-0.5) {\tt 4 AB.CD.EF.GH.IJ.KL};

\end{tikzpicture}
\end{document}


You need node font=\ttfamily (or font=\ttfamily), \tt or wrapping the text of the nodes in \texttt are (AFAIK) wrong. The pgfmanual v3.1.5b says on p. 233

And you need align=flush left. The pgfmanual v3.1.5b says on p. 236

\documentclass[border=4]{standalone}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\tikzset{
bytesbox/.style={minimum width = 10cm, text width=10cm, align=flush left},
}

\node[bytesbox, anchor=west,node font=\ttfamily] at (0,1cm) {1 AB CD EF GH IJ KL};
\node[bytesbox, anchor=west,node font=\ttfamily] at (0,0.5cm) {2.AB.CD.EF.GH.IJ.KL};
\node[anchor=west,node font=\ttfamily] at (0,0) {3 AB CD EF GH IJ KL};
\node[anchor=west,node font=\ttfamily] at (0,-0.5) {4 AB.CD.EF.GH.IJ.KL};

\end{tikzpicture}

\end{document}


Of course, you can add the font information to the bytesbox style.

\tikzset{
bytesbox/.style={minimum width = 10cm, text width=10cm, align=flush left,node font=\ttfamily},
}

• Well, flush left essentially does text badly ragged. The font= option can be useful, but it's not necessary as we're not interested in the em value that TikZ uses to set \spaceskip, as we don't want it to. – egreg Feb 25 at 22:48
• what's the difference between flush left and text badly ragged ? I see your excerpt from pgfmanual on flush left... from a typesetting standpoint I know what flush left means, I can see what \raggedright is I guess... text badly ragged is badly named though. – Jason S Feb 25 at 22:54
• @egreg Well, more than 80% of the questions of the type "why is the spacing of my node off" can be answered by "because you did not use node font (or font)", so there is really no reason to propagate unfortunate practice further. Of course, you can always get the result in other ways as suggested by the manual, but if you follow the manual you are fine. – Schrödinger's cat Feb 25 at 22:59
• @JasonS It may just be me but I fail to see text badly ragged in the manual, so I cannot comment on that one. All I can say is that, according to what I find, if one follows the manual, there is no problem. (There are other cases in which the manual is not quite correct, but here it seems to be great.) – Schrödinger's cat Feb 25 at 23:06

It's TikZ that tampers with \spaceskip in text boxes.

You can solve this with text badly ragged. Please, avoid \tt that has been deprecated for 25 years.

\documentclass[border=4]{standalone}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\tikzset{
bytesbox/.style={minimum width = 10cm, text width=10cm, align=left,text badly ragged},
}

\node[bytesbox, anchor=west] at (0,1cm) {\texttt{1 AB CD EF GH IJ KL}};
\node[bytesbox, anchor=west] at (0,0.5cm) {\texttt{2.AB.CD.EF.GH.IJ.KL}};
\node[anchor=west] at (0,0) {\texttt{3 AB CD EF GH IJ KL}};
\node[anchor=west] at (0,-0.5) {\texttt{4 AB.CD.EF.GH.IJ.KL}};

\end{tikzpicture}

\end{document}


• " Please, avoid \tt that has been deprecated for 25 years." -- how are we supposed to learn about these things?! – Jason S Feb 25 at 21:29
• and what does text badly ragged actually do? just not touch the \spaceskip? – Jason S Feb 25 at 21:30
• @JasonS It does \raggedright, which is good for you. – egreg Feb 25 at 21:31
• @JasonS I did comment on your previous question that \tt was not defined by default:-) – David Carlisle Feb 25 at 21:34

\spaceskip=0.5em seems to allow me to fix it. (not sure why it's 0.5em rather than 1em)

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{}
\begin{document}
\begin{tikzpicture}
\tikzset{
bytesbox/.style={minimum width = 10cm, text width=10cm, align=left},
bytesbox2/.style={minimum width = 10cm, text width=10cm, font={\ttfamily}, align=left, execute at begin node={\spaceskip 0.5em}},
}
% phantom space
\def\psp{\phantom{0}}

\node[bytesbox, anchor=west] at (0,1cm) {\tt\spaceskip=0.5em 1 AB CD EF GH IJ KL};
\node[bytesbox, anchor=west] at (0,0.5cm) {\tt 2.AB.CD.EF.GH.IJ.KL};
\node[anchor=west] at (0,0) {\tt 3 AB CD EF GH IJ KL};
\node[anchor=west] at (0,-0.5) {\tt 4 AB.CD.EF.GH.IJ.KL};
\node[bytesbox2, anchor=west] at (0,-1cm) {5 AB CD EF GH IJ KL};

\end{tikzpicture}
\end{document}


• The default monospace font has the same character width as a digit, which is half the width of the Computer Modern em-width. (In other words, that's a design decision.) – barbara beeton Feb 25 at 21:34
• @barbarabeeton which is true but it means that the width of an M is half an em, which is ... unusual. – David Carlisle Feb 25 at 21:35
• ... Half the width of a Computer Modern Roman em-width. There's probably a comment in the Metafont recipe in Volume E. – barbara beeton Feb 25 at 21:39
• @barbarabeeton not just cm roman though, em is font-specific so a cmtt M is half a cmtt em wide. – David Carlisle Feb 25 at 21:45
• @DavidCarlisle -- What I'm trying to say, though obviously not very well, is that the cmtt em width is the same as that of the cmr em. A design decision. (I haven't explored, but it would be interesting to learn what the em width is for some non-cm monowidth fonts, say Courier or the Lucida mono.) – barbara beeton Feb 25 at 22:08