开发者

New environment in latex using other environments, compiler doesn't find the \end

开发者 https://www.devze.com 2022-12-11 23:44 出处:网络
I\'m setting up a new environment for my latex document for consistent tab开发者_Go百科les. It looks like this:

I'm setting up a new environment for my latex document for consistent tab开发者_Go百科les. 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 doesn't seem to find the \end{table} though:

! LaTeX Error: \begin{table} on input line 23 ended by \end{document}.

Is there a way to avoid this?


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

The \@float and \end@float are LaTeX's internal commands for starting and ending the float environment.

You'll also want to follow Alexey's advice on the #2 parameter. Store it in the first part of your environment (\gdef\mycaption{#2}) and then recall it later \caption{\mycaption} in the second part. Put \def\mycaption{\relax} just before the \begin{defaultTable} line.

Also, since \@float and \end@float have @ signs in them, if this code is in the preamble of your document file (instead of say, a .sty file), you'll need to put \makeatletter before your \begin{defaultTable} and also \makeatother after \end{defaultTable}.


You can use #2 in the end if you use the xparse mechanism:

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


You can not use #2 in the last argument of the \newenvironment macros. You should use #1..#9 in the second argument only.

Save your #2 to \tempa (or any macros). And use \tempa in the caption.

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


I've has the same problem, and it is because of the "\end{tabularx}". The solution is:

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

So you define the rows as a parameter.

Regards, Eric


You could also just use a \newcommand similar to Eric's solution.

\documentclass{article}

\usepackage{tabularx}

% The table design.
\newcommand{\defaultTable}[2]{
    \begin{table}[h]
        \begin{tabularx}{\textwidth}{cc}
        Column A & Column B \\
        #2
        \end{tabularx}
        \caption{#1}
    \end{table}
}
\newcommand{\defaultTableRow}[2]{#1 & #2 \\}

\begin{document}

% The creation of a table.
\defaultTable{Example}{
  \defaultTableRow{bla}{0815}
  \defaultTableRow{blup}{0815}
}

\end{document}

New environment in latex using other environments, compiler doesn't find the \end

This will avoid both your problems (the missing \end{table} and the error when referencing arguments in the environments closing code) without much hassle.

In fact I also like the idea of separating the table design from the table data. Especially if you create multiple tables that need to look equal.

0

精彩评论

暂无评论...
验证码 换一张
取 消