我想将所有章节标题中每个单词的首字母自动转换为大写。可以吗?
IE
\section{This is my first section}
第 1 节。这是我的第一节
答案1
我不确定这种方法是否会对所有部分自动设置标题大写,但它对给出的示例有效。查看文档titlecaps
以了解如何在其参数中处理不同类型的非标准文本。
请注意它如何处理“排除词”列表、字体样式、字体大小变化以及变音符号和国家符号等\ae
。
\documentclass{article}
\usepackage{titlecaps}
\let\svsection\section
\def\section#1{\svsection{\titlecap{#1}}}
\begin{document}
\section{This is my first section}
Tah dah But if you didn't like ``my,'' ``for,'' or ``an'' capitalized, except as the first word:
\Addlcwords{my for an}
\section{for an \ae ncore, my \textit{section} for \small \c consideration}
done.
\end{document}
\tableofcontents
更新:我确实发现,除非小心谨慎,否则上面的部分重新定义将会中断。如果这是您工作的必要组成部分,那么下面的这种方法将有效。更新\def
的\section
必须在之后进行\tableofcontents
。此外,\Addlcwords
必须在之前进行\tableofcontents
。此外,部分标题中的字体大小更改也会破坏\tableofcontents
,因此我已将它们从此示例中删除。
已编辑以处理中的可选参数\section
,如下面的第 3 节所示。
\documentclass{article}
\usepackage{titlecaps}
\let\svsection\section
\Addlcwords{is for an of}
\begin{document}
\tableofcontents
\renewcommand\section[2][\relax]{%
\ifx\relax#1%
\svsection{\titlecap{#2}}%
\else%
\svsection[\titlecap{#1}]{\titlecap{#2}}%
\fi%
}
\section{This is my first section}
Tah dah But if you didn't like ``my,'' ``for,'' or ``an'' capitalized, except as the first word:
\section{for an \ae ncore, my \textit{section} for \c consideration}
\section[table of contents title]{document title}
done.
\end{document}
答案2
这是另一个与 Steven 的一样的工作示例。这个示例\chapter
也包括。包括创建 PDF 书签时出现问题的解决方法和创建参考书目时出现问题的另一种解决方法(下面的评论)。
\documentclass{book}
\usepackage{titlecaps}
\usepackage[bookmarks]{hyperref}
\let\svchapter\chapter
\let\svsection\section
\let\svsubsection\subsection
\let\svsubsubsection\subsubsection
\Addlcwords{is for an of}
\begin{document}
\tableofcontents
\chapter{First chapter}
%CAPS FOR CHAPTER HEADERS
\renewcommand{\chapter}[2][\relax]{%
\ifx\relax#1%
%\texorpdfstring FIXES LATEX WARNING. PROBLEM: PDF BOOKMARKS ARE NOT CAPITALIZED
\svchapter{\texorpdfstring{\titlecap{#2}}{#2}}%
\else%
\svchapter[\texorpdfstring{\titlecap{#1}}{#1}]{\texorpdfstring{\titlecap{#2}}{#2}}%
\fi%
}
%CAPS FOR SECTION HEADERS
\renewcommand{\section}[2][\relax]{%
\ifx\relax#1%
\svsection{\texorpdfstring{\titlecap{#2}}{#2}}%
\else%
\svsection[\texorpdfstring{\titlecap{#1}}{#1}]{\texorpdfstring{\titlecap{#2}}{#2}}%
\fi%
}
\renewcommand{\subsection}[2][\relax]{%
\ifx\relax#1%
\svsubsection{\texorpdfstring{\titlecap{#2}}{#2}}%
\else%
\svsubsection[\texorpdfstring{\titlecap{#1}}{#1}]{\texorpdfstring{\titlecap{#2}}{#2}}%
\fi%
}
\renewcommand{\subsubsection}[2][\relax]{%
\ifx\relax#1%
\svsubsubsection{\texorpdfstring{\titlecap{#2}}{#2}}%
\else%
\svsubsubsection[\texorpdfstring{\titlecap{#1}}{#1}]{\texorpdfstring{\titlecap{#2}}{#2}}%
\fi%
}
\section{This is my first section}
Cite: \cite{lamport94}
\section{for an \ae ncore, my \textit{section} for \c consideration}
\subsection[table of contents title]{document title}
done.
%RE-SET CHAPTER COMMAND. OTHERWISE BIBLIOGRAPHY WILL BREAK.
\renewcommand{\chapter}{\svchapter}
\begin{thebibliography}{9}
\bibitem{lamport94}
Leslie Lamport,
\emph{\LaTeX: a document preparation system},
Addison Wesley, Massachusetts,
2nd edition,
1994.
\end{thebibliography}
\end{document}
答案3
另一种选择是,如果您希望将未编号(带星号)的部分作为文档的一部分,则可以使用以下内容
\usepackage{titlecaps}
\let\svsection\section
\DeclareDocumentCommand\section{ s m }{% s = star, m = mandatory arg
\IfBooleanTF{#1}{%
\svsection*{\titlecap{#2}}
}{%
\svsection{\titlecap{#2}}
}%
}
注意:如果您在所有级别上执行此操作,则需要从子子节开始,然后逐步完成节。反之,我似乎会遇到错误。