保存箱內的 Tabularx 和 colortbl 衝突

保存箱內的 Tabularx 和 colortbl 衝突

我的總體目標是在保存箱內建立一個表格,以便我可以輕鬆地用多個副本填充頁面。有人建議使用 tabularx,它效果很好,直到我也嘗試使用 colortbl 套件來為某些行著色。

在下面的 MWE 中,請注意...

  1. 表 1:tabularx 和 colortbl 在保存箱之外可以很好地協同工作。
  2. 表 2:當儲存在保存箱中時,tabularx 運作得很好。
  3. 表 3:嘗試將表 1 儲存在保存箱中會導致「未定義的控制序列」錯誤。

特別值得注意的是,在控制台上選擇“s”(跳過)選項會產生一個已編譯的文檔,其中所有三個表似乎都已正確呈現。因此,雖然我可以排版文檔,但它讓我想知道這個問題是否有「修復」。

微量元素:

\documentclass{article}
\usepackage{colortbl}
\usepackage{tabularx}
\usepackage{xcolor}

\newsavebox{\tabularxInBox}
\sbox{\tabularxInBox}{
    \begin{tabularx}{\textwidth}{cX}
        Col 1 & Col 2\\
        \hline
        Row 1& No Colored Background\\
        Row 2& No Colored Background\\
    \end{tabularx}
}

\newsavebox{\tabularxAndColortblInBox}
\sbox{\tabularxAndColortblInBox}{
    \begin{tabularx}{\textwidth}{cX}
        Col 1 & Col 2\\
        \hline
        Row 1& No Colored Background\\
        \rowcolor{lightgray}
        Row 2& Colored Background\\
\end{tabularx}
}

\begin{document}
No conflict with tabularx and colortbl normally:\\

    \begin{tabularx}{\textwidth}{cX}
        Col 1 & Col 2\\
        \hline
        Row 1& No Colored Background\\
        \rowcolor{lightgray}
        Row 2& Colored Background\\
\end{tabularx}
\vskip 1cm

No conflict with tabularx and sbox:\\

\usebox{\tabularxInBox}
\vskip 1cm

However, using both tabularx and colortble inside an \sbox throws an error:\\
\usebox{\tabularxAndColortblInBox}

\end{document}

答案1

排版直到 才完全設定\begin{document}。有時可以將簡單文字保存在序言中的 sbox 中,但在這裡不行:

\documentclass{article}
\usepackage{colortbl}
\usepackage{tabularx}
\usepackage{xcolor}

\newsavebox{\tabularxInBox}
\newsavebox{\tabularxAndColortblInBox}

\begin{document}

\sbox{\tabularxInBox}{%%%dont forget  eol
    \begin{tabularx}{\textwidth}{cX}
        Col 1 & Col 2\\
        \hline
        Row 1& No Colored Background\\
        Row 2& No Colored Background\\
    \end{tabularx}%%%dont forget  eol
}


\sbox{\tabularxAndColortblInBox}{%%%dont forget  eol
    \begin{tabularx}{\textwidth}{cX}
        Col 1 & Col 2\\
        \hline
        Row 1& No Colored Background\\
        \rowcolor{lightgray}
        Row 2& Colored Background\\
\end{tabularx}%%%dont forget  eol
}


No conflict with tabularx and colortbl normally:\\
    \begin{tabularx}{\textwidth}{cX}
        Col 1 & Col 2\\
        \hline
        Row 1& No Colored Background\\
        \rowcolor{lightgray}
        Row 2& Colored Background\\
\end{tabularx}
\vskip 1cm

No conflict with tabularx and sbox:\\
\usebox{\tabularxInBox}
\vskip 1cm

However, using both tabularx and colortble inside an \verb|\sbox| throws an error:\\
\usebox{\tabularxAndColortblInBox}

\end{document}

相關內容