章节标题是否自动大写?

章节标题是否自动大写?

有没有什么办法可以让 LaTeX 自动将章节标题大写?

也就是说,我希望生成一个大写的标题:

\documentclass{article}
\begin{document}
\section{This should be in title case.}
But it isn't.
\end{document}

答案1

另一个解决方案是使用任何附加包来修改\section\subsection宏。可以使用以下命令轻松完成此操作

\makeatletter
\renewcommand{\section}{\@startsection {section}{1}{\z@}%
             {-3.5ex \@plus -1ex \@minus -.2ex}%
             {2.3ex \@plus .2ex}%
             {\normalfont\Large\scshape\bfseries}}

\renewcommand{\subsection}{\@startsection{subsection}{2}{\z@}%
             {-3.25ex\@plus -1ex \@minus -.2ex}%
             {1.5ex \@plus .2ex}%
             {\normalfont\large\scshape\bfseries}}

\renewcommand{\subsubsection}{\@startsection{subsubsection}{2}{\z@}%
             {-3.25ex\@plus -1ex \@minus -.2ex}%
             {1.5ex \@plus .2ex}%
             {\normalfont\normalsize\scshape\bfseries}}
\makeatother

章节、小节和小小节的标题将采用小写字母。

答案2

sectsty提供轻松挂钩到分段单元。\MakeUppercase将标题变成大写,而\titlecap(从titlecaps) 将其转换为标题大小写。

在此处输入图片描述

\documentclass{article}
\usepackage{sectsty}% http://ctan.org/pkg/sectsty
\usepackage{titlecaps}% http://ctan.org/pkg/titlecaps
\begin{document}
\tableofcontents
\sectionfont{\MakeUppercase}
\section{This should be in upper case}
It is!
\sectionfont{\titlecap}
\section{This should be in title case}
It is!
\end{document}

答案3

如果您使用hyperref\MakeUppercase将会破坏一些 ToC 超链接。您可以使用 latex3 功能作为 的替代\MakeUppercase,只需重新定义\section。MWE:

\documentclass{article}

\usepackage{xparse}
\usepackage{hyperref}

\ExplSyntaxOn
\let\oldsection\section
\RenewDocumentCommand{\section}{ s o m }{ % Capitalize section headings
    % \text_uppercase will replace \tl_upper_case in later l3token version
    \tl_set:Nx\section_upper{\cs_if_exist:NTF{\text_uppercase:n}
        {\text_uppercase:n{#3}}{\tl_upper_case:n{#3}}}
    \IfBooleanTF{#1}
        {\oldsection*{\section_upper}}
        {\IfNoValueTF{#2}
            {\oldsection{\section_upper}}
            {\oldsection[#2]{\section_upper}}}}
\ExplSyntaxOff

\makeatletter
% Always capitalize section headings in ToC (tex.stackexchange.com/a/156917)
\let\@oldcontentsline\contentsline
\def\contentsline#1#2{%
    \expandafter\ifx\csname l@#1\endcsname\l@section
        \expandafter\@firstoftwo
    \else
        \expandafter\@secondoftwo
    \fi
    {\@oldcontentsline{#1}{\MakeUppercase{#2}}}%
    {\@oldcontentsline{#1}{#2}}%
}
\makeatother

\begin{document}
\tableofcontents
\section{this should be in upper case}
It is!
\end{document}

相关内容