Dynamically sized brackets/parentheses for text
You can use \vphantom
to adjust the size so that max is used. The macro below has 4 parameters:
- The width of the parbox. If you want to allow for different widths you could adjust this macro to take two paramters, one for each width. This parameter is optional and defaults to
0.425\textwidth
. - The brace that you want to use.
- The content of the left
\parbox
- The content of the right
\parbox
Here is what it looks like with a curly brace: \RelatedText{\}}{\lipsum[2]}{\lipsum[3]}
\documentclass[12pt]{memoir}
\usepackage{lipsum}
\newcommand{\RelatedText}[4][0.425\textwidth]{%
\begin{math}%
\left.%
\parbox{#1}{#3}\vphantom{\parbox{#1}{#4}}%
\right#2%
\parbox{#1}{#4}\vphantom{\parbox{#1}{#3}}%
\end{math}
}%
\begin{document}
\RelatedText{)}{\lipsum[2]}{\lipsum[3]}% Use a round )
\RelatedText{\}}{\lipsum[2]}{\lipsum[3]}% Use a curly }
\RelatedText{]}{\lipsum[2]}{\lipsum[3]}% Use a square ]
\end{document}
Remember that you can supply an empty extensible delimiter to match a non-empty one. This is possible using (say) \left.
or \right.
to denote the missing delimiter. I've used this approach in my solution below.
Put the lefthand block of text in a box (called \leftbox
) and the righthand block of text in another box (called \rightbox
). This is easy enough using the lrbox
environment. Then, to ensure that the alignment is the largest possible, an "invisible" \rule
can be used.
In the MWE below, the command \lrboxbrace[<lwidth>]{<ltext>}[<rwidth>]{<rtext>}
is provided. It typesets <ltext>
in a minipage
of width <lwidth>
and <rtext>
in a minipage
of width <rwidth>
. The starred version *
duplicates this behaviour using the varwidth
environment (from the varwidth
package). The actual display is performed in math mode using \ensuremath
, allowing it to be placed in existing math mode (like $...$
or \[...\]
), or text mode:
\documentclass{article}
\usepackage[margin=15mm]{geometry}% http://ctan.org/pkg/geometry
\usepackage{xparse}% http://ctan.org/pkg/xparse
\usepackage{varwidth}% http://ctan.org/pkg/varwidth
\begin{document}
\newsavebox{\leftbox} \newsavebox{\rightbox}%
\NewDocumentCommand{\lrboxbrace}{s O{0.4\linewidth} m O{0.4\linewidth} m}{% \lrboxbrace[<lwidth>]{<ltext>}[<rwidth>]{<rtext>}
\begin{lrbox}{\leftbox}% Left box
\IfBooleanTF{#1}% starred/unstarred
{\begin{varwidth}{#2}#3\end{varwidth}}
{\begin{minipage}{#2}#3\end{minipage}}
\end{lrbox}
\begin{lrbox}{\rightbox}% Right box
\IfBooleanTF{#1}% starred/unstarred
{\begin{varwidth}{#4}#5\end{varwidth}}
{\begin{minipage}{#4}#5\end{minipage}}
\end{lrbox}
\ensuremath{\left.\usebox\leftbox\rule{0pt}{\ht\rightbox}\right\}\usebox\rightbox}
}
\begin{center}
\lrboxbrace%
{Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Sed eleifend tincidunt enim, eu tincidunt felis auctor quis. Aenean
eget enim urna.}%
{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eleifend
tincidunt enim, eu tincidunt felis auctor quis. Aenean eget enim urna.
Pellentesque tincidunt adipiscing velit a fermentum. \endgraf
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eleifend
tincidunt enim, eu tincidunt felis auctor quis. Aenean eget enim urna.
Pellentesque tincidunt adipiscing velit a fermentum.}
\end{center}
\begin{center}
\lrboxbrace%
{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eleifend
tincidunt enim, eu tincidunt felis auctor quis. Aenean eget enim urna.
Pellentesque tincidunt adipiscing velit a fermentum. \endgraf
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eleifend
tincidunt enim, eu tincidunt felis auctor quis. Aenean eget enim urna.
Pellentesque tincidunt adipiscing velit a fermentum.}
{Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Sed eleifend tincidunt enim, eu tincidunt felis auctor quis. Aenean
eget enim urna.}%
\end{center}
\begin{center}
\lrboxbrace*% starred version
{A short sentence.}
{And another.}%
\end{center}
\begin{center}
\lrboxbrace%
[60pt]{Here is some different text that should be split}%
[120pt]{This is another line that is actually wider than the box
on the left. It should span five lines in total (one more
than the left box).}
\end{center}
\end{document}
geometry
was loaded just to increase the margins. xparse
provides the interface to easily intermix optional and mandatory arguments.
One caveat of this approach is the fact that paragraph use is slightly limited. It is possible, but requires the use of \endgraf
rather than \par
or an empty line. However, since this was not included in the original request, it may not be a problem.
You can get rid of the superfluous left brace by using \left.
and scale depending on the height of both sides using \vphantom
. The solution below uses scratch boxes for efficiency.
\newbox\braceboxa
\newbox\braceboxb
\makeatletter
\newcommand\braced[2]
{\setbox\braceboxa\hbox{\parbox{0.45\linewidth}{#1}}%
\setbox\braceboxb\hbox{\parbox{0.45\linewidth}{#2}}%
\hbox to \linewidth
\bgroup \hss \copy\braceboxa\hss
\m@th$\left.\vphantom{\copy\braceboxa\copy\braceboxb}\right\}$%
\hss\copy\braceboxb\hss
\egroup}
\makeatother
Note that I am scaling to \linewidth
rather than \textwidth
and have added \hss
at appropriate places so that the remaining whitespace of 0.1\linewidth
is equally distributed.