创建新类 - 重用 \NewDocumentEnvironment

创建新类 - 重用 \NewDocumentEnvironment

我正在尝试为我的论文创建一个新类。我定义了一个\NewDocumentEnvironment抽象的页。

用法如下(#1 m、#2 o、#3 O):

\abstract{Your abstract}[number of blank pages][comment~on~the~last~page]

例如,\abstract{content}[3]给出一个 4 页的文档,第一页包含内容,其余两页为空白,第四页显示默认This~page~is~intentionally~left~empty!消息。

现在,假设我想致谢页面具有相同的样式。一种选择是复制所有内容并重命名。但更好的选择是什么?我可以创建一个在和\NewDocumentEnvironment中都使用的吗?abstractacknowledgement

班级testtwo.cls

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{testtwo} [2017/05/11 v0.1 A class to write PhD thesis]

\LoadClass{book}

\usepackage{xparse} % http://ctan.org/pkg/xparse
%% Abstract
{}
\ExplSyntaxOn
\NewDocumentEnvironment{abstract}{m o O{This~page~is~intentionally~left~empty!}} % https://tex.stackexchange.com/a/30992/38244
{
    \pagestyle{empty}
    \null\vfil
    \begin{center}
        \setlength{\parskip}{0pt}
        {\huge{\textit{Abstract}} \par}
        \bigskip
    \end{center}
        {\normalsize #1 \par}
        \vspace*{\fill}

    \newcount\tmp 


    \IfNoValueTF{#2} 
    {   %#2 is nothing 
        \pagebreak
    } 
    { % #2 is something
        {
            \tmp=0 % https://tex.stackexchange.com/a/89015/38244
            \loop
            \advance\tmp by 1
            \null\newpage 
            \ifnum\tmp<#2
            \repeat


        \IfNoValueTF{#3} 
        { %#3 is nothing!
            \pagebreak
        } 
        {   %#3 is something!
            \pagebreak
            \topskip0pt % https://tex.stackexchange.com/a/2327/38244
            \vspace*{\fill}
            \hspace*{\fill}
            \textit{#3} % Not sure why spaces aren't coming!
            \hspace*{\fill}
            \vspace*{\fill}
        }
        %#3 ends here   
        }
    }


}
{}
\ExplSyntaxOff

平均能量损失testtwo.tex

\documentclass{testtwo}

\begin{document}
    \abstract{something}[3]%[]
\end{document}

答案1

您应该决定是选择命令还是选择环境。

在我看来,最好的语法是与环境相结合的。

\documentclass[oneside]{book}
\usepackage{xparse}

\providecommand{\abstractname}{Abstract}
\providecommand{\acknowledgmentname}{Acknowledgment}

\ExplSyntaxOn
\NewDocumentEnvironment{abstract}{O{0}O{\c_pushpen_thesis_emptypage_tl}}
 {
  \pushpen_thesis_commonenv_start:n { \abstractname }
 }
 {
  \pushpen_thesis_commonenv_end:nn { #1 } { #2 }
 }
\NewDocumentEnvironment{acknowledgment}{O{0}O{\c_pushpen_thesis_emptypage_tl}}
 {
  \pushpen_thesis_commonenv_start:n { \acknowledgmentname }
 }
 {
  \pushpen_thesis_commonenv_end:nn { #1 } { #2 }
 }

\cs_new_protected:Nn \pushpen_thesis_commonenv_start:n
 {
  \clearpage
  \thispagestyle{empty}
  \vspace*{\fill}
  \begin{center}
  \setlength{\parskip}{0pt}% Why? It should always be zero!
  \huge\itshape #1
  \end{center}
  \par\bigskip
 }

\cs_new_protected:Nn \pushpen_thesis_commonenv_end:nn
 {
  \par\vspace*{\fill}
  \clearpage
  \prg_replicate:nn { #1 } % do #1 times the following
   {
    \vspace*{\fill}\thispagestyle{empty}
    {\centering #2\par}
    \vspace*{\fill}
    \clearpage
   }
 }

\tl_const:Nn \c_pushpen_thesis_emptypage_tl
 {
  This~page~intentionally~left~blank
 }

\ExplSyntaxOff

\begin{document}

\begin{abstract}[2][Nothing here]
This is the text of the abstract.
This is the text of the abstract.
This is the text of the abstract.
This is the text of the abstract.

More than one paragraph, too!
More than one paragraph, too!
More than one paragraph, too!
More than one paragraph, too!
More than one paragraph, too!
\end{abstract}

\begin{acknowledgment}
The author is grateful to his cat
for not walking on the keyboard
while jiofoaijo dwjoioa ewdiroenwe
\end{acknowledgment}

\chapter{This is where everything begins}

Some text.

\end{document}

公共部分是通过一些宏完成的,您可以将标题传递给这些宏。

相关内容