
我使用主題中的程式碼:如何使表格/表格出現在 tcolorbox 中?
但錯誤「不在外部標準模式」且表格沒有出現。
最小程式碼:
\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}
答案1
您不應該將浮動插入table
盒子中。刪除它並供標題使用(在和 包中\captionof
定義)命令:caption
capt-of
\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}
答案2
tcolorbox
允許將您的圖形和表格轉換成漂亮的浮動彩色盒子。blend into=tables
選項使用tcolorbox
標題作為標題,並將表格計數器和標題新增至listoftables
。如果您希望 tcolorbox 調整為表格大小,capture=hbox
請這樣做。
\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}
答案3
你想要浮動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}
不,您不僅僅想要h!
,因為桌子不太可能適合“正確的位置”。不用擔心它是否會去其他地方,請參閱如何影響 LaTeX 中圖形和表格等浮動環境的位置?