3

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}

MWE result

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
3

You could use \textcolor:

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

or an additional group:

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

enter image description here

| improve this answer | |
  • 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
1

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}

enter image description here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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