The new latex environment, using other environments, the compiler does not find \ end

I am creating a new environment for my latex document for consistent tables. It looks like this:

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

It does not seem to find \ end {table}:

! LaTeX: \ begin {table} error in input line 23 ending with \ end {document}.

Is there any way to avoid this?

+5
source share
4 answers

Replace \begin{table}with \@float{table}and replace \end{table}with \end@float.

\@floatand \end@floatare internal LaTeX commands for starting and ending a floating point environment.

№2. (\gdef\mycaption{#2}) \caption{\mycaption} . \def\mycaption{\relax} \begin{defaultTable}.

, \@float \end@float @ , ( .sty), \makeatletter \begin{defaultTable}, \makeatother \end{defaultTable}.

+3

# 2 , xparse:

\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}
}
+4

#2 \newenvironment. # 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}
}
+3

I have the same problem and this is because of "\ end {tabularx}". Decision:

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

So, you define the strings as a parameter.

Regards, Eric

+2
source

All Articles