# can glue be colored?

When I'm fine-tuning the design of my documents in LaTeX/XeLaTeX, it's sometimes helpful to use color to highlight the location and size of my fixed spacing. For example, I sometimes temporarily replace code like \hspace{2pt} with the code \textcolor{red}{\rule{2pt}{2pt}}} so I can very clearly see the 2pt space and, for example, make sure there are not any unintended spaces or extra spaces appearing on the left or right of my definite 2pt space.

But I have not been able to find a similar way in LaTeX/XeLaTeX of colorfully highlighting the stretchable glue that I use in my documents.

For example, \textcolor{red}{\hspace{6pt plus 2pt minus 1pt}} doesn't work. Also, \textcolor{red}{\rule{6pt plus 2pt minus 1pt}{2pt}} doesn't work.

How can I apply color to stretchable glue using TeX/LaTeX/XeLaTeX so it is easy to see it when I am fine-tuning my documents?

Is there maybe a way to color the background (i.e., the paper) so that stretchable glue will appear to be colored compared to the text before and after it?

Is there maybe a programmatic way of detecting the value of the stretchable glue as the document is being compiled and automatically replacing the stretchable glue with a \rule set to the same width as the stretchable glue?

• I think this has been done in LuaTeX. I highly recommend this blog post. – Ruixi Zhang Sep 14 '18 at 3:51
• Good article. My projects are almost always in XeLaTeX, so I am not sure I can implement that LuaTeX solution. I will investigate it further. Thanks. EDIT: That LuaTeX solution definitely requires LuaTeX and can't be done in XeLaTeX. I'm hopeful for a TeX/LaTeX solution. – Tim Stewart Sep 14 '18 at 4:13
• I might have an idea for \vspace which is easy to implement (using TikZ), but would need at least 2 runs to get it right. – Skillmon Sep 14 '18 at 8:38
• In an odd way, my answer here may be related: tex.stackexchange.com/questions/166499/… – Steven B. Segletes Sep 14 '18 at 10:11
• It would be better to move your questions 2 and 3 to another (possibly marked as follow-up) post. – user4686 Sep 14 '18 at 11:55

A solution working for \hspace (didn't look into \vspace yet, but it won't work the same way).

EDIT also a solution for \vspace but it will not always behave like a \vspace.

\documentclass[]{article}

\usepackage{xcolor}
\usepackage[normalem]{ulem}
\usepackage{tikz}

\makeatletter
\newcommand*\hlhspace{\@ifstar\hlhspace@star\hlhspace@nostar}
\newcommand*\hlhspace@nostar[1]{\hlhspace@out{\hspace{#1}}}
\newcommand*\hlhspace@star[1]{\hlhspace@out{\hspace*{#1}}}
\newcommand*\hlhspace@out[1]
{%
\begingroup
\color{red}%
\bgroup
\markoverwith{\rule[-.3ex]{0.2ex}{2ex}}%
\ULon{#1}%
\endgroup
}

\newcommand*\hlvspace{\@ifstar\hlvspace@out\hlvspace@out}
\newcommand*\hlvspace@out[1]
{%
\tikz[remember picture, overlay, baseline=(Begin.base)]
{%
\node[anchor=base, inner sep=0pt, outer sep=0pt] (Begin) {\strut};
}%
\vskip #1\relax
\tikz[remember picture, overlay, baseline=(End.base)]
{%
\node[anchor=base, inner sep=0pt, outer sep=0pt] (End) {\strut};
\draw[red,line width=2ex] (Begin.base) -- (End.base);
}
}
\makeatother

\begin{document}
\hbox to \textwidth{This text \hlhspace{5pt plus 5pt} is justified.}

Text
\hlvspace*{5pt plus 1fill}
More text.

\noindent
more text.
\end{document}


\documentclass{article}

\usepackage{color}

\begin{document}
justified.\newline And continues here.
\end{document}


Variant

\documentclass{article}

\usepackage{color}

\begin{document}
justified.\newline And continues here.
\end{document}


Extended demo as requested per comment.

\documentclass{article}

\usepackage{color}

\begin{document}
justified.\newline And continues here.

\newcommand\mycoloredglue{%
}

\def\x{x\mycoloredglue}

\def\y{\x\x\x\x\x\x\x\x\x\x}

\def\z{\y\y\y\y\y\y\y\y\y\y}

\z

\end{document}


The image clearly demonstrates stretch/shrink.

It is amusing that with 110 x rather than 100, the same number of lines is obtained but the x's are more squeezed.

\documentclass{article}

\usepackage{color}

\begin{document}
justified.\newline And continues here.

\newcommand\mycoloredglue{%
}

\def\x{x\mycoloredglue}

\def\y{\x\x\x\x\x\x\x\x\x\x}

\def\z{\y\y\y\y\y\y\y\y\y\y}

\z

\z\y

\end{document}


Do you see immediately that second paragraph as 10 additional x's compared to first paragraph?

To be completely honest there is ONE location where this is not exact replacement for a non-colored \hspace, as one can see in code above if using

\renewcommand\mycoloredglue{%
{\strut\hskip15pt plus 10pt minus 20pt\relax}%
}


I mean at end of paragraph because the TeX routine will do \unskip which removes the last skip.

However if you do the above \renewcommand and then issue in code above \z\strut or \z\y\strut you will see it matches exactly the colored output.

Try it.

So, except at end of paragraph, this does exactly the expected thing.

edit The above is not meant to say that my suggestion is really "colored glue". It is an Ersatz, and it is not glue for TeX: it will not go away via \unskip. But end of paragraph is not a problem per see, as one can see with this, inserted in above code

\def\x{x }


where the x's are at exactly identical locations.
• Does this work with something like 5pt plus 1pt minus 1pt? – Skillmon Sep 14 '18 at 12:54