美文网首页LaTex设计
LaTeX 自定义修正 \title 和 \maketitle

LaTeX 自定义修正 \title 和 \maketitle

作者: 千羽之城88 | 来源:发表于2018-09-14 21:14 被阅读672次

    使用到的命令

    • \makeatletter ... makeatother
    • \renewcommand*
    • \maketitle

    随着最近使用 \LaTeX 增多,感觉默认的Article的样式有些太单调。比如:

    image.png

    基本概念

    我们先看看精简的\maketitle的定义:

    \if@titlepage{ % 针对不分栏的情况,默认
      \newcommand\maketitle{
          \begin{titlepage}
          \@title\par
          \end{titlepage}
        }
    } else { % 针对页面分栏的情况
      \newcomand\maketitle{
        \begin{group}
          \@title\par
        \end{group}
    }
    

    我们的目的是缩减一些margin,不放作者和时间行。注意一点:含有 @ 的命令是内部命令,不需要立即执行,需要配合 \makeatletter ... \makeatother延迟,之后再执行。

    解决方案是

    \makeatletter % change default title style
    \renewcommand*\maketitle{%
        \begin{center}% 居中标题
            \bfseries % 默认粗体
            {\LARGE \@title \par} % LARGE字号
            \vskip 1em% %%%  标题下面只有1em的缩进或margin
            {\global\let\author\@empty}%
            {\global\let\date\@empty}%
            \thispagestyle{empty} %  不设置页面样式
        \end{center}%
      \setcounter{footnote}{0}%
    }
    \makeatother
    
    image.png

    这样的方式有点粗暴,还有更简单的方案,就是 \title\date\author 空置,然后另外定义一个标题,然后放到正文中。我的方案有点 hard,但是了解一下内部命令,算做是一种学习吧。

    article.cls\maketitle 的定义:

     \if@titlepage
     \newcommand\maketitle{\begin{titlepage}%
     \let\footnotesize\small
     \let\footnoterule\relax
     \let \footnote \thanks
     \null\vfil
     \vskip 60\p@
     \begin{center}%
       {\LARGE \@title \par}%
       \vskip 3em%
       {\large
        \lineskip .75em%
         \begin{tabular}[t]{c}%
           \@author
         \end{tabular}\par}%
         \vskip 1.5em%
       {\large \@date \par}%       % Set date in \large size.
     \end{center}\par
     \@thanks
     \vfil\null
     \end{titlepage}%
     \setcounter{footnote}{0}%
     \global\let\thanks\relax
     \global\let\maketitle\relax
     \global\let\@thanks\@empty
     \global\let\@author\@empty
     \global\let\@date\@empty
     \global\let\@title\@empty
     \global\let\title\relax
     \global\let\author\relax
     \global\let\date\relax
     \global\let\and\relax
    }
    \else
    \newcommand\maketitle{\par
     \begingroup
       \renewcommand\thefootnote{\@fnsymbol\c@footnote}%
       \def\@makefnmark{\rlap{\@textsuperscript{\normalfont\@thefnmark}}}%
       \long\def\@makefntext##1{\parindent 1em\noindent
               \hb@xt@1.8em{%
                   \hss\@textsuperscript{\normalfont\@thefnmark}}##1}%
       \if@twocolumn
         \ifnum \col@number=\@ne
           \@maketitle
         \else
           \twocolumn[\@maketitle]%
         \fi
       \else
         \newpage
         \global\@topnum\z@   % Prevents figures from going at top of page.
         \@maketitle
       \fi
       \thispagestyle{plain}\@thanks
     \endgroup
     \setcounter{footnote}{0}%
     \global\let\thanks\relax
     \global\let\maketitle\relax
     \global\let\@maketitle\relax
     \global\let\@thanks\@empty
     \global\let\@author\@empty
     \global\let\@date\@empty
     \global\let\@title\@empty
     \global\let\title\relax
     \global\let\author\relax
     \global\let\date\relax
     \global\let\and\relax
    }
    

    参考

    不需要专门的说明吧,我现在就可以说清楚了
    \makeatletter就是把@当作普通字母处理,反之\makeatother就是把@当作一般符号处理,由于\LaTeX内部命令有@,所以需要\makeatletter@当作字母,每个字符都有分类(可以用\catcode改)的,你可以参看TeXbook

    http://bbs.ctex.org/forum.php?mod=viewthread&tid=26525&highlight=makeatletter

    \makeatletter changes the category code of '@' character to 11
    (which is the catcode of ordinarary characters a-z,A-Z).
    \makeatother reverts this to its original catcode of 12.

    Knuth assigns a category code for each and every character like:

    • 0 for escape \,
    • 1 for begining of a group {,
    • 2 for end of group },
    • 3 for math shift $,
    • 4 for alignmet tab &,
    • 5 for end of line,
    • 6 for paramter #,
    • 7 for superscript ^,
    • 8 for subscript _,
    • 9 for ignored character,
    • 10 for space,
    • 11 for letters,
    • 13 for active character ~,
    • 14 for comment character %,
    • 15 for invalid character
    • 12 for characters other than the above.

    https://www.tug.org/pipermail/tugindia/2002-January/000178.html

    相关文章

      网友评论

        本文标题:LaTeX 自定义修正 \title 和 \maketitle

        本文链接:https://www.haomeiwen.com/subject/cwjagftx.html