【问题标题】:New environment in latex using other environments, compiler doesn't find the \end使用其他环境的乳胶中的新环境,编译器找不到 \end
【发布时间】:2009-11-18 08:16:42
【问题描述】:

我正在为我的 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}
}

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

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

有没有办法避免这种情况?

【问题讨论】:

    标签: latex


    【解决方案1】:

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

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

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

    另外,由于 \@float\end@float@ 符号在其中,如果此代码在文档文件的序言中(而不是说,.sty 文件),你需要把\makeatletter 在您的\begin{defaultTable} 之前以及\makeatother\end{defaultTable} 之后。

    【讨论】:

      【解决方案2】:

      如果使用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}
      }
      

      【讨论】:

        【解决方案3】:

        您不能在\newenvironment 宏的最后一个参数中使用#2。您应该只在第二个参数中使用#1..#9。

        将您的 #2 保存到 \tempa(或任何宏)。并在标题中使用\tempa

        \newenvironment{defaultTable}[2]{
          \begin{table}[h]
          \def\tempa{#2}
          \noindent    
          \tabularx{\textwidth}{#1}    \specialrule{0.5pt}{10pt}{0pt} \rowcolor[gray]{.9}
        }{    
         \bottomrule     
         \endtabularx    
         \caption{\tempa}
         \end{table}
        }
        

        【讨论】:

        • 感谢提示,但它并没有解决问题,我仍然得到 \begin{table} 由 \end{document} 结束的错误
        • 使用序言编写文件的完整版本。
        【解决方案4】:

        我也有同样的问题,这是因为“\end{tabularx}”。解决办法是:

        \newenvironment{defaultTable}[3] {
        \begin{table}[h]
        \caption{#2}
        \noindent
        \begin{tabularx}{\textwidth}{#1}
        \specialrule{0.5pt}{10pt}{0pt} \rowcolor[gray]{.9}
        #3
        \bottomrule
        \end{tabularx}
        } {
        \end{table} }

        因此您将行定义为参数。

        问候, 埃里克

        【讨论】:

          猜你喜欢
          • 2022-01-17
          • 2017-08-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多