tcolorbox 中的 tabularx 和 hbox 选项问题

tcolorbox 中的 tabularx 和 hbox 选项问题

我在格式化时遇到问题,tcolorbox以便它包含一个表格并且其宽度适合表格的宽度。 我的代码的 MWE 如下:

\documentclass{standalone}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{colortbl}
\usepackage{array}
\usepackage{tikz}
\usepackage{tcolorbox}
\tcbuselibrary{skins, breakable}

\begin{document}
    \begin{tcolorbox}[skin = enhanced jigsaw, tabularx = {p{2cm}p{3cm}}, capture = hbox, breakable = false, title = {Test}, label = {test}, center]
        \textbf{Column 1} & \textbf{Column 2}\tabularnewline\midrule
        1 & 2\tabularnewline
        3 & 4\tabularnewline
        5 & 6\tabularnewline
    \end{tcolorbox}
\end{document}

尝试编译 LuaLaTeX 时会抛出以下错误:

Argument of \TX@get@body has an extra }. \end{tcolorbox}
Paragraph ended before \TX@get@body was complete. \end{tcolorbox}
Missing } inserted. \end{tcolorbox}

相同的代码在没有该hbox选项的情况下也可以编译,但我似乎找不到有关它与 之间不兼容性的任何信息tabularx。设置或取消设置明确的表格列宽(例如,使用p)不起作用。请指教。

答案1

我不确定真正的问题是什么。一开始就使用tabularx无列就是错误的。X

也许定义一种新的风格模型tabularx正是您正在寻找的:

\documentclass{standalone}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{colortbl}
\usepackage{array}
\usepackage{tikz}
\usepackage{tcolorbox}
\tcbuselibrary{skins, breakable}

\makeatletter
\tcbset{
  tabular/.style={
    boxsep=\z@,top=\z@,bottom=\z@,leftupper=\z@,rightupper=\z@,
    toptitle=1mm,bottomtitle=1mm,boxrule=0.5mm,
    before upper={\arrayrulecolor{tcbcol@frame}\def\arraystretch{1.1}%
      \tcb@hack@currenvir\tabular{#1}},
    after upper=\endtabular\arrayrulecolor{black}},
}
\makeatother

\begin{document}

\begin{tcolorbox}[
  skin = enhanced jigsaw,
  tabular = {p{2cm}p{3cm}},
  capture = hbox,
  breakable = false,
  title = {Test},
  label = {test},
  center
]
\textbf{Column 1} & \textbf{Column 2}\tabularnewline\midrule
1 & 2\tabularnewline
3 & 4\tabularnewline
5 & 6\tabularnewline
\end{tcolorbox}

\end{document}

在此处输入图片描述

答案2

那这个呢:

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{breakable}
\usepackage{tabularx, colortbl, booktabs}
\begin{document}

\begin{tcolorbox}[tabularx = {XX}, breakable = false, title = {Test}, label = {test}, center]
    \textbf{Column 1} & \textbf{Column 2}\tabularnewline\midrule
    1 & 2\tabularnewline
    3 & 4\tabularnewline
    5 & 6\tabularnewline
\end{tcolorbox}

\noindent X\dotfill X\par
\end{document}

在此处输入图片描述

这就是所要求的吗?

\documentclass{standalone}
\usepackage{booktabs}
%\usepackage{tabularx}
\usepackage{colortbl}
\usepackage{array}
\usepackage{tikz}
\usepackage{tcolorbox}
\tcbuselibrary{skins, breakable}

\begin{document}
    \begin{tcolorbox}[skin = enhanced jigsaw, breakable = false,
      capture=hbox, title = {Test}, label = {test}, center]
      \begin{tabular}{p{2cm}p{3cm}}
        \textbf{Column 1} & \textbf{Column 2}\tabularnewline\midrule
        1 & 2\tabularnewline
        3 & 4\tabularnewline
        5 & 6
      \end{tabular}
    \end{tcolorbox}
\end{document}

在此处输入图片描述

相关内容