# Adjusting minipage's width to avoid line breaks

For foo and baz, widthof return sensible/nonsensible values. When the width of the minipage wrapped around foo and baz is set to these numbers, neither has the desired width, that is, the width of the longest line contained within. In other words, I'm looking to set minipage's width such that no line breaks.

\documentclass[12pt]{report}
\usepackage{calc}
\usepackage{mwe}
\usepackage{xparse}
\usepackage{zebra-goodies}

\newsavebox\foo
\sbox{\foo}{%
% 3456789
abcdefgh
\\abc%
}

\NewDocumentCommand{\baz}{}
{%
% 3456789
abcdefgh\\% "longest line"
abc
}

\begin{document}

\noindent \texttt{foo}

% https://tex.stackexchange.com/a/37294/112708
\begin{tabular}{ll}
w & \widthof\foo \\
h & \heightof\foo \\
d & \depthof\foo
\end{tabular}

\frame{\begin{minipage}{\widthof{\foo}}
\usebox{\foo}
\end{minipage}}

{\tiny\todo{adjust mp's width to fit exactly longest line}}

\noindent \texttt{baz}

\begin{tabular}{ll}
w & \widthof\baz \\
h & \heightof\baz \\
d & \depthof\baz
\end{tabular}

\frame{\begin{minipage}{\widthof{\baz}}
\baz
\end{minipage}}

{\tiny\todo{adjust mp's width to fit exactly longest line}}

\end{document}


• \frame{\begin{minipage}{\wd\foo} You want the width of the box and not the width of '\foo', right? For \baz put it in a box if you want to measure it. – cfr Oct 24 '19 at 3:33
• Surely I have not understood the problem, but a minipage of the width of longest sentence without allow line breaks is a simple tabular environment, i.e., \frame{\begin{tabular}{l} \baz \end{tabular}} – Fran Oct 24 '19 at 3:41
• I want the minipage to contain abcdefgh\\abc and have the width of abcdefgh since its the longest. I have the intuition boxes comes into play, just not sure how. – Erwann Oct 24 '19 at 3:54

As a first debugging step, you should look at what's printed with \usebox{\foo}, namely

Why's that? Because \\ essentially does \hfill\break which has no effect whatsoever when building a horizontal box.

If you want to set a length to the widest word in a list, you can use a tabular:

\newcommand{\settowidest}[2]{% items must be separated by \\
\settowidth{#1}{\begin{tabular}{@{}l@{}}#2\end{tabular}}%
}


Example:

\documentclass[12pt]{report}

\newcommand{\settowidest}[2]{% items must be separated by \\
\settowidth{#1}{\begin{tabular}{@{}l@{}}#2\end{tabular}}%
}

\newcommand{\foo}{%
% 3456789
abcdefgh\\% "longest line"
abc%
}

\newlength{\foolen}
\AtBeginDocument{%
\settowidest{\foolen}{\foo}%
}

\begin{document}

\fbox{\begin{minipage}{\foolen}\foo\end{minipage}}

\end{document}


On the other hand, you can avoid all measuring by directly using a tabular just like in the definition of \settowidest. Or you can use varwidth (look for it on the site).