考虑以下代码,源自这答案并产生如本文底部所示的输出。
\documentclass[a4paper, 10pt]{article}
\usepackage[margin=2cm]{geometry}
\usepackage[listings,skins,theorems,breakable,most]{tcolorbox}
\usepackage{lipsum}
\usepackage{fancyhdr}
\usepackage{tabularx}
\usepackage{xcolor}
\usepackage{multicol}
\NewDocumentCommand{\tcbtab}{O{}m+m}{%
\begin{tcolorbox}[%
enhanced,
arc = 0pt,
outer arc = 0pt,
titlerule = 0pt,
boxsep = 0pt,
left = 0pt,
right = 0pt,
top=0pt,
colframe = black,
box align=center,
halign=center,
valign=center,
attach boxed title to top={xshift=0mm,yshift=0mm},
boxed title style={
enhanced,
colback = black,
arc=0pt,
outer arc=0pt,
},
listing only,
title = {\centering\makebox[\linewidth][c]{#2}},#1]
#3%
\end{tcolorbox}%
}
\begin{document}
\lipsum[1]
\tcbtab{Title}{
\begin{tabularx}{\linewidth}{|p{0.5\linewidth}|p{0.5\linewidth}|}
First column & Second column
\end{tabularx}
}
\lipsum[1]
\begin{multicols}{2}
\lipsum[1]
\tcbtab{Title}{
\begin{tabularx}{\linewidth}{|p{0.5\linewidth}|p{0.5\linewidth}|}
First column & Second column
\end{tabularx}
}
\lipsum[1-2]
\end{multicols}
\end{document}
现在我已经tcolorboxes
对齐并居中了,我想在其中放置表格。但是,当前方法没有给出预期的结果,因为列太宽了。
我希望能够指定一个tabularx
位置,我可以轻松指定p{0.2\something}*5
或类似的内容,如果我想要 5 个多行列,或者p{0.5\something}*2
,如果我想要 2 个多行列,或者p{0.2\something}|p{0.2\something}|c
如果我想要 2 个多行列和 1 个填充所有剩余空间的居中列。
语法可能有点不同,但我希望能够轻松设计盒装表格,而无需花费数小时手动对齐每列以使其看起来美观。并且结果应该与和兼容\multicolumn
。\multirow
如何实现这一点?(以及如何删除数组内部前后的垂直空间the tcolorbox
?)
答案1
使用tabularx
模式tcolorbox
(并删除那里的方框标题)。
\documentclass[a4paper, 10pt]{article}
\usepackage[margin=2cm]{geometry}
\usepackage{array}
\usepackage[table]{xcolor}
\usepackage[skins,theorems,breakable,most]{tcolorbox}
\usepackage{lipsum}
\usepackage{fancyhdr}
\usepackage{tabularx}
\usepackage{multicol}
\NewDocumentCommand{\tcbtab}{O{}m+m}{%
\begin{tcolorbox}[%
enhanced,
sharp corners,
outer arc = 0pt,
titlerule = 0pt,
boxsep=0pt,
left=0pt,
right=0pt,
top=0pt,
colframe = black,
box align=center,
halign=center,
valign=center,
attach boxed title to top={xshift=0mm,yshift=0mm},
boxed title style={
enhanced,
colback = black,
arc=0pt,
outer arc=0pt,
},
listing only,
title = {\centering\makebox[\linewidth][c]{#2}},#1]
#3%
\end{tcolorbox}%
}
\NewDocumentCommand{\tcbtabagain}{O{}m+m}{%
\begin{tcolorbox}[%
enhanced,
sharp corners,
right=0pt,
left=0pt,
titlerule = 0pt,
colframe = black,
box align=center,
halign=center,
valign=center,
% attach boxed title to top={xshift=0mm,yshift=0mm},
% boxed title style={
% enhanced,
% colback = black,
% arc=0pt,
% outer arc=0pt,
% },
listing only,
title = {\centering{#2}},
tabularx={|*{2}{X}|},
#1]
#3%
\end{tcolorbox}%
}
\begin{document}
\lipsum[1]
\tcbtab[tabularx={|X|X|}]{Title}{%
First column & Second column
}
\lipsum[1]
\begin{multicols}{2}
\lipsum[1]
\tcbtabagain{Title}{%
First column & Second column
}
\lipsum[1-2]
\end{multicols}
\end{document}