4

i use code from topic: How to make a Table/tabular appears in a tcolorbox?

But error "Not in outer par mode" and table does not appear.

Minimal Code:

\documentclass[12pt,a4paper]{article}
\usepackage{mwe}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage[x11names,table]{xcolor} % dddd%
\usepackage{eurosym}

\usepackage{datetime}
\settimeformat{ampmtime}

\usepackage{hyperref}
\hypersetup{colorlinks=true, urlcolor=blue}

\usepackage{graphicx,subcaption}
\usepackage{tcolorbox}

\begin{document}

\begin{tcolorbox}[colback=gray!5!white,colframe=gray!75!black,title=\large{In the next lines I'm trying to insert the same in a box}]

\begin{table}[h!]
\centering
\begin{tabular}{|c|c|c|c|}
  \hline
  % after \\: \hline or \cline{col1-col2} \cline{col3-col4} ...
  a& b & T/F & dif\\ \hline
1 & 2 & False & -1\\
2 & 4 & False & -2\\
  \hline
\end{tabular}\\
\caption{Caption Table}\label{tab:table-out}
\end{table}
\end{tcolorbox}

\end{document}

enter image description here How can i fix it? Thanks

latexforti
  • 2,091

3 Answers3

5

tcolorbox allows to convert your figures and tables into nice floating colourful boxes. blend into=tables option uses the tcolorbox title as caption and adds the table counter and the caption into listoftables. If you prefer that tcolorbox adjust to table size, capture=hbox does it.

\documentclass[12pt,a4paper]{article}

\usepackage{tcolorbox}
\usepackage{hyperref}

\hypersetup{colorlinks=true, urlcolor=blue}

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

\begin{document}

\listoftables

\begin{mytable}{A nice floating table in tcolorbox format}
\begin{tabular}{|c|c|c|c|}
  \hline
  % after \\: \hline or \cline{col1-col2} \cline{col3-col4} ...
  a& b & T/F & dif\\ \hline
1 & 2 & False & -1\\
2 & 4 & False & -2\\
  \hline
\end{tabular}
\end{mytable}

\begin{mytable}[capture=hbox, colback=red!10, colframe=red!40!black]{Another nice floating table in tcolorbox format}
\begin{tabular}{|c|c|c|c|}
  \hline
  % after \\: \hline or \cline{col1-col2} \cline{col3-col4} ...
  a& b & T/F & dif\\ \hline
1 & 2 & False & -1\\
2 & 4 & False & -2\\
  \hline
\end{tabular}
\end{mytable}

\end{document}

enter image description here

Ignasi
  • 136,588
  • 2
    Nice solution in the tcolorbox spirit, but your screenshot doesn't correspond to your code. Indeed, your code has capture=hbox unconditionally in the options for \newtcolorbox[blend into=tables]{mytable}{...}, thus both boxes have capture=hbox. Besides, I would simplify the preamble a lot: `\documentclass{article} \usepackage{tcolorbox}

    \newtcolorbox[blend into=tables]{mytable}[2][]{ float=htb, title={#2}, every float=\centering, #1}is enough for the sample code inside thedocument` environment.

    – frougon Aug 05 '19 at 09:24
  • @frougon You're right. I modified the code on my computer but I did no copy the whole code on this site. I've corrected it. About the preamble, you are also right, but I just used the original one. – Ignasi Aug 05 '19 at 09:33
  • 2
    Users often write their whole preamble in the questions, but I generally clean these up when answering (unless there is really document contents that depends on the preamble). Otherwise, it's really difficult to exploit answers with an overly complex preamble: it takes time to filter out what is useless, and the users who need answers most don't even know they have to do so nor how to do it; they will copy huge preambles to their documents, and in the end they will load 100 packages, of which they use 10. You know the problems this creates. – frougon Aug 05 '19 at 09:41
  • Regarding this edit, didn't you want before upper rather than after upper? – frougon Aug 05 '19 at 09:46
  • @frougon Ups! you're right again! Thank you. – Ignasi Aug 05 '19 at 10:18
  • Okay, thanks for the edit. I gave you my +1 now, but you still have unneeded hyperref (and a4paper and 12pt)... – frougon Aug 05 '19 at 10:32
4

enter image description here

You should not insert float table into box. Remove it and for caption use \captionof (defined in the caption and in capt-of package) command:

\documentclass[12pt,a4paper,
               x11names,table]{article}
\usepackage{tcolorbox}
\usepackage[skip=1ex]{caption}

\begin{document}

\begin{tcolorbox}[colback=gray!5!white,colframe=gray!75!black,title=\large{In the next lines I'm trying to insert the same in a box}]
\centering
\begin{tabular}{|c|c|c|c|}
  \hline
  % after \\: \hline or \cline{col1-col2} \cline{col3-col4} ...
  a& b & T/F & dif\\ \hline
1 & 2 & False & -1\\
2 & 4 & False & -2\\
  \hline
\end{tabular}\\
\captionof{figure}{Caption Table}\label{tab:table-out}
    \end{tcolorbox}
\end{document}
Zarko
  • 296,517
2

You want to float the tcolorbox:

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage[x11names,table]{xcolor} % dddd%
\usepackage{eurosym}

\usepackage{datetime}
\usepackage{graphicx,subcaption}
\usepackage{tcolorbox}


\usepackage{hyperref}

\hypersetup{colorlinks=true, urlcolor=blue}
\settimeformat{ampmtime}

\begin{document}

\begin{table}[htp!]
\begin{tcolorbox}[
  colback=gray!5!white,
  colframe=gray!75!black,
  title=\large{In the next lines I'm trying to insert the same in a box}
]
\centering
\begin{tabular}{|c|c|c|c|}
  \hline
  % after \\: \hline or \cline{col1-col2} \cline{col3-col4} ...
  a& b & T/F & dif\\ \hline
1 & 2 & False & -1\\
2 & 4 & False & -2\\
  \hline
\end{tabular}

\caption{Caption Table}\label{tab:table-out}
\end{tcolorbox}
\end{table}

\end{document}

enter image description here

And no, you don't want just h!, because it's highly unlikely that a table will fit at the “right spot”. Don't worry if it goes somewhere else, see How to influence the position of float environments like figure and table in LaTeX?

egreg
  • 1,121,712