1

I'm looking for a way to combine the minipage and varwidth environments side by side, so that the width of minipage automatically adapts to that of varwidth, to fill the entire line.

\begin{minipage}{\linewidth-\varwidth}
Content of minipage.
\end{minipage}\hfill%
\begin{varwidth}{.5\linewidth}
Content of varwidth.
\end{varwidth}

A two-step compilation may be required, but thanks for any suggestion. I don't know how to get the width of varwidth.

MB4E
  • 1,084

2 Answers2

1

The \MeasureVarwidth[<maximum width of varwidth>]{<text>} command will measure the natural width of a text and save the content for later use.

If the measured content of text is greater than the optional <maximum width of varwidth> (default= 0.5\linewidth), the width of the varwidth part will be set to that value.

The width of the minipage will then be set to fill the entire line.

The minipages were enclosed in a \fbox for better visualization.

a

\documentclass{article}
\usepackage{showframe} % show the margins

\usepackage{calc}% needed \usepackage{varwidth}

\newsavebox{\ToVarwidth} \newlength{\WidthofVarwidth} \newlength{\miniwidth}

% save box and measure its width \newcommand{\MeasureVarwidth}[2][0.5\linewidth]{% #1 optional max width of varwidth, #2 varwidth text \savebox{\ToVarwidth}{#2}%
\settowidth{\WidthofVarwidth}{\usebox{\ToVarwidth}}% \setlength{\miniwidth}{\linewidth-\WidthofVarwidth}% \ifdim\WidthofVarwidth>#1% \savebox{\ToVarwidth}{\parbox{#1}{#2}}%
\setlength{\miniwidth}{\linewidth-#1}% \fi }

\begin{document}

\setlength{\fboxsep}{0pt}% only for \fbox

\MeasureVarwidth{Some text}

\noindent\fbox{%% \fbox only to visualize \begin{minipage}{\miniwidth} Content of minipage. \end{minipage}% }\hfill \begin{varwidth}{\linewidth} \usebox{\ToVarwidth} \end{varwidth}

\bigskip

\MeasureVarwidth{Some text Some text Some text}

\noindent\fbox{% \begin{minipage}{\miniwidth} Content of minipage. \end{minipage}% }\hfill \begin{varwidth}{\linewidth} \usebox{\ToVarwidth} \end{varwidth}

\bigskip \MeasureVarwidth{Some text Some text Some text Some text Some text Some text Some text }

\noindent\fbox{% \begin{minipage}{\miniwidth} Content of minipage. \end{minipage}% }\hfill \begin{varwidth}{\linewidth} \usebox{\ToVarwidth} \end{varwidth}

\bigskip

Changing the maximum width of \verb|varwidth| \medskip

\MeasureVarwidth[0.7\linewidth]{Some text Some text Some text Some text Some text Some text Some text }

\noindent\fbox{% \begin{minipage}{\miniwidth} Content of minipage. \end{minipage}% }\hfill \begin{varwidth}{\linewidth} \usebox{\ToVarwidth} \end{varwidth}

\end{document}

Simon Dispa
  • 39,141
1

First possibility

\documentclass{article}
\usepackage{varwidth}

\usepackage{lipsum}

\ExplSyntaxOn

\NewDocumentEnvironment{doubleminipage}{+b} { \mbfore_doubleminipage:n { #1 } }{}

\seq_new:N \l__mbfore_doubleminipage_parts_seq

\cs_new_protected:Nn \mbfore_doubleminipage:n { \seq_set_split:Nnn \l__mbfore_doubleminipage_parts_seq { \BREAK } { #1 } \hbox_set:Nn \l_tmpa_box { \begin{varwidth}{0.5\linewidth} \seq_item:Nn \l__mbfore_doubleminipage_parts_seq { 2 } \end{varwidth} } \noindent \begin{minipage}{\dim_eval:n { \linewidth - \box_wd:N \l_tmpa_box - 1em }} \seq_item:Nn \l__mbfore_doubleminipage_parts_seq { 1 } \end{minipage} \hfill \box_use:N \l_tmpa_box }

\ExplSyntaxOff

\begin{document}

\begin{doubleminipage} \lipsum[1][1-4] \BREAK Some contents \ on the right \end{doubleminipage}

\end{document}

Second possibility

\documentclass{article}
\usepackage{varwidth}

\usepackage{lipsum}

\ExplSyntaxOn

\NewDocumentEnvironment{doubleminipage}{m} { __mbfore_doubleminipage:n { #1 } \noindent \begin{minipage}{\dim_eval:n { \linewidth - \box_wd:N \l_tmpa_box - 1em }} } { \end{minipage}\hfill\box_use:N \l_tmpa_box }

\cs_new_protected:Nn __mbfore_doubleminipage:n { \hbox_set:Nn \l_tmpa_box { \begin{varwidth}{0.5\linewidth} #1 \end{varwidth} } }

\ExplSyntaxOff

\begin{document}

\begin{doubleminipage}{ Some contents \ on the right } \lipsum[1][1-4] \end{doubleminipage}

\end{document}

Output for both

enter image description here

Note

I used a 1em gutter in order to separate the two boxes.

egreg
  • 1,121,712
  • The first possibility seems better for my usage, but doesn't seem to produce the expected result. – MB4E Sep 11 '23 at 06:37
  • @MB4E Sorry, but you’re not saying what is the result you expect. – egreg Sep 11 '23 at 07:00
  • The result is good for me, but the first possibility does not seem to work correctly and does not give the same result as the second one. – MB4E Sep 11 '23 at 07:07
  • @MB4E Sorry, there was a typo, not sure how it crept in. – egreg Sep 11 '23 at 07:19
  • This is now working fine with current usage, even with \begin{tcolorbox}[hbox] x \end{tcolorbox} environments on the right side, but not with something like \begin{tcblisting}{hbox,listing only} x \end{tcblisting} or just \begin{verbatim} x \end{verbatim}. Is there a way to use verbatim content in the right side ? I had tried before and it is possible to put verbatim content in the varwidth environment. – MB4E Sep 11 '23 at 07:42
  • @MB4E Verbatim would require a rather different approach. – egreg Sep 11 '23 at 07:55
  • Ok, new question here : https://tex.stackexchange.com/q/695710/56520. – MB4E Sep 11 '23 at 09:02