新表格环境中的错误(失控参数和错位对齐制表符和)

新表格环境中的错误(失控参数和错位对齐制表符和)

我在序言中定义了以下新环境来创建编号的表格定义表:

\usepackage{etoolbox}

\newenvironment{numdeftable}
{
\preto\tabular{\setcounter{magicrownumbers}{0}}
\newcounter{magicrownumbers}
\newcommand\rownumber{\stepcounter{magicrownumbers}\arabic{magicrownumbers}}
\vspace{1.5pt}
    \begin{tabular}{@{\makebox[3em][l]{\rownumber.\space}}ll}
    \end{tabular}
}

但是,当我尝试编译/构建时,它失败了,并且我的日志中出现以下错误:

Runaway argument?
{ \preto \tabular {\setcounter {magicrownumbers}{0}} \newcounter {mag\ETC.
! File ended while scanning use of \@newenv.
<inserted text> 
            \par 
l.3 \input{../../../template/common/preamble}
                                         
I suspect you have forgotten a `}', causing me
to read past where you wanted me to stop.
I'll try to recover; but if the error is serious,
you'd better type `E' or `X' now and fix your file.

因此,我在文档中将环境称为numdeftable

\begin{numdeftable}
    Term1 & Definition of term 1.\\
    Term2 & Definition of term 2.\\
\end{numdeftable}

抛出

! LaTeX Error: Environment numdeftable undefined.

在尝试在环境中实现相同功能之前,我的文档中含有以下内容:

\preto\tabular{\setcounter{magicrownumbers}{0}}
\newcounter{magicrownumbers}
\newcommand\rownumber{\stepcounter{magicrownumbers}\arabic{magicrownumbers}}

\vspace{1.5pt}

\begin{tabular}{@{\makebox[3em][l]{\rownumber.\space}}ll}
    {[\texttt{Term 1}]} & Definition of term 1.\\
    {[\texttt{Term 2}]} & Definition of term 2.\\
\end{tabular}

结果如下:

1. [Term 1] Definition of term 1.
2. [Term 2] Definition of term 2.

这里可能出了什么问题?我该如何修复?任何指示都会很有帮助。

更新

从以下答案来看,错误Runaway argument?不再存在,这意味着新的环境定义工作正常,但是! Misplaced alignment tab character &.当我使用环境时遇到错误numdeftable

\begin{numdeftable}
    Term1 & Definition of term 1.\\
    Term2 & Definition of term 2.\\
\end{numdeftable}

这让人困惑。我是否需要添加/修改(显然默认制表符不起作用)@egreg's回答为了numdeftable识别我的行格式?

答案1

您的代码存在一些缺陷。

  1. 您不应该\newcounter{magicrownumbers}在定义主体中执行此操作,因为每次numdeftable使用时它都会分配一个新的计数器。

  2. \makebox\protect在表格序言中编辑。

  3. 您的定义太严格,因为它只允许两列表格。

  4. 没有必要使用\preto

重新定义,其中表格前言变得更简单,并且取决于指定表格列的参数。

\documentclass{article}

\newenvironment{numdeftable}[1]
 {% first of all reset the counter
  \setcounter{magicrownumber}{0}%
  \begin{tabular}{@{\rownumber}#1@{}}
 }
 {\end{tabular}}
\newcounter{magicrownumber}
\DeclareRobustCommand{\rownumber}{%
  \makebox[3em][l]{\stepcounter{magicrownumber}\themagicrownumber.}%
}

\begin{document}

\begin{numdeftable}{ll}
    Term1 & Definition of term 1.\\
    Term2 & Definition of term 2.\\
\end{numdeftable}

\end{document}

在此处输入图片描述

答案2

以下应产生预期的输出:

\documentclass{article}
\usepackage{etoolbox}
\usepackage{array}

\preto\tabular{\setcounter{magicrownumbers}{0}}
\newcounter{magicrownumbers}
\newcommand\rownumber{\stepcounter{magicrownumbers}\arabic{magicrownumbers}}

\newenvironment{numdeftable}
{\begin{tabular}{@{\makebox[3em][l]{\rownumber.\space}}ll}}{\end{tabular}}

\begin{document}

\begin{numdeftable}
    Term1 & Definition of term 1.\\
    Term2 & Definition of term 2.\\
\end{numdeftable}

\medskip

\begin{numdeftable}
    Term1 & Definition of term 1.\\
    Term2 & Definition of term 2.\\
\end{numdeftable}

\end{document}

相关内容