5

I try to format a table in LaTeX and have trouble putting content on the left and the right side of the table. This is what it looks like:

Current Table

And this is what I expect it to look like:

Expected Table

I want to add the table name on the left side of the table. I tried to achieve this by adding another column to the table before the first column and adding the table name there, but unfortunately the three lines in the pictures were drawn as well. I want the table name to look like it is not part of the actual table.

The second problem I have is that the Relation text on the right side should only apply to the three rows and not the whole table. I have only found solutions which caused the bracket to be placed inside the table like they did it here or solutions which used \left and \right commands which I was not able to compile without errors.

This should be the MWE:

\documentclass[a4paper, twoside=false, DIV=12, BCOR=5mm, bibliography=totocnumbered, headheight=1cm, footheight=1cm, numbers=autoendperiod]{scrartcl}
\usepackage{booktabs}
\usepackage[table]{xcolor}
\usepackage{amsmath}

\newenvironment{rcases}
  {\left.\begin{aligned}}
  {\end{aligned}\right\rbrace}

\begin{document}
\begin{figure}[hbp]
\center
\[
\begin{rcases}
\begin{tabular}{lllr}
\toprule
\multicolumn{1}{c}{\textbf{A}} & \multicolumn{1}{c}{\textbf{B}} & \multicolumn{1}{c}{\textbf{C}} & \multicolumn{1}{c}{\textbf{D}}\\
\midrule
    1   & 4 & 7 & 16 \\
    2   & 5 & 8 & 15 \\
    3   & 6 & 9 & 14 \\
    \rowcolor{gray!30}10 & 11 & 12 & 13 \\
    & & & \multicolumn{1}{c}{$\underbrace{\hspace{15em}}$} \\
    & & & \multicolumn{1}{c}{Column} \\
\bottomrule
\end{tabular}   
~\hspace{0.5cm}\end{rcases} Relation
\]
\caption{Example of a DB}
\end{figure}

\end{document}
Drudge
  • 251
  • @cfr sorry for that, I fixed the example and made sure it compiles. – Drudge Oct 25 '15 at 00:24
  • Thanks for fixing your code. It was the figure before document that suggested less than extensive testing ;). – cfr Oct 25 '15 at 01:13

2 Answers2

5

Something like this?

tabular and annotations

I've used tikzmark which requires TikZ. This probably isn't the most efficient strategy unless you need TikZ anyway, but I tend to use it because I'm familiar with it and it works.

\documentclass[a4paper, twoside=false, DIV=12, BCOR=5mm, bibliography=totocnumbered, headheight=1cm, footheight=1cm, numbers=autoendperiod]{scrartcl}
\usepackage{booktabs}
\usepackage[table]{xcolor}
\usepackage{tikz}
\usetikzlibrary{tikzmark,decorations.pathreplacing,calc}
\newlength\mywidth
\begin{document}
  \begin{figure}[hbp]
    \centering
    \settowidth\mywidth{\textbf{\MakeUppercase{Table Name}}}
    \begin{minipage}[t]{\mywidth}
      \mbox{}\par
      \textbf{\MakeUppercase{Table Name}}
    \end{minipage}
    \begin{tabular}[t]{lllr}
      \toprule
      \multicolumn{1}{c}{\textbf{A}} & \multicolumn{1}{c}{\textbf{B}} & \multicolumn{1}{c}{\textbf{C}} & \multicolumn{1}{c}{\textbf{D}}\tikzmark{a}\\
      \midrule
      1   & 4 & 7 & 16\tikzmark{b}\\
      2   & 5 & 8 & 15 \\
      3   & 6 & 9 & 14 \\
      \rowcolor{gray!30}10 & 11 & 12 & 13\tikzmark{c}\\
      & & & \multicolumn{1}{c}{$\underbrace{\hspace{15em}}$}\tikzmark{d}\\
      & & & \multicolumn{1}{c}{Column} \\
      \bottomrule
    \end{tabular}
    \tikz[remember picture, overlay]{%
      \draw [decorate, decoration={brace, amplitude=5pt}] ($({pic cs:a})!1/2!({pic cs:b})$) +(1.5em,0) coordinate (e) -- ({$({pic cs:c})!1/2!({pic cs:d})$} -| e) node [midway, right, xshift=5mm] {Relation};}
    \caption{Example of a DB}
  \end{figure}
