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?

  • 3
    I think this has been done in LuaTeX. I highly recommend this blog post. – Ruixi Zhang Sep 14 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 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 at 8:38
  • 1
    In an odd way, my answer here may be related: tex.stackexchange.com/questions/166499/… – Steven B. Segletes Sep 14 at 10:11
  • It would be better to move your questions 2 and 3 to another (possibly marked as follow-up) post. – jfbu Sep 14 at 11:55
up vote 10 down vote accepted

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}

enter image description here

\documentclass{article}

\usepackage{color}

\begin{document}
\noindent This text {\color{red}\leaders\vrule\hfill} is
justified.\newline And continues here.
\end{document}

enter image description here

Variant

\documentclass{article}

\usepackage{color}

\begin{document}
\noindent This text {\color{red}\strut\leaders\vrule\hfill} is
justified.\newline And continues here.
\end{document}

enter image description here


Extended demo as requested per comment.

\documentclass{article}

\usepackage{color}

\begin{document}
\noindent This text {\color{red}\strut\leaders\vrule\hfill} is
justified.\newline And continues here.

\newcommand\mycoloredglue{%
   {\color{red}\strut\leaders\vrule\hskip15pt plus 10pt minus 20pt\relax}%
}

\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.

enter image description here

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}
\noindent This text {\color{red}\strut\leaders\vrule\hfill} is
justified.\newline And continues here.

\newcommand\mycoloredglue{%
   {\color{red}\strut\leaders\vrule\hskip15pt plus 10pt minus 20pt\relax}%
}

\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}

enter image description here

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 }

\z\x\x\x\x\x\x\x\x\x\x\x\x{\color{red}\strut\leaders\vrule\hfill}

\z\x\x\x\x\x\x\x\x\x\x\x\x

which produces

enter image description here

where the x's are at exactly identical locations.

  • 1
    Does this work with something like 5pt plus 1pt minus 1pt? – Skillmon Sep 14 at 12:54
  • 1
    @Skillmon of course it does. – jfbu Sep 14 at 13:37

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.