保存框内的 Tabularx 和 colortbl 发生冲突

保存框内的 Tabularx 和 colortbl 发生冲突

我的总体目标是在保存框内构建一个表格,这样我就可以轻松地用多个副本填充页面。有人建议使用 tabularx,效果很好,直到我还尝试使用 colortbl 包为某些行着色。

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

  1. 表 1:tabularx 和 colortbl 在保存框外很好地协同工作。
  2. 表 2:tabularx 在存储在 savebox 中时工作正常。
  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}

相关内容