Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
1  
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
add comment

3 Answers

up vote 9 down vote accepted

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

enter image description here

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

share|improve this answer
add comment

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}

enter image description here

(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.)

share|improve this answer
    
Wow, that was easy! –  Werner Jul 8 '12 at 5:13
    
I used $\upbracefill$ before for the brace cell, and I was wondering that what happened was that the array cells are in display math mode, so then, I suppose, the outer display math mode got discarded because $ starts math mode (while already in math mode), and \upbracefill starts (and closes) another math mode, and finally the last $ closes the math mode started by the first $. If that is the case, then I guess it was better before the edit. –  morbusg Jul 9 '12 at 7:45
add comment

You can also use the usual overkill solution with tikz.

enter image description here

Note:

  • This does require two runs. First one to determine the locations, and the second to do the drawing.

References:

Code:

\documentclass{article}

\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{decorations.pathreplacing}

\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}

\newcommand*{\BraceAmplitude}{0.5em}%  Can be tweaked if
\newcommand*{\VerticalOffset}{1.2ex}%  necessary.
\newcommand*{\HorizontalOffset}{0.8em}%  necessary.
\newcommand*{\InsertUnderBrace}[4][]{%
    \begin{tikzpicture}[overlay,remember picture]
\draw [decoration={brace,amplitude=\BraceAmplitude},decorate, thick,draw=blue,text=black,#1]
        ($(#3)+(\HorizontalOffset,-\VerticalOffset)$) -- 
        ($(#2)+(-\HorizontalOffset,-\VerticalOffset)$)
        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}
share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.