3

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

2

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}
4

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

  • 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 the document environment. – frougon Aug 5 at 9: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 5 at 9: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 5 at 9:41
  • Regarding this edit, didn't you want before upper rather than after upper? – frougon Aug 5 at 9:46
  • @frougon Ups! you're right again! Thank you. – Ignasi Aug 5 at 10:18
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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.