乳胶使用其他环境的新环境,编译器找不到\ end

ars*_*bon 5 latex

我正在为我的乳胶文档设置一个新环境以获得一致的表格.它看起来像这样:

\newenvironment{defaultTable}[2] {
    \begin{table}[h]
    \noindent
    \tabularx{\textwidth}{#1}
    \specialrule{0.5pt}{10pt}{0pt} \rowcolor[gray]{.9}
} {
    \bottomrule 
    \endtabularx
    \caption{#2}
    \end{table}
}
Run Code Online (Sandbox Code Playgroud)

它似乎没有找到\ end {table}:

!LaTeX错误:输入行23上的\ begin {table}以\ end {document}结束.

有办法避免这种情况吗?

Jos*_*ght 5

如果使用xparse机制,最后可以使用#2:

\usepackage{xparse}
\NewDocumentEnvironment{defaultTable}{+m+m}{%
    \begin{table}[h]
    \noindent
    \tabularx{\textwidth}{#1}
    \specialrule{0.5pt}{10pt}{0pt} \rowcolor[gray]{.9}
} {%
    \bottomrule 
    \endtabularx
    \caption{#2}
    \end{table}
}
Run Code Online (Sandbox Code Playgroud)


god*_*byk 5

替换\begin{table}\@float{table}并替换\end{table}\end@float

和是 LaTeX 用于启动和结束浮动环境的内部命令\@float\end@float

您还需要遵循 Alexey 关于 #2 参数的建议。将其存储在环境的第一部分 ( \gdef\mycaption{#2}) 中,然后\caption{\mycaption}在第二部分中调用它。放在\def\mycaption{\relax}该行之前\begin{defaultTable}

另外,由于\@float和中\end@float有符号,如果此代码位于文档文件@(而不是文件)的序言中,则.sty需要\makeatletter将.\begin{defaultTable}\makeatother\end{defaultTable}