tabularx 鍵在 newtcolorbox 宣告的方塊中失敗

tabularx 鍵在 newtcolorbox 宣告的方塊中失敗

考慮以下程式碼:

\documentclass{article}
\usepackage{tabularx}
\usepackage{colortbl}
\usepackage{array}
\usepackage{tcolorbox}

\newtcolorbox{testbox}{
    width=.5\textwidth
}

\begin{document}
% \begin{testbox}                      %compilation fails when this is uncommented
%    \begin{tcolorbox}[tabularx={l},width=4cm]
%       test1 \\\hline
%       test2
%   \end{tcolorbox}
% \end{testbox}

\begin{tcolorbox}[width=.5\textwidth]
    \begin{tcolorbox}[tabularx={l},width=4cm]
        test1 \\\hline
        test2
    \end{tcolorbox}
\end{tcolorbox}
\end{document}

聲明testbox的vianewtcolorbox與手動新增選項的tcolorbox完全相同width=.5\textwidth

然而,雖然如果我將另一個帶有選項的框tabularx放入後者,它會按預期工作,但如果我將相同的框放入我的框,testbox它就不再編譯。

關於這裡發生的事情有任何線索嗎?

答案1

第一種情況失敗的原因是 hack fortabularx取得了錯誤的環境名稱。此名稱由選項存儲,並由外盒自動savedelimiter設定。testbox對於內部框,幾乎所有選項都設定回預設值 -savedelimiter是極少數保持當前值的選項之一(記錄為鍵reset)。

此範例透過直接使用修復savedelimiter

\documentclass{article}
\usepackage{tabularx}
\usepackage{colortbl}
\usepackage{array}
\usepackage{tcolorbox}

\newtcolorbox{testbox}{
    width=.5\textwidth
}

\begin{document}
 \begin{testbox}                      
    \begin{tcolorbox}[tabularx={l},width=4cm,savedelimiter=tcolorbox]
       test1 \\\hline
       test2
   \end{tcolorbox}
 \end{testbox}

\begin{tcolorbox}[width=.5\textwidth]
    \begin{tcolorbox}[tabularx={l},width=4cm]
        test1 \\\hline
        test2
    \end{tcolorbox}
\end{tcolorbox}
\end{document}

正如大衛卡萊爾(David Carlisle)所寫: 嵌套tabularx有點棘手。在這裡,我們嵌套tabularx在一個tcolorbox裡面tcolorbox......

相關內容