# Using \color in varwidth environment produces warning and does not work as intended

Trying to use color in a varwidth environment, I stumbled upon a warning from the varwidth package. Inspecting the issue and trying the same with \parbox and the minipage environment lead to the following MWE:

\documentclass{article}

\usepackage{xcolor}
\usepackage{varwidth}

\begin{document}

\begin{tabular}{lll}
\verb|\parbox| & \verb|\minipage| & \verb|\varwidth| \\

%without color, everything works as expected.
\parbox[t]{4em}{
%  \color{red}
test
}abc &
\begin{minipage}[t]{4em}
%  \color{red}
test
\end{minipage}
abc &
\begin{varwidth}[t]{4em}
%  \color{red}
test
\end{varwidth}
abc\\

% but using \color needs \leavevmode and breaks varwidth
\parbox[t]{4em}{
\leavevmode
\color{red}
test
}abc &
\begin{minipage}[t]{4em}
\leavevmode
\color{red}
test
\end{minipage}
abc &
% throws error (twice):
% Package varwidth Warning: Failed to reprocess entire contents on input line 45.
\begin{varwidth}[t]{4em}
\leavevmode\color{red}
test
\end{varwidth}% this is line 45
abc
\end{tabular}

\end{document}


produces the following warning twice

Package varwidth Warning: Failed to reprocess entire contents on input line 45.

As you can see, you can use \color within \parboxes and minipages, if you insert a \leavevmode, but it breaks the varwidth environment with a warning. The behavior is similar to a \parbox without \leavevmode.

Is this (easily) fixable? Are there alternatives to varwidth?

To clarify what I want: I want to use colors in a varwidth environment that work as intended: placement as in the line without colors.

• As Henny Youngman used to say, don't do that. You could always run the data twice (environ package), once using \let\color=\@gobble and varwidth and save the width, then again using parbox using the computed width. (Actually, I think that is sort of how varwidth works.) – John Kormylo Nov 17 '18 at 19:07

You could use \textcolor:

\begin{varwidth}[t]{4em}
\textcolor{red}{test}
\end{varwidth}


\begin{varwidth}[t]{4em}
\leavevmode
{\color{red}
test}
\end{varwidth}


Example:

\documentclass{article}
\usepackage{xcolor}
\usepackage{varwidth}
\begin{document}
\begin{tabular}{lll}
\verb|\parbox| & \verb|\minipage| & \verb|\varwidth| \\

\parbox[t]{4em}{
test
}abc &
\begin{minipage}[t]{4em}
test
\end{minipage}
abc &
\begin{varwidth}[t]{4em}
test
\end{varwidth}
abc\\

\parbox[t]{4em}{
\textcolor{red}{test}
}abc &
\begin{minipage}[t]{4em}
\textcolor{red}{test}
\end{minipage}
abc &
\begin{varwidth}[t]{4em}
\textcolor{red}{test}
\end{varwidth}
abc
\\
\parbox[t]{4em}{
\leavevmode
\color{red}
test
}abc &
\begin{minipage}[t]{4em}
\leavevmode
\color{red}
test
\end{minipage}
abc &
\begin{varwidth}[t]{4em}
\leavevmode
{\color{red}
test}
\end{varwidth}
abc
\end{tabular}
\end{document}


Result:

• Oh, as easy as that, thanks :) would you mind explaining why this additional group does what it does? – nox Nov 21 '18 at 10:45

Starting a minipage or \parbox with \color always has the side effect of producing a blank line at the top. You can solve the issue by using

\begin{minipage}[t]{4em}
\leavevmode\color{red}% <--- don't forget
test
\end{minipage}


However this doesn't work for varwidth. You can instead start the coloring outside the varwidth environment:

\documentclass{article}

\usepackage{xcolor}
\usepackage{varwidth}

\newenvironment{colorvarwidth}[1]
{\leavevmode\color{#1}\varwidth}
{\endvarwidth}

\begin{document}

\begin{tabular}{lll}
\verb|\parbox| & \verb|\minipage| & \verb|\varwidth| \\

%without color, everything works as expected.
\parbox[t]{4em}{
\leavevmode\color{red}%
test
}abc &
\begin{minipage}[t]{4em}
\leavevmode\color{red}%
test
\end{minipage}
abc &
\begin{colorvarwidth}{red}[t]{4em}
test
\end{colorvarwidth}
abc
\end{tabular}

\end{document}