`tcolorbox` 和 `tabularx` - toprule 和設定衝突

`tcolorbox` 和 `tabularx` - toprule 和設定衝突

在將一些tabularx表格轉換為 tcolorboxed 表格時,我發現它tcolorbox做了很多未經「授權」的事情,但不能關閉。 Fi 它會改變表格和陣列拉伸的一般規則顏色。使用自訂的booktabs設定確實會破壞一致性。

主要的兩個問題我已經提過。因此有兩個問題(我在手冊中沒有找到任何設定):

  • 如何以非解決方法的方式關閉規則顏色的變更?進行兩次更改(一次一次tcolorbox,一次使用before upper app)並不是一個好的解決方案。
  • 如何關閉 的變更arraystretch?我還沒找到什麼好的解決方法。我真的需要修補這些內部 tcolorbox 巨集之一嗎?

還有什麼也很好:

  • 我不想更改所有有關內容的表格。因此,我真的很感激能夠使用以 開頭的表的第一行\toprule(目前會導致錯誤,我使用 tcolorbox 規則)的解決方法。

為了說明問題:

表問題

微量元素:

\documentclass{scrbook}
\usepackage{array,tabularx}
\usepackage[table]{xcolor}
\usepackage[most]{tcolorbox}
\usepackage{booktabs}

\renewcommand*{\arraystretch}{1.4}

\def\tabletitle#1{%
    \refstepcounter{table}Table\ \thetable\ --\ #1%
    \addcontentsline{lot}{section}{#1}%
}
\newtcolorbox{tablebox}[3][]{
    enhanced, sharp corners=all,colback=white,colframe=white,tabularx={#2},title={\tabletitle{#3}},label=#1,attach boxed title to top right={xshift=1cm,yshift=-2mm},before upper app={\arrayrulecolor{black}}
}

\begin{document}

\begin{tabularx}{.5\linewidth}{lX}
  \toprule \textbf{Stufe} & \textbf{Name}\\\midrule
    -1 & Teil\\
  0 & Kapitel\\
    1 & Abschnitt\\
    2 & Unterabschnitt\\
    3 & Unterunterabschnitt\\
    4 & Absatz\\
    5 & Unterabsatz\\\bottomrule
\end{tabularx}
\captionof{table}{Test}

\begin{tablebox}{lX}{Test}
  \textbf{Stufe} & \textbf{Name}\\\midrule%\toprule
    -1 & Teil\\
  0 & Kapitel\\
    1 & Abschnitt\\
    2 & Unterabschnitt\\
    3 & Unterunterabschnitt\\
    4 & Absatz\\
    5 & Unterabsatz\\\bottomrule
\end{tablebox}
\end{document}

更新:導致所有這些問題的程式碼(由tabularx鍵設定的選項):

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}#1%
   \tcb@hack@currenvir\tabularx{\linewidth}{#2}},
after upper=\endtabularx\arrayrulecolor{black}},

答案1

您自己給了答案,但您也寫道,您認為您的問題沒有得到答案。我不明白,為什麼不呢。

不管怎樣,既然你要求一種侵入性較小的方式,我可以給出兩個選項。兩種選擇都不tcolorbox用於頂級規則,而是\toprule在無害的地方插入一條。

  • tableboxA與您自己的答案非常相似,但定義了 amytablarx*而不是重新定義tabularx*。請注意\toprule\let\toprule\relax我的首要規則替代方案。

  • tableboxB不需要重新定義tabularx*mytabularx*都使用一些內部程式碼。數組拉伸和數組規則顏色直接設定。在你的問題中,你表示兩次定義顏色不是一個好的解決方案......但是,嗯,這是一個解決方案,我認為這也不是一個壞的解決方案......

在我看來,唯一煩人的就是\toprule不能像人們想像的那樣直接使用。在這裡,我們需要像您的答案或我的答案一樣的解決方法,但是調整顏色和數組拉伸(甚至兩次)或多或少是無害的:

\documentclass{scrbook}
\usepackage[table]{xcolor}
\usepackage{booktabs}
\usepackage{array}
\usepackage{tabularx}
\usepackage[most]{tcolorbox}

\renewcommand{\arraystretch}{1.4}

