Question
Is it possible to get the width of a multi-paragraph piece of text? For example, if I want to get the width of a single piece of text, I can do the following:
\documentclass[a4paper]{article}
\begin{document}
\newlength{\mytextwidth}
\settowidth{\mytextwidth}{some text}
\end{document}
However, if that text has multiple paragraphs, that doesn't work. E.g. If I do something like the following I get the error Paragraph ended before \@settodim was complete. <to be read again>:
\documentclass[a4paper]{article}
\begin{document}
\newlength{\testlen}
\settowidth{\testlen}{
some text
some longer text
}
\end{document}
What I'm wondering is if there is a way to get the widest width of text content (even if it's multi-paragraph text). So, for the first example it would still give the length of some text and for the second example, it would give the length of some longer text.
Context
Above is what I think I need for what I'm trying to do, but in case it's helpful, I'll also add the context for why I'm interested in getting this.
I'm attempting to write a command that will take in text content for a left side and right side. The content will be split into two columns, with the left side content being flush left and the right side content being flush right. The columns will adjust themselves to fit as much text on a line as they can before wrapping to the next line.
So if the left side content is much longer than the right side content, the right column will be much shorter in width than the left column and vice-versa. If neither side's content requires half the line width, each column's width will simply be half the line width. Similarly, if both side's content requires at least half the line width, each column's width will just be half the line width.
Here's what I have, which is working for single paragraph text:
\documentclass[a4paper]{article}
\begin{document}
% length variables for the command below
\newlength{\leftwidth}
\newlength{\rightwidth}
\newlength{\halfline}
\newlength{\leftexcess}
\newlength{\rightexcess}
% arg1: flush left minipage content
% arg2: flush right minipage content
\newcommand{\mycmd}[2]{
\settowidth{\leftwidth}{#1}
\settowidth{\rightwidth}{#2}
\setlength{\halfline}{0.5\linewidth}
% if the left side content doesn't need at least half of the
% line width, store the excess leftover space
\ifdim\leftwidth<\halfline\relax
\setlength{\leftexcess}{\dimexpr(\halfline-\leftwidth)}
\else
\setlength{\leftexcess}{0pt}
\fi
% if the right side content doesn't need at least half of the
% line width, store the excess leftover space
\ifdim\rightwidth<\halfline\relax
\setlength{\rightexcess}{\dimexpr(\halfline-\rightwidth)}
\else
\setlength{\rightexcess}{0pt}
\fi
% neither side has any leftover space, so each column just gets half the line
\ifdim\rightexcess=0pt\relax
\ifdim\leftexcess=0pt\relax
\setlength{\leftwidth}{\halfline}
\setlength{\rightwidth}{\halfline}
\fi
\fi
% neither side needs any extra space to fit, so each column just gets half the line
\ifdim\leftexcess>0pt\relax
\ifdim\rightexcess>0pt\relax
\setlength{\leftwidth}{\halfline}
\setlength{\rightwidth}{\halfline}
\fi
\fi
% if the right side has space to spare and the left side needs more space than
% just half the line, give the left side the excess space from the right side.
\ifdim\rightexcess>0pt\relax
\ifdim\leftwidth>\halfline\relax
\setlength{\leftwidth}{\dimexpr(\halfline+\rightexcess)}
\fi
\fi
% if the left side has space to spare and the right side needs more space than
% just half the line, give the right side the excess space from the left side.
\ifdim\leftexcess>0pt\relax
\ifdim\rightwidth>\halfline\relax
\setlength{\rightwidth}{\dimexpr(\halfline+\leftexcess)}
\fi
\fi
% command output
\par\noindent%
\begin{minipage}[t]{\leftwidth}%
\begin{flushleft}%
#1
\end{flushleft}%
\end{minipage}%
\begin{minipage}[t]{\rightwidth}%
\begin{flushright}%
#2
\end{flushright}%
\end{minipage}%
}
% works fine
\mycmd
{
left left left left left left left left left left left left left left left
}
{
right right
}
% errors with: Paragraph ended before @settodim was complete.
% \mycmd
% {
% left left left left left left left left left left left left left left left
% left left left
% }
% {
% right right
% }
\end{document}



varwidth:\usepackage{varwidth} \newbox{\mybox} ... \begin{lrbox}{\mybox}\begin{varwidth}{\maxdimen} <paragraph text> \end{varwidth}\end{lrbox}. Now\wd\myboxprovides the widest line length. – Werner Apr 20 '21 at 22:26{LR}would work. – David Carlisle Apr 20 '21 at 22:49varwidthfirst and it ended up working perfectly. I haven't triedtabularyyet, and it's possible it's a better solution for my overall goal (I'll definitely look to it first next time I'm trying to do something like this), but I think thevarwidthsolution better answers the specific question I posted here. @Werner if you want to post your comment as a solution, I'd be happy to accept it. – tdashroy Apr 21 '21 at 17:51\the\linewidth. Any paragraph with more than one full line always will have this width. – Fran Apr 22 '21 at 02:46varwidthof width\xyzwon't shrink any text longer that\xyz, only paragraphs with only 1 line shorter than\xyzlength, or with manual breaks to make several smaller lines. It cannot automatically shrink nor widen multlines paragraphs taking into account what is outside the environment. This need some calculations in advance to find an exact (fixed) optimal\xyz, but then a minipage is enough. On the contrary,tabularycan balance cells with long texts without having to do black magic. – Fran Apr 22 '21 at 04:25\xyzwon't shrink any text longer that\xyz..." that's okay, I'm not usingvarwidthto shrink or widen, just to find the length (width? not sure if these mean different things) of a block of text content that can contain (but doesn't have to) multiple paragraphs. As an aside, I'm only using the word "paragraph" here because I thought that was the technical term in LaTeX for text that is to be placed on a new line. The "paragraphs" in my use-case for this are really just short text broken across lines, none of which are that likely to be the length of entire line. – tdashroy Apr 22 '21 at 20:11