# Using \underbrace with table columns

I have the following code:

\begin{displaymath}
\begin{array}{|c|c|c|c}
p
& q
& p\lor{}q
& p\land{}q \\
\hline
0 & 0 & 0 & 0 \\
0 & 1 & 1 & 0 \\
1 & 0 & 1 & 0 \\
1 & 1 & 1 & 1 \\
\hline
\end{array}
\end{displaymath}


I'd like to place an \underbrace under the last two columns, but can't find any TeX/LaTex manuals indicating how to do this. Any ideas?

-
Welcome to TeX.SE. For future reference, please keep in mind that while code snippets are useful in explanations, it is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. –  Peter Grill Jul 8 '12 at 2:15

You need to play around with the spaces used inside of array:

\documentclass{article}
\begin{document}
$\begin{array}{|c|c|c|c} p & q & p\lor{}q & p\land{}q \\ \hline 0 & 0 & 0 & 0 \\ 0 & 1 & 1 & 0 \\ 1 & 0 & 1 & 0 \\ 1 & 1 & 1 & 1 \\ \hline \multicolumn{1}{c}{} & \multicolumn{1}{c}{} & \multicolumn{2}{@{}l@{}}{% \raisebox{.5\normalbaselineskip}{% \rlap{\underbrace{\hphantom{\mbox{p\lor{}q\hspace*{\dimexpr4\arraycolsep+\arrayrulewidth}p\land{}q}}}_{\ell}}}% } \end{array}$
\end{document}


The last two columns have the combined width of

• $p\lor{}q$ (third column heading)
• $p\land{}q$ (fourth column heading)
• 4\arraycolsep (two on either side of the two headings, totalling 4)
• \arrayrulewidth (between the two headings)

The addition of \multicolumns is to remove the vertical rules inserted with your column specification |c|c|c|c|. Finally, \raisebox{.5\normalbaselineskip} raises the entire \underbrace up half a line.

-

There is also \upbracefill (and \downbracefill) which uses \leaders to build an extending brace. For example, \hbox to 5cm{\upbracefill} would "fill" that five centimeter length with the brace. The \upbracefill starts math-mode, and when already in math-mode with the array, I need to escape the array's math-mode for those columns (lest TeX gets into display-math-mode). I use primitives for this here because I don't know the LaTeX equivalents.

After \omitting the column definition(s), I need to reinstate the \strut on the line, so that the brace-row doesn't touch the previous \hrule. I use a slightly smaller \mathstrut for this. I am also \omitting the first couple of columns for the brace-row and \ell-row because otherwise the vertical rules would extend to the last two rows.

\documentclass{article}
\begin{document}
$\begin{array}{|c|c|c|c} p & q & p\lor{}q & p\land{}q \\ \hline 0 & 0 & 0 & 0 \\ 0 & 1 & 1 & 0 \\ 1 & 0 & 1 & 0 \\ 1 & 1 & 1 & 1 \\ \hline \omit & \omit & \omit\span\omit\mathstrut\upbracefill \\ \omit & \omit & \multicolumn{2}{c}{\scriptstyle\ell} \\ \end{array}$
\end{document}


(One could also use \multispan<no of cols> <col contents> to emit \omit\span\omit..., so the first row could be \multispan2 & \multispan2\mathstrut\upbracefill.)

-
Wow, that was easy! –  Werner Jul 8 '12 at 5:13
node [below=\VerticalOffset, midway] {#4};
\end{tikzpicture}%
}%

\begin{document}
$\begin{array}{|c|c|c|c} p & q & p\lor{}q & p\land{}q \\ \hline 0 & 0 & 0 & 0 \\ 0 & 1 & 1 & 0 \\ 1 & 0 & 1 & 0 \\ 1 & 1 & \tikzmark{StartBrace}1 & 1\tikzmark{EndBrace} \\ \hline \end{array}$
\InsertUnderBrace[draw=red,text=blue]{StartBrace}{EndBrace}{Explanation}
\end{document}

-
Ah, I couldn't make this work. Yours is much prettier. –  Ryan Reich Jul 8 '12 at 2:31
@RyanReich: Helps to have something to start with. –  Peter Grill Jul 8 '12 at 2:57