# 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.

\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 \noalign{\vspace{-3pt}} \multicolumn{2}{c}{}& \multicolumn{2}{@{}c@{}}{{\upbracefill}} \\ \multicolumn{2}{c}{}& \multicolumn{2}{c}{\scriptstyle\ell} \\ \end{array}
\end{document}


}
},
}

\begin{document}

\begin{center}
\begin{tikzpicture}
\matrix (M)[matrix of math nodes,
nodes={anchor=center,rectangle,
minimum width=9mm, minimum height=5mm,
inner sep=0mm
}
]{
p & q & p\lor{}q & p\land{}q \\
0 & 0 & 0 & 0 \\
0 & 1 & 1 & 0 \\
1 & 0 & 1 & 0 \\
1 & 1 & 1 & 1 \\
};
% vertical lines
\foreach \c in {1,...,4} {
\draw[thick](M-1-\c.north west)--(M-5-\c.south west);
}
% horizontal lines
\foreach \r in {1,5} {
\draw[thick](M-\r-1.south west)--(M-\r-4.south east);
}
% and, finally, the underbrace!
\pic{underbrace={3,4,5,\ell}};
\end{tikzpicture}
\end{center}

\end{document}