\makeatletter
\tcbset{%
  mytabularx*/.style 2 args={%
    boxsep=\z@,top=\z@,bottom=\z@,leftupper=\z@,rightupper=\z@,
    toptitle=1mm,bottomtitle=1mm,boxrule=0pt,
    before upper={#1\tcb@hack@currenvir\tabularx{\linewidth}{#2}\toprule\let\toprule\relax},            % through deleting: solve the arraystretch and color issue
    after upper=\endtabularx}
}
\makeatother

\newtcolorbox[use counter*=table]{tableboxA}[3][]{%
  enhanced jigsaw, sharp corners=all, colback=white,colframe=black,
  title={Table\ \thetable\ --\ #3},list text={#3},add to list={lot}{section},label=#1,
  mytabularx*={}{#2},
  attach boxed title to top right={xshift=1cm,yshift=2mm},
}

\newtcolorbox[use counter*=table]{tableboxB}[3][]{%
  blank, sharp corners=all, colback=white,colframe=black,
  title={Table\ \thetable\ --\ #3},list text={#3},add to list={lot}{section},label=#1,
  tabularx*={\arrayrulecolor{black}\renewcommand{\arraystretch}{1.4}}{#2},
  before upper app={\toprule\let\toprule\relax},
  boxrule=0pt,
  attach boxed title to top right={xshift=1cm,yshift=2mm},
}

\begin{document}

\captionof{table}{Test}
\begin{tabularx}{\linewidth}{lX}
  \toprule \textbf{Stufe} & \textbf{Name}\\\midrule
    -1 & Teil\\
    0 & Kapitel\\
    1 & Abschnitt\\
    2 & Unterabschnitt\\
    3 & Unterunterabschnitt\\
    4 & Absatz\\
    5 & Unterabsatz\\\bottomrule
\end{tabularx}

\vskip2\baselineskip

\begin{tableboxA}{lX}{Test}
  \toprule \textbf{Stufe} & \textbf{Name}\\\midrule
    -1 & Teil\\
    0 & Kapitel\\
    1 & Abschnitt\\
    2 & Unterabschnitt\\
    3 & Unterunterabschnitt\\
    4 & Absatz\\
    5 & Unterabsatz\\\bottomrule
\end{tableboxA}

\vskip2\baselineskip

\begin{tableboxB}{lX}{Test}
  \toprule \textbf{Stufe} & \textbf{Name}\\\midrule
    -1 & Teil\\
    0 & Kapitel\\
    1 & Abschnitt\\
    2 & Unterabschnitt\\
    3 & Unterunterabschnitt\\
    4 & Absatz\\
    5 & Unterabsatz\\\bottomrule
\end{tableboxB}

\end{document}

答案2

以下是一種解決方法(如果有人知道侵入性較小的方法,請在此處發布)。它確實明確不能解決問題。它能做什麼:

  • 重新定義tabularx樣式以剔除所有不需要的和不需要的程式碼段
  • \topruleto\relax然後用盒子規則模擬頂規則

表x

代碼:

\documentclass{scrbook}
\usepackage[table]{xcolor}
\usepackage{booktabs}
\usepackage{array}
\usepackage{tabularx}
\usepackage[most]{tcolorbox}

\renewcommand{\arraystretch}{1.4}

\makeatletter
\tcbset{%
  tabularx*/.style 2 args={%
    boxsep=\z@,top=\z@,bottom=\z@,leftupper=\z@,rightupper=\z@,
    toptitle=1mm,bottomtitle=1mm,boxrule=0pt, toprule=1pt, top=2.25pt,        % "solve" toprule issue (i)
    before upper={#1\tcb@hack@currenvir\tabularx{\linewidth}{#2}},            % through deleting: solve the arraystretch and color issue
    before upper app = {\let\toprule\relax},                                  % "solve" toprule issue (ii)
    after upper=\endtabularx}
}
\makeatother

\newtcolorbox[use counter*=table]{tablebox}[3][]{%
  enhanced jigsaw, sharp corners=all, colback=white,colframe=black,
  title={Table\ \thetable\ --\ #3},list text={#3},add to list={lot}{section},label=#1,
  tabularx*={}{#2},
  attach boxed title to top right={xshift=1cm,yshift=2mm},
}

\begin{document}

\captionof{table}{Test}
\begin{tabularx}{\linewidth}{lX}
  \toprule \textbf{Stufe} & \textbf{Name}\\\midrule
    -1 & Teil\\
    0 & Kapitel\\
    1 & Abschnitt\\
    2 & Unterabschnitt\\
    3 & Unterunterabschnitt\\
    4 & Absatz\\
    5 & Unterabsatz\\\bottomrule
\end{tabularx}

\vskip2\baselineskip

\begin{tablebox}{lX}{Test}
  \toprule \textbf{Stufe} & \textbf{Name}\\\midrule
    -1 & Teil\\
    0 & Kapitel\\
    1 & Abschnitt\\
    2 & Unterabschnitt\\
    3 & Unterunterabschnitt\\
    4 & Absatz\\
    5 & Unterabsatz\\\bottomrule
\end{tablebox}
\end{document}

相關內容