# Dynamically sized brackets/parentheses for text

I'd like to have a large bracket that shows a relationship from one set of text to another, like this:

Nam dui ligula, fringilla a,     )
euismod sodales, sollicitudin    )
vel, wisi. Morbi auctor lorem    )  Nulla malesuada porttitor diam. Donec felis erat,
non justo. Nam lacus libero,     )  congue non, volutpat at, tincidunt tristique, libero.
pretium at, lobortis vitae,      )  Vivamus viverra fermentum felis.
...                              )


Instead of the line of round braces ), I'd like to replace them with one proportional curly brace }. I'd like the size of the brace to change to fill the amount of vertical text on the "longer" side.

Here are my ASCII abilities put-to-the-test, depicting what I'd like (where, obviously, the lines are meant to be parts of one glyph for a brace):

Nam dui ligula, fringilla a,     \
euismod sodales, sollicitudin    |
vel, wisi. Morbi auctor lorem    \  Nulla malesuada porttitor diam. Donec felis erat,
non justo. Nam lacus libero,      >  congue non, volutpat at, tincidunt tristique, libero.
pretium at, lobortis vitae,      /  Vivamus viverra fermentum felis.
...                              |
/


In the degenerate case, I'd like:

Nam dui ligula, fringilla a.     } Nulla malesuada portitor diam.


As one side of the text or the other becomes larger, I'd like the brace to grow to be at least as high as the height of the taller side of text.

A starting point I'm working from with the Tex is:

\documentclass[12pt]{memoir}
\usepackage{fontspec,xltxtra,xunicode}
\usepackage{lipsum}

\begin{document}
\begin{tabular}{lcr}
\parbox{0.425\textwidth}{\lipsum[2]}%
& )%
& \parbox{0.425\textwidth}{\lipsum[3]} \\
\end{tabular}
\end{document}


Obviously in the above case we end up with a small and statically sized ) and not a proportionally sized }, however it's just an example of the lines I'm thinking along.

In the past, I've avoided this problem by selecting a sufficiently large font for the }. That technique requires unnecessary "eyeballing", that I'd like to avoid.

I've used braces in math mode, like this:

\begin{math}
\left[% superfluous
\parbox{0.425\textwidth}{\lipsum[2]}%
\right\}%
\parbox{0.425\textwidth}{\lipsum[3]}
\end{math}


This is the right concept, except (1) the braces are proportional to the left side; and (2) there's a superfluous left brace.

I'm grateful for any thoughts & suggestions.

Edit: Related questions:

-

You can use \vphantom to adjust the size so that max is used. The macro below has 4 parameters:

1. 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.
2. The brace that you want to use.
3. The content of the left \parbox
4. 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}

-

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.

-

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.

-

Another solution that builds two equal width columns:

\documentclass[a4paper]{article}

\newenvironment{reltext}[1][1]
{\calcreltextwd{#1}%
\begin{center}\hbox to\linewidth\bgroup\hss
$\mathsurround=0pt\nulldelimiterspace=0pt \bgroup\left.\begin{minipage}{\reltextwd} \vspace*{2pt}} {\vspace*{2pt}\end{minipage}\checkreltext$\hss\egroup\end{center}}
\newcommand{\righttext}{\end{minipage}\right\rbrace\egroup
\def\righttextgiven{}\begin{minipage}{\reltextwd}}
\newcommand{\checkreltext}{%
\ifdefined\righttextgiven\else\right\rbrace\egroup\fi}

\newcommand{\calcreltextwd}[1]{%
\sbox0{$\nulldelimiterspace=0pt{\left.\vbox to 100pt{}\right\rbrace}$}%
\edef\reltextwd{\the\dimexpr(#1\linewidth-\wd0)/2\relax}}

\usepackage{lipsum}
\begin{document}
\lipsum[2]
\begin{reltext}
\lipsum*[3]
\righttext
\lipsum*[2]
\end{reltext}
\lipsum[2]
\newpage
\lipsum[2]
\begin{reltext}[.8]
\lipsum[4]
\righttext
\lipsum[4]
\end{reltext}
\end{document}


The command \righttext separates the left text from the right text; if it's not specified, it will be clear because there will be nothing to the right of the big brace.

The optional argument (default 1) to the environment is a "scale factor" for the total width of the object with respect to the line width.

The \nulldelimiterspace setting is meant to remove the space that TeX usually places when finding \left. or \right.. The supplementary braces serve to avoid having an "inner atom".

-