\end{document}

EDIT

There has to be a better way to do this, but...

\documentclass[a4paper, twoside=false, DIV=12, BCOR=5mm, bibliography=totocnumbered, headheight=1cm, footheight=1cm, numbers=autoendperiod]{scrartcl}
\usepackage{booktabs}
\usepackage[table]{xcolor}
\usepackage{tikz,showframe,calc}
\usetikzlibrary{tikzmark,decorations.pathreplacing,calc}
\newlength\mywidth
\begin{document}
  \begin{figure}[hbp]
    \centering
    \settowidth\mywidth{\textbf{\MakeUppercase{Table Name}}}
    \begin{minipage}[t]{\mywidth}
      \mbox{}\par
      \textbf{\MakeUppercase{Table Name}}
    \end{minipage}
    \begin{tabular}[t]{lllr}
      \toprule
      \multicolumn{1}{c}{\textbf{A}} & \multicolumn{1}{c}{\textbf{B}} & \multicolumn{1}{c}{\textbf{C}} & \multicolumn{1}{c}{\textbf{D}}\tikzmark{a}\\
      \midrule
      1   & 4 & 7 & 16\tikzmark{b}\\
      2   & 5 & 8 & 15 \\
      3   & 6 & 9 & 14 \\
      \rowcolor{gray!30}10 & 11 & 12 & 13\tikzmark{c}\\
      & & & \multicolumn{1}{c}{$\underbrace{\hspace{15em}}$}\tikzmark{d}\\
      & & & \multicolumn{1}{c}{Column} \\
      \bottomrule
    \end{tabular}
    \settowidth\mywidth{Relation}%
    \addtolength\mywidth{1.5em+5mm+5pt}%
    \hspace*{\mywidth}%
    \tikz[remember picture, overlay]{%
      \draw [decorate, decoration={brace, amplitude=5pt}] ($({pic cs:a})!1/2!({pic cs:b})$) +(1.5em,0) coordinate (e) -- ({$({pic cs:c})!1/2!({pic cs:d})$} -| e) node [midway, right, xshift=5mm] {Relation};}
    \caption{Example of a DB}
  \end{figure}
\end{document}

showframe is just to see the figure relative to the page. Remove it for real use. The result (with the frame shown):

adjusted

This is probably not quite perfectly centred but it may be close enough....

cfr
  • 198,882
  • Thanks, it works perfectly as expected! Does \settowidth\mywidth{\textbf{\MakeUppercase{Table Name}}} store the textlength of the String "Table Name" for using this value later? And is the "Table Name" placed there by the Minipage or is the another trick behind the placement? – Drudge Oct 25 '15 at 00:56
  • Yes. It stores the width so that the minipage matches the width required. This keeps the centring correct. Otherwise, if you change the name or font, you'd have to adjust a hard-coded width to avoid the centring being off. The minipage is placing it. The \mbox{}\par is to compensate for the skip around the \toprule of the tabular. The [t] is aligning things at the top, but we want the first line of the minipage moved down a bit to compensate for the start of the tabular. Does that make sense? Say if it is not coherent! – cfr Oct 25 '15 at 01:11
  • It makes sense, indeed. But for me as a LaTeX beginner, it's not a solution I would have figured out myself. That's why I asked, I want to get the concepts behind those things even more than just the solution. The tikz solution is sneaky, too. I definitely should have a closer look at some examples. – Drudge Oct 25 '15 at 01:18
  • The tikzmark library is incredibly useful. Since I discovered it, I find myself using it even though I didn't know I needed it beforehand, if you know what I mean. On the other hand, if you are a LaTeX beginner, I would not prioritise TikZ immediately. There is a lot of it and LaTeX already has what they politely refer to as a 'steep learning curve'. – cfr Oct 25 '15 at 01:39
  • Yes, I know what you mean. I am able to draw some basic shapes and connections with tikz, but there is too much magic I don't know yet. But I think even if there is a lot of time needed to learn how to deal with tikz, it can save you even more time afterwards :) – Drudge Oct 25 '15 at 01:46
  • That's the theory. But it can be rather... addictive. – cfr Oct 25 '15 at 02:09
  • One last question to a problem I encounter. It seems that LaTeX threats my table as the figure but not the Tikz picture. Hence, the figure is not centered on the page. Is there a way to center it anyway? – Drudge Oct 25 '15 at 02:30
  • To be honest, I'd not thought about that. Maybe Harish Kumar's solution would work better? You could use the minipage from my answer and add the [t] position specifier to HK's tabular to combine the stuff I put on the left with the stuff he puts on the right. – cfr Oct 25 '15 at 02:42
  • Thanks for the idea. Currently I added a \hspace*{-3cm} after the beginning of the figure. At least it works. If it becomes more complex or I need it more often, I'll try HK's idea! – Drudge Oct 25 '15 at 02:46
  • @Drudge Actually, my idea unfortunately doesn't work. It still ignores the stuff on the right when centring. – cfr Oct 25 '15 at 02:48
  • Good to know. I think I'll continue using hspace. I just have to use 10 or 15 figures, shouldn't be a problem to do it by hand. – Drudge Oct 25 '15 at 02:50
  • @Drudge Please see edit. Rather brute force, but seems to work.... – cfr Oct 25 '15 at 03:05
  • sadly, the shape of the brace at the right doesn't match the shape of the underbrace. (maybe this is just the image on the screen?) – barbara beeton Oct 25 '15 at 15:21
  • @barbarabeeton Probably not. It wouldn't particularly bother me, to be honest, and you could certainly adjust the thickness of the TikZ bracket. That would probably be good enough for me. But, then, I'd never use \underbrace, so I'd be unlikely to encounter the issue in the first place. You could use TikZ for both, of course. But that would get very complicated, I think. – cfr Oct 25 '15 at 16:13
5

Another method using bigdelim: from documentation (in the source code)

% Usage in a tabular or array:
% \usepackage{multirow}
% \usepackage{bigdelim}
% \ldelim({n}{w}[text]
% \rdelim){n}{w}[text]
% Use these commands in a column of a tabular or array. They generate a big
% parenthesis/brace extending over the next n rows (including this one).
% The corresponding entries in the next rows must explicitely be given as
% empty cells. The first parameter must be the delimiter to be used, e.g.
% \{ \} [ ] ( ), in fact any parameter that can be used with \left and
% \right. The text is optional and is set centered to the left of
% \ldelim and to the right of \rdelim.  w must be the width reserved for
% the brace and text.  If you have a recent version of multirow.sty {w} may
% be given as {*}, but this may cause the braces to be too small. Also with
% a recent version of multirow.sty the commands may be used in the last row
% of the extension with a negative n parameter. This is useful in
% combination with the colortbl package (see multirow.sty)
% If there are unusually large rows you may have to enlarge n (you can use
% non-integral values).
% If you have horizontal lines that interact with the braces you are
% advised to use the hhline package to make the lines.

The code:

\documentclass[a4paper, twoside=false, DIV=12, BCOR=5mm, bibliography=totocnumbered, headheight=1cm, footheight=1cm, numbers=autoendperiod]{scrartcl}
\usepackage{booktabs}
\usepackage[table]{xcolor}
\usepackage{amsmath}

\usepackage{multirow}
\usepackage{bigdelim}

\newenvironment{rcases}
  {\left.\begin{aligned}}
  {\end{aligned}\right\rbrace}

\begin{document}
\begin{figure}[hbp]
\centering

\begin{tabular}{lllrl@{}}
\toprule
\multicolumn{1}{c}{\textbf{A}} & \multicolumn{1}{c}{\textbf{B}} & \multicolumn{1}{c}{\textbf{C}} & \multicolumn{1}{c}{\textbf{D}}\\
\midrule
    1   & 4 & 7 & 16 & \\
    2   & 5 & 8 & 15 & \\
    3   & 6 & 9 & 14 & \\
    \rowcolor{gray!30}10 & 11 & 12 & 13 & \cellcolor{white}\rdelim\}{-4}{-\tabcolsep}[Relation] \\
    & & & \multicolumn{1}{c}{$\underbrace{\hspace{15em}}$} & \\
    & & & \multicolumn{1}{c}{Column} & \\
\bottomrule
\end{tabular}
\caption{Example of a DB}
\end{figure}

\end{document}

enter image description here

  • I used cfr's answer but thank your for your answer, too :) – Drudge Oct 25 '15 at 02:07
  • Can't upvote you again ;). (I upvoted for the trick changing the cell colour off - I hate dealing with coloured rows in tables.) – cfr Oct 25 '15 at 02:26