2

I want to create a table with a fancy outer box. I thought it is a good idea to use this example from texample.net

The problem is that it is not possible to use floats inside the tikzpicture and the other bigger problem is, I want to set my caption in a way that ut appeards in the fancytitle but it should be a "real" caption as I use \listoftables.

If it is important: In this example there is this $\clubsuit$ on the right side of the box. In my application there will be a custom small tikzpicture.

Is it possible to achive this without tikz? Is it possible anyway?

MWE

\documentclass{article}
\usepackage{longtable}
\usepackage{caption}

% tikz example taken from http://www.texample.net/tikz/examples/boxes-with-text-and-math/
\usepackage{tikz}
\usetikzlibrary{shapes,snakes}
% Define box and box title style
\tikzstyle{mybox} = [draw=red, fill=blue!20, very thick, rectangle, rounded corners, inner sep=10pt, inner ysep=20pt]
\tikzstyle{fancytitle} = [fill=red, text=white]
\begin{document}

\listoftables

  \begin{tikzpicture}
    \node [mybox] (box){%
    % put table here
    %  \begin{table}
    %    \caption{the cpation}
    %    \centering
    %    \begin{tabular}{l l}
    %      a &b \\
    %      b &d
    %    \end{tabular}
    %    \label{tab:tab}
    %  \end{table}
    };
    \node[fancytitle, right=10pt] at (box.north west) {the fancy caption}; % put caption here, it must be a "real" caption since I need the cpation for \listoftables
    \node[fancytitle, rounded corners] at (box.east) {$\clubsuit$};
  \end{tikzpicture}%
\end{document}

Wanted Output

wanted output

user69453
  • 936

2 Answers2

3

Here's a solution using Tikz and the package caption. I have also added a regular table to show you how they appear in text. The newcommand for the fancy table is the following:

\fancytab[ <optional> ]{ <label> }{ <caption> }{ <table goes here> }

While the arguments are all self-explanatory, the first optional argument only takes \centering or other alignment commands like \flushright, anything else would probably give an error or not work at all. If you ignore this parameter, the table will appear left-aligned.

I have tried to test the command for robustness and everything seems in order, but let me know if you notice some situations where it breaks.

Output (two pages)

enter image description here

enter image description here

Code

\documentclass{article}
\usepackage{tikz}
\usepackage{caption}
\usepackage{geometry}
\usepackage{booktabs}

\usetikzlibrary{calc, backgrounds}

\newcommand\fancytab[4][]{%
\vspace*{\baselineskip}\noindent
\begin{minipage}{\linewidth}%
\captionsetup[table]{format=plain,skip=0pt,indention=0pt, font={color=white}}
\newlength{\mywidth}
\settowidth{\mywidth}{Table~n:~#3+2}
#1
\begin{tikzpicture}
\node (around) {%
#4
};
\begin{scope}[on background layer]
\draw[red, rounded corners, thick, fill=blue!10] ($(around.north west)+(0,5mm)$) rectangle (around.south east);
\node[fill=red, anchor=west, xshift=3mm, text width=\mywidth] at ($(around.north west)+(0,5mm)$) {\captionof{table}{#3}};
\node[text=white, fill=red] at (around.east) {$\clubsuit$};
\end{scope}
\end{tikzpicture}
\label{tab:#2}
\end{minipage}\par
\vspace*{\baselineskip}
\let\mywidth\relax % so we can reuse the same length
}

\begin{document}

\listoftables
\newpage

\begin{table}[!htb]
\centering
\caption{One caption}
\begin{tabular}{|r|l|}
  \hline
  7C0 & hexadecimal \\
  3700 & octal \\ \cline{2-2}
  11111000000 & binary \\
  \hline \hline
  1984 & decimal \\
  \hline
\end{tabular}
\end{table}

Nullam libero arcu, placerat et eleifend nec, tincidunt vel eros. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec vel dui pulvinar, malesuada mauris ac, finibus neque. Phasellus imperdiet posuere sodales. Sed feugiat quis lorem id laoreet.

\fancytab{tabnew}{A caption}{%
\begin{tabular}{rl}
7C0 & hexadecimal \\
3700 & octal \\
11111000000 & binary \\
\midrule
1984 & decimal \\
\end{tabular}
}

Quisque blandit pretium suscipit. Nulla eget lectus rhoncus, maximus turpis sed, mattis sapien. Sed scelerisque metus dolor, sed elementum nisi laoreet eget. Aenean non tempus arcu. Praesent pharetra diam ut dictum dapibus. Etiam maximus orci at magna efficitur bibendum.

\fancytab[\centering]{tab}{A much much longer caption}{%
\begin{tabular}{l*{6}{c}r}
Team              & P & W & D & L & F  & A & Pts \\
\midrule
Manchester United & 6 & 4 & 0 & 2 & 10 & 5 & 12  \\
Celtic            & 6 & 3 & 0 & 3 &  8 & 9 &  9  \\
Benfica           & 6 & 2 & 1 & 3 &  7 & 8 &  7  \\
FC Copenhagen     & 6 & 2 & 1 & 3 &  5 & 8 &  7  \\
\end{tabular}
}
\end{document} 
Alenanno
  • 37,338
1

Following code shows a solution with tcolorbox package. As percusse suggested, this package can easily do this kind of fancy boxes.

With help of blend into initialization option, it's also very easy to integrate tcolorboxes in lists of figures or tables: the title is used as a caption and integrated into LOT and numeration follows table counter.

Instead of \begin{table} .... \caption{...} \end{table}, you use

\begin{mytable}[optional parameters]{mandatory table caption}
... table contents ....
\end{mytable}

An example with tables contents stolen from Alenanno`s answer could be:

\documentclass{article}
\usepackage{booktabs}
\usepackage[most]{tcolorbox}

\newtcolorbox[blend into=tables]{mytable}[2][]{%
    enhanced,
    float, 
    every float=\centering,
    capture=hbox, 
    title = #2, 
    attach boxed title to top left={%
        xshift=5mm, 
        yshift=-\tcboxedtitleheight/2, 
        yshifttext=-1mm},
    boxed title style={colback=red, sharp corners},
    colframe = red,
    colback = blue!20,  
    overlay = {\node[text=white, fill=red] at (frame.east) 
        {$\clubsuit$};},
    #1}

\begin{document}
\listoftables

\begin{table}
\centering
\begin{tabular}{|r|l|}
  \hline
  7C0 & hexadecimal \\
  3700 & octal \\ \cline{2-2}
  11111000000 & binary \\
  \hline \hline
  1984 & decimal \\
  \hline
\end{tabular}
\caption{First table}
\end{table}

\begin{mytable}{Second table}
\begin{tabular}{rl}
  7C0 & hexadecimal \\
  3700 & octal \\ 
  11111000000 & binary \\
  \midrule
  1984 & decimal \\
\end{tabular}
\end{mytable}

\begin{mytable}[colback=red!5]{Trird table}
\begin{tabular}{l*{6}{c}r}
Team              & P & W & D & L & F  & A & Pts \\
\midrule
Manchester United & 6 & 4 & 0 & 2 & 10 & 5 & 12  \\
Celtic            & 6 & 3 & 0 & 3 &  8 & 9 &  9  \\
Benfica           & 6 & 2 & 1 & 3 &  7 & 8 &  7  \\
FC Copenhagen     & 6 & 2 & 1 & 3 &  5 & 8 &  7  \\
\end{tabular}
\end{mytable}

\end{document}

enter image description here

Ignasi
  • 136,588