14

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?

  • 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
10

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.

11

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}

enter image description here

  • 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
  • \omit and \multispan are not very idiomatic latex the same could be made with \multicolumn{2}{c}{}& \multicolumn{2}{c}{${\upbracefill}$} \\ \multicolumn{2}{c}{}& \multicolumn{2}{c}{\scriptstyle\ell} \\ This answer was referenced here tex.stackexchange.com/q/400513/1090 – David Carlisle Nov 9 '17 at 16:44
  • @DavidCarlisle Yes, I understand, I’ll amend the answer. Btw can you tell how the original answer worked? It’s been puzzling me. – morbusg Nov 9 '17 at 16:54
  • 2
    @morbusg that;same as i did although I added {} for safety. the math mode cell is $...$ and you added $$...$$ so there are two empty math lists doing nothing (unless you set mathsurround) and ... is out of math mode. $$ is an empty math list not a start of display math in inner h mode (try \mbox{$$ one two three $$} – David Carlisle Nov 9 '17 at 19:44
  • 1
    \noalign isn't either (as I say there is a booktabs alternative) but without a package there isn't an obvious alternative normally you could use \\[-2pt] but not after \hline a failing in the latex syntax. but no list really: it's a judgment call whether using lower level things likely to break other macros. I only commented on the \multispan use as spanning cells is trickier than \noalign and as a user had shown that it didn't really work with beamer, I felt it was probably easier to change this than change beamer or add some complicated workaround to make it all work together . – David Carlisle Nov 10 '17 at 7:27
6

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}
  • 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
2

Here is another tikz solution, this time using a matrix of math nodes. There is some trickery with pic to put the underbrace in the right place, but the end result is:

enter image description here

Of course, you can add colour and styling to your heart's content! Here's the code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,calc}
\newdimen\colL
\newdimen\colR

\tikzset{%
  pics/underbrace/.style args={#1,#2,#3,#4}{%
    % a pic for putting an underbrace under certain columns
    % #1 = left column
    % #2 = right column index
    % #3 = index of the row with the underbrace
    % #4 = label text
    code = {
       \pgfextractx\colL{\pgfpointanchor{M-#3-#1}{south west}}  % x-coords of columns
       \pgfextractx\colR{\pgfpointanchor{M-#3-#2}{south east}}
       % draw the delimiter with the correct width
       \node[rectangle, below delimiter={\}}, minimum width=\dimexpr\colR-\colL-1pt]
              at ($ (M-#3-#1)!0.5!(M-#3-#2)+(0,-0.1) $){};
       % the label
       \node at ($ (M-#3-#1.south)!0.5!(M-#3-#2.south) $)[below=2mm]{$\scriptstyle #4$};
       }
   },
}

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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