如何创建 \newenvironment{name}[num]{before}{after} 并在 {after} 中带有参数

如何创建 \newenvironment{name}[num]{before}{after} 并在 {after} 中带有参数

我的表格全部格式如下:

\begin{table}[t]
    \centering 
    \begin{threeparttable}
        \caption{\label{tab:<my label>}<my table title>}
        \small%
        <the actual table>
        \begin{tablenotes}[para,flushleft]           
            \footnotesize{<the table notes>}
        \end{tablenotes}     
    \end{threeparttable}
\end{table}

我想创建一个新环境(这是我第一次这样做!)以便仅输入其中指示的部分<...>

我尝试了这个,但是没有用:

\documentclass[11pt,openright]{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[a4paper]{geometry}
\geometry{verbose,tmargin=3cm,bmargin=3.5cm,lmargin=4cm,rmargin=3cm,marginparwidth=70pt}
\usepackage{comment}
\usepackage{prettyref}
\usepackage{setspace}
\usepackage{fancyhdr}
\usepackage[normalem]{ulem}
\usepackage{setspace}
\usepackage{bigstrut}
\usepackage{array}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{dcolumn} 
\usepackage{multirow}
\renewcommand{\multirowsetup}{\centering} 
\usepackage[para,flushleft]{threeparttable}
\usepackage{makecell}
\usepackage[margin=10pt,font=small,labelfont=bf,labelsep=period,format=hang,indention=0cm]{caption}
\captionsetup[table]{position=above, belowskip=4pt}

\newenvironment{tabella}[3]{%before
    \begin{table}[t]
    \centering 
    \begin{threeparttable}[b]
    \caption{\label{tab:#1}#2}
    \small
}
{%after
    \begin{tablenotes}[para,flushleft]    
        \footnotesize{#3}
    \end{tablenotes}     
    \end{threeparttable}
    \end{table}
}

\begin{document}
% Example of working table without new environment
\begin{table}[t]
    \centering 
    \begin{threeparttable}
        \caption{\label{tab:mytab1}Example without new environment}
        \small%
        \begin{tabular}{@{}ccc@{}}
            \toprule 
            \addlinespace
            Ducks  & Lions & Penguins\\
            \addlinespace
            \midrule
            1& 2& 3\\
            Quack & Roar & ?\tnote{a}\\
            \bottomrule
        \end{tabular}
        \begin{tablenotes}[para,flushleft]           
            \footnotesize{\item[a] What is the call of the penguins?\\\emph{Source}: blah, blah, blah.}
        \end{tablenotes}     
    \end{threeparttable}
\end{table}

% Example of not working table with new environment
\begin{tabella}{mytab2}{Example with new environment}
    {\item[a] What is the call of the penguins?\\\emph{Source}: blah, blah, blah.}
    \begin{tabular}{@{}lll@{}}
        \toprule 
        \addlinespace
        Ducks  & Lions & Penguins\\
        \addlinespace
        \midrule
        1& 2& 3\\
        Quack & Roar & ?\tnote{a}\\
        \bottomrule
    \end{tabular}
\end{tabella}

\end{document}

答案1

environ您可以使用该包和以下内容解决您的问题:

\NewEnviron{tabella}[3]{%before
    \begin{table}[t]
    \centering 
    \begin{threeparttable}[b]
    \caption{\label{tab:#1}#2}
    \small
    \BODY
    \begin{tablenotes}[para,flushleft]    
       {\footnotesize #3}
    \end{tablenotes}     
    \end{threeparttable}
    \end{table}
}

答案2

 \def\zzzzz{{\footnotesize #3\par}}

在你的开始代码中,然后使用

\zzzzz

在您希望它出现的最后代码中。(注意,\footnotesize没有参数,因此我移动了括号。)

相关内容