当文本中的标题为小写字母时,如何将目录中的每个单词大写

当文本中的标题为小写字母时,如何将目录中的每个单词大写

我喜欢将章节、节和小节标题用小写字母表示,首字母不大写,例如在代码中写成“章节标题名称”而不是“章节标题名称”。但是,使用默认设置,在目录中显然会将其翻译为“章节标题名称”。

我知道我也可以在目录中使用小写字母,但我希望恢复为正常的大写文本。我很难做到这一点。我发现您可以使用然后使用 titletoc 包中的、和选项中的\usepackage{mfirstuc}命令。\capitalisewords\titlecontents{chapter}\titlecontents{section}\titlecontents{subsection}

但是,目录中第一个单词的首字母不是大写的,而后面每个单词的其他首字母都是大写的!奇怪的是,如果我将文本中章节、节和小节标题的第一个单词的首字母大写,那么目录中每个章节、节和小节的第一个单词的首字母现在确实是大写的。

我觉得这是 TeX 工作方式的基本内容,我需要调整其他东西。

请参阅下面的 MWE:

\documentclass[10pt,a4paper]{book}

\usepackage{titlesec,titletoc}

% In the following I ensure chapter, section and subsection headings are small caps via \scshape command
\titleformat{\chapter}[display]{\sf}{Chapter \thechapter}{2mm}{\scshape}
\titleformat{\section}[hang]{\rm\scshape}{\thesection}{4mm}{}
\titleformat{\subsection}[hang]{\rm\scshape}{\thesubsection}{4mm}{}

\usepackage{mfirstuc} % <--- this package used to capitalise first words via \capitalisewords command

\titlecontents{chapter}
[0pt]                                               
{}
{\contentsmargin{0pt}                               
    \thecontentslabel\enspace
    \capitalisewords} % <---
{\contentsmargin{0pt}\large}                        
{\titlerule*[.5pc]{.}\contentspage}                 
[]  

\titlecontents{section}
[0pt]                                               
{}
{\contentsmargin{0pt}                               
    \thecontentslabel\enspace
    \capitalisewords} % <---
{\contentsmargin{0pt}\large}                        
{\titlerule*[.5pc]{.}\contentspage}                 
[]     

\titlecontents{subsection}
[0pt]                                               
{}
{\contentsmargin{0pt}                               
    \thecontentslabel\enspace
    \capitalisewords} % <---
{\contentsmargin{0pt}\large}                        
{\titlerule*[.5pc]{.}\contentspage}                 
[]     

\begin{document}

\tableofcontents

\chapter{my first chapter} % if changed to 'My first chapter' then it works
\section{my first section} % if changed to 'My first section' then it works
\subsection{my first subsection} % if changed to 'My first subsection' then it works

\end{document}

输出: 输出

答案1

标题前插入了一些代码,导致无法\capitalisewords识别my为单词。

\capitalisefmtwords*命令能够正确解析它。

\documentclass[10pt,a4paper]{book}

\usepackage{titlesec,titletoc}

% In the following I ensure chapter, section and subsection headings are small caps via \scshape command
\titleformat{\chapter}[display]{\sf}{Chapter \thechapter}{2mm}{\scshape}
\titleformat{\section}[hang]{\rm\scshape}{\thesection}{4mm}{}
\titleformat{\subsection}[hang]{\rm\scshape}{\thesubsection}{4mm}{}

\usepackage{mfirstuc} % <--- this package used to capitalise first words via \capitalisewords command

\titlecontents{chapter}
[0pt]                                               
{}
{\contentsmargin{0pt}                               
    \thecontentslabel\enspace
    \capitalisefmtwords*} % <---
{\contentsmargin{0pt}\large}                        
{\titlerule*[.5pc]{.}\contentspage}                 
[]  

\titlecontents{section}
[0pt]                                               
{}
{\contentsmargin{0pt}                               
    \thecontentslabel\enspace
    \capitalisefmtwords*} % <---
{\contentsmargin{0pt}\large}                        
{\titlerule*[.5pc]{.}\contentspage}                 
[]     

\titlecontents{subsection}
[0pt]                                               
{}
{\contentsmargin{0pt}                               
    \thecontentslabel\enspace
    \capitalisefmtwords*} % <---
{\contentsmargin{0pt}\large}                        
{\titlerule*[.5pc]{.}\contentspage}                 
[]     

\begin{document}

\tableofcontents

\chapter{my first chapter} % if changed to 'My first chapter' then it works
\section{my first section} % if changed to 'My first section' then it works
\subsection{my first subsection} % if changed to 'My first subsection' then it works

\end{document}

答案2

您在文档中用小写字母书写部门标题,并用小写字母打印。为什么不直接将目录条目设置为小写字母呢?该tocloft软件包可让您做到这一点(以及许多其他目录调整)。

% capitaliseprob.tex  SE 587274

\documentclass[10pt,a4paper]{book}

\usepackage{comment}

%%%%%% PW code for setting the ToC
\usepackage{tocloft}

\renewcommand{\cfttoctitlefont}{\scshape} % small caps for ToC title
% In the book class, chapter entries in the ToC are  bold, no dotted leader
\renewcommand{\cftchapfont}{\scshape} % chapter titles in small caps
\renewcommand{\cftchapleader}{\cftdotfill{\cftdotsep}} % not bold but dotted
\renewcommand{\cftchappagefont}{} % not bold page number
\renewcommand{\cftsecfont}{\scshape} % small caps for section titles
\setlength{\cftsecindent}{0em} % no indentation of section entries
% and similary for subsections
\renewcommand{\cftsubsecfont}{\scshape}
\setlength{\cftsubsecindent}{0em}
%\usepackage{titletoc} % done via tocloft
%%%%% end of PW code

\usepackage{titlesec}

% In the following I ensure chapter, section and subsection headings are small caps via \scshape command
\titleformat{\chapter}[display]{\sf}{Chapter \thechapter}{2mm}{\scshape}
\titleformat{\section}[hang]{\rm\scshape}{\thesection}{4mm}{}
\titleformat{\subsection}[hang]{\rm\scshape}{\thesubsection}{4mm}{}

\begin{comment}
%%% comment out some of the OP's original code

\usepackage{mfirstuc} % <--- this package used to capitalise first words via \capitalisewords command

\titlecontents{chapter}
[0pt]                                               
{}
{\contentsmargin{0pt}                               
    \thecontentslabel\enspace
    \capitalisewords} % <---
{\contentsmargin{0pt}\large}                        
{\titlerule*[.5pc]{.}\contentspage}                 
[]  

\titlecontents{section}
[0pt]                                               
{}
{\contentsmargin{0pt}                               
    \thecontentslabel\enspace
    \capitalisewords} % <---
{\contentsmargin{0pt}\large}                        
{\titlerule*[.5pc]{.}\contentspage}                 
[]     

\titlecontents{subsection}
[0pt]                                               
{}
{\contentsmargin{0pt}                               
    \thecontentslabel\enspace
    \capitalisewords} % <---
{\contentsmargin{0pt}\large}                        
{\titlerule*[.5pc]{.}\contentspage}                 
[]     

\end{comment}

\begin{document}

\tableofcontents

\chapter{my first chapter} % if changed to 'My first chapter' then it works
\section{my first section} % if changed to 'My first section' then it works
\subsection{my first subsection} % if changed to 'My first subsection' then it works

\end{document}

在此处输入图片描述

相关内容