17

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?

David Carlisle
  • 757,742
  • 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 08 '12 at 02:15

4 Answers4

13

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

morbusg
  • 25,490
  • 4
  • 81
  • 162
  • 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 09 '12 at 07: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 https://tex.stackexchange.com/q/400513/1090 – David Carlisle Nov 09 '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 09 '17 at 16:54
  • @morbusg but it's your answer:-) Which bit is puzzling? – David Carlisle Nov 09 '17 at 16:59
  • @DavidCarlisle your solution does not start/end at the vertical lines – user1 Nov 09 '17 at 17:06
  • @DavidCarlisle and it contains more vertical space above the upbrace – user1 Nov 09 '17 at 17:13
  • @Ben @{}c@{} fixes the width and possibly easiest way to adjust the vertical position is \noalign (or the booktabs package has a command for adding inter-row space) \noalign{\vspace{-3pt}} \multicolumn{2}{c}{}& \multicolumn{2}{@{}c@{}}{${\upbracefill}$} \\ \multicolumn{2}{c}{}& \multicolumn{2}{c}{\scriptstyle\ell} \\ – David Carlisle Nov 09 '17 at 18:27
  • @DavidCarlisle in the original answer I had $\upbracefill$ as the array cell content (note the math mode). Now, as the cells are already in math mode inside array, I don't quite understand how it was able to work, as $$ starts a display math mode (illegal inside \halign?). – morbusg Nov 09 '17 at 19:28
  • 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 09 '17 at 19:44
  • @DavidCarlisle Oh! Oh!! "$$ is an empty math list in inner h mode" This was completely new to me, Thank you! – morbusg Nov 09 '17 at 19:50
  • @DavidCarlisle is there a list of idiomatic latex commands which are from plain/core (as here \omit is non-idiomatic, but \noalign isn’t)? – morbusg Nov 10 '17 at 05:59
  • 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 07:27
  • shorter version of the above: I break my own rules:-) – David Carlisle Nov 10 '17 at 07:30
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.

Moriambar
  • 11,466
Werner
  • 603,163
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}

Peter Grill
  • 223,288
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}