使 \maketitle 宏成为可选的

使 \maketitle 宏成为可选的

我看到了这篇文章https://tex.stackexchange.com/a/124995/230596 我定义了自己的标题以放入.sty文件中。如果我选​​择不包含我定义的宏之一,则编码方式 LaTex 会发出警告:LaTeX Error: There's no line here to end.

STY file

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{tmpmktitle}[2022/02/11 Template title]

\def\title#1{\def\@title{#1}}
\def\subject#1{\def\@subject{#1}}
\def\author{\def\@author{Author Name}}
\def\school#1{\def\@school{#1}}
\def\department{\def\@department{Department of something}}
\def\date#1{\def\@date{#1}}

\def\schoolA{This is School A}
\def\subjectA{--- This is subject A ---}

% redefine maketitle
\def\maketitle{%
    \thispagestyle{empty}%\vbox to \vsize{%
    \begin{center}%
        \vspace*{1cm}
        \LARGE{\bfseries \@title}\\\vskip .75em
        \Large{\itshape \@subject}\\
        \vspace{.7cm}%
       \large{\@author}\\ [.5\baselineskip]
        \normalsize{\@school}\\[.2\baselineskip]
        %\vspace{-.3em}
        \normalsize{\@department}\\
        \vspace{.7cm}
        \large{\@date}
    \end{center}%
\par\vspace{1cm}
}
\endinput

LaTeX doc

\documentclass{article}
\usepackage{kantlipsum}
\usepackage{tmpmktitle}

\title{This is a long title for the document in use}
\subject{\subjectA}
\author
\school{\schoolA}
\department

\date{\today}


\begin{document}
\maketitle

\kant [1]

\end{document}

答案1

您可以使用\ifcsname <csname>\endcsname...\fi来判断是否\<csname>存在。因此,下面将检查您要打印的每个与标题相关的宏,从而允许在元素存在与否之间进行判断。

在此处输入图片描述

\documentclass{article}

\makeatletter
% Start of tmpmktitle.sty

% Set some defaults that one can compare against later
\def\title#1{\def\@title{#1}}
\def\subject#1{\def\@subject{#1}}
\def\author#1{\def\@author{#1}}% \def\author{\def\@author{Author Name}}% 
\def\school#1{\def\@school{#1}}
\def\department#1{\def\@department{#1}}% \def\department{\def\@department{Department of something}}
\def\date#1{\def\@date{#1}}

% redefine maketitle
\def\maketitle{%
  \thispagestyle{empty}%
  \begin{center}%
    \ifcsname @title\endcsname
      {\LARGE\bfseries \@title\par}\vspace{0.75em}
    \fi
    \ifcsname @subject\endcsname
      {\Large\itshape \@subject\par}\vspace{.7cm}
    \fi
    \ifcsname @author\endcsname
      {\large\@author\par}\vspace{.5\baselineskip}
    \fi
    \ifcsname @school\endcsname
      {\normalsize\@school\par}\vspace{.2\baselineskip}
    \fi
    \ifcsname @department\endcsname
      {\normalsize\@department\par}\vspace{.7cm}
    \fi
    \ifcsname @date\endcsname
      {\large\@date\par}
  \end{center}%
  \vspace{1cm}
}
% End of tmpmktitle.sty
\makeatother

\title{This is a long title for the document in use}
\author{Author Name}
\school{This is School~A}
\subject{--- This is subject~A ---}
\department{Department of Something}
\date{\today}

\begin{document}

\maketitle

\end{document}

相关内容