4

Is possible insert the table environment into the tcolorbox environment, made available by the package tcolorbox?

% !TEX program = pdflatex
% arara: pdflatex: {synctex: yes}
\documentclass[a4paper,11pt]{report}

% Generali
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel}
\usepackage{microtype}
\usepackage[italian]{varioref}
\usepackage{relsize}
\usepackage{lipsum}
\usepackage{tabularx}

% Riquadri
\usepackage{tcolorbox}
    \tcbuselibrary{theorems}

\begin{document}

\begin{tcolorbox}[adjusted title= Box1: Title.]
%\begin{table}[tb]
\begin{tabular}{cc}
a & b \\
c & d
\end{tabular}
%\end{table}
\end{tcolorbox}

\end{document}

Is there a way to insert a table with caption and label?

2

3 Answers 3

6

tcolorbox (as its name suggests) is a box; table is a float, and inside a box, a float is stuck ... which is why it doesn't work.

just use the tabular, and give it a caption using \captionof from capt-of.sty or (iirc) caption.sty (which has so many options i'm never quite sure).

6

tcolorbox-es can also float and you can declare tcolorboxes to be referenced as figures and tables. Following code declares a floating mytable box. The title will be preceded by Table # and label option can be used for references.

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{lmodern}
\usepackage{lipsum}

\newtcolorbox[blend into=tables]{mytable}[2][]{float=htb, halign=center,  title={#2}, every float=\centering, #1}

\begin{document}

\lipsum[1]

\begin{mytable}[float=t, label=myref]{This is a floating box}
\begin{tabular}{cc}
a & b \\
c & d
\end{tabular}
\end{mytable}

{\Large As you can see in table~\ref{myref} \dots}

\lipsum[2]
\end{document}

enter image description here

1
  • Thank you, this helped so much at the very moment!
    – Leon
    Commented Oct 4, 2022 at 10:49
3

You can't put a table inside a box. But you can do the reverse -- put a box inside a table:

% !TEX program = pdflatex
% arara: pdflatex: {synctex: yes}
\documentclass[a4paper,11pt]{report}

% Generali
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel}
\usepackage{microtype}
\usepackage[italian]{varioref}
\usepackage{relsize}
\usepackage{lipsum}
\usepackage{tabularx}

% Riquadri
\usepackage{tcolorbox}
    \tcbuselibrary{theorems}

\begin{document}
\begin{table}[tb]
\caption{This is table}
\label{tab:mytable}
\begin{tcolorbox}[adjusted title= Box1: Title.]
%\begin{table}[tb]
\begin{tabular}{cc}
a & b \\
c & d
\end{tabular}
%\end{table}
\end{tcolorbox}
\end{table}

As per Table~\ref{tab:mytable}, this works.

\end{document}

enter image description here

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .