仅当包含 etoolbox 包时,输出 tabularx 的自定义环境才会生成错误

仅当包含 etoolbox 包时,输出 tabularx 的自定义环境才会生成错误

我正在处理一个非常复杂的表,我想定义一个带有一些附加宏的环境,以使表创建代码更具语义。到目前为止,我已经定义了环境和宏,它们似乎可以工作,但是一旦将它们放入我的主文档中,我就会收到一些奇怪的错误。我进一步调查,似乎错误是由包引起的etoolbox。这是一个最小的例子:

\documentclass[12pt]{article}

\usepackage{tabularx}
\usepackage{etoolbox}


\title{MyTitle}
\author{MyAuthor}

\newenvironment{scenario}[1]
   { \newcommand{\entry}[1]{Number. & Test & ##1\\\hline}
    \tabularx{\linewidth}{|c|c|X|}
    \hline 
    \multicolumn{3}{|c|}{#1}\\
    \hline}
   {\multicolumn{3}{|c|}{}\\
    \hline
    \endtabularx}

\begin{document}
\maketitle

\section{MySection}
\subsection{MySubSection}

\begin{scenario}{Title}
    \entry{Hello LaTeX.SX}
    \entry{if i include the etoolbox package i get an error.}
\end{scenario}

\end{document}

这是 xelatex 输出:

This is XeTeX, Version 3.14159265-2.6-0.999991 (TeX Live 2019/Debian) (preloaded format=xelatex)
 restricted \write18 enabled.
entering extended mode
(./wewe.tex
LaTeX2e <2020-02-02> patch level 2
L3 programming layer <2020-02-14>
(/usr/share/texlive/texmf-dist/tex/latex/base/article.cls
Document Class: article 2019/12/20 v1.4l Standard LaTeX document class
(/usr/share/texlive/texmf-dist/tex/latex/base/size12.clo))
(/usr/share/texlive/texmf-dist/tex/latex/tools/tabularx.sty
(/usr/share/texlive/texmf-dist/tex/latex/tools/array.sty))
(/usr/share/texlive/texmf-dist/tex/latex/etoolbox/etoolbox.sty)
(/usr/share/texlive/texmf-dist/tex/latex/l3backend/l3backend-xdvipdfmx.def)
(./wewe.aux) (/usr/share/texlive/texmf-dist/tex/latex/base/ts1cmr.fd)
! Misplaced \omit.
\multispan ->\omit 
                   \@multispan 
l.28 \end{scenario}
                   
? ! Misplaced \span.
\sp@n ->\span 
              \omit \advance \@multicnt \m@ne 
l.28 \end{scenario}
                   
? ! Misplaced \omit.
\sp@n ->\span \omit 
                    \advance \@multicnt \m@ne 
l.28 \end{scenario}
                   
? ! Misplaced \span.
\sp@n ->\span 
              \omit \advance \@multicnt \m@ne 
l.28 \end{scenario}
                   
? ! Misplaced \omit.
\sp@n ->\span \omit 
                    \advance \@multicnt \m@ne 
l.28 \end{scenario}
                   
? ! Misplaced \noalign.
\hline ->\noalign 
                  {\ifnum 0=`}\fi \hrule \@height \arrayrulewidth \futurelet...
l.28 \end{scenario}
                   
? 
Overfull \hbox (0.4pt too wide) in paragraph at lines 28--28
[]|

Underfull \hbox (badness 10000) in paragraph at lines 28--28

[1] (./wewe.aux) )
Output written on wewe.pdf (1 page).
Transcript written on wewe.log.

我该如何解决这个问题?

答案1

etoolboxTeX Live 2019 中包含的在开始时引入了与部分环境end相关的问题。\multicolumn

您可以使用解决该问题\NewEnviron,但您确实应该升级您的 TeX Live。

\documentclass[12pt]{article}

\usepackage{tabularx}
\usepackage{etoolbox}
\usepackage{environ}

\title{MyTitle}
\author{MyAuthor}

\NewEnviron{scenario}[1]
 {%
  \newcommand{\entry}[1]{Number. & Test & ##1\\\hline}%
  \begin{tabularx}{\linewidth}{|c|c|X|}
  \hline 
  \multicolumn{3}{|c|}{#1}\\
  \hline
  \BODY
  \multicolumn{3}{|c|}{}\\
  \hline
  \end{tabularx}
 }

\begin{document}

\begin{scenario}{Title}
    \entry{Hello LaTeX.SX}
    \entry{if i include the etoolbox package i get an error.}
\end{scenario}

\end{document}

相关内容