can glue be colored?

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


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

Tags:

Color

Glue