2

I'm using the pbox package's \settominwidth command to measure the typeset width of text that includes explicit line breaks (which is a case in which standard LaTeX's settowidth doesn't work).

In the following MWE, I add a brace group to localize the effect of a color switch (\color{red}). Adding the brace group causes an error when that command is passed to \settominwidth: Missing } inserted.

I'd like to know what's illegitimate about adding the brace group. (Note that the color switch without the added brace group, in \textA, doesn't raise a complaint.)

I'm also curious why \settominwidth reports about 2pt more width for \widthB than for \widthA, but that's not a mandatory part of the solution.

I realize that, in this limited MWE, I could avoid the issue by using \textcolor{red}{someText} instead, but this MWE is abstracted from a larger context in which a switch is preferable.

Stack Exchange suggested as a "similar question": "Why can't I use braces in an \if… \fi group?". Perhaps it holds the key, but I don't know enough about \settominwidth to know.

\documentclass{article}
\usepackage{pbox}
\usepackage{xcolor}
\setlength{\parindent}{0pt}
\newlength{\widthA}
\newlength{\widthB}
\newcommand{\textA}{\color{blue} One \\ Two}
\newcommand{\textB}{{\color{red} One \\ Two}}

\settominwidth{\widthA}{\textA}
\settominwidth{\widthB}{\textB}

\begin{document}
\texttt{\textbackslash{}widthA}: \the\widthA\\
\texttt{\textbackslash{}widthB}: \the\widthB

\textA

\textB

After text
\end{document}

enter image description here

Jim Ratliff
  • 1,049

1 Answers1

5

\settominwidth uses internally a tabular. This means that you are doing

\begin{tabular}{@{}l@{}}
{\color{red} One \\ Two}
\end{tabular}

And this doesn't work as cell are groups.

Ulrike Fischer
  • 327,261
  • what would be the general formulation of what you can't do in the argument of \settominwidth? Is it just that you can't encapsulate the entire argument in a brace group? Or is it more general than that? – Jim Ratliff Dec 23 '18 at 20:30