梅威瑟:
\documentclass[12pt,a4paper]{report}
\usepackage{fancyhdr}
\usepackage{etoolbox}
\makeatletter
%this is a hack to get the math in titles bold & italic, but not bold in table of contents
\patchcmd{\@sect}{#8}{\boldmath #8}{}{}
\let\ori@chapter\@chapter
\def\@chapter[#1]#2{\ori@chapter[\boldmath#1]{\boldmath#2}}
\makeatother
\begin{document}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[L]{\nouppercase{\leftmark}}
\fancyhead[R]{\thepage}
\tableofcontents
\setcounter{page}{1}
\chapter{Proof that $x=y$}
\section{Proof that $x=y$}
This chapter proves that $x=y$
\newpage
$x=y$.
\end{document}
编译此文档时,您会发现目录中的数学符号排版正确(在常规文本为粗体的地方以粗体显示,否则不显示),但在标题的花哨页眉中则不正确。有没有简单的解决方法?
答案1
\boldmath
对常规文本没有影响,请参见代码末尾的示例。
如果确实想要这个,就必须有一个\bfseries
命令。\leftmark
我增加了头部高度值(因为fancyhdr
抱怨该值太小!)
\documentclass[12pt,a4paper]{report}
\usepackage{fancyhdr}
\usepackage{etoolbox}
\makeatletter
%this is a hack to get the math in titles bold & italic, but not bold in table of contents
\patchcmd{\@sect}{#8}{\boldmath #8}{}{}
\let\ori@chapter\@chapter
\def\@chapter[#1]#2{\ori@chapter[\boldmath#1]{\boldmath#2}}
\makeatother
\begin{document}
\setlength{\headheight}{15pt}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[L]{\bfseries\nouppercase{\leftmark}}
\fancyhead[R]{\bfseries\thepage}
\tableofcontents
\pagenumbering{arabic}
\chapter{Proof that $x=y$}
\section{Proof that $x=y$}
This chapter proves that $x=y$
\newpage
$x=y$.
\verb!\boldmath foo -- will be normal text and not bold!!
\boldmath foo
\end{document}
更新
\documentclass[12pt,a4paper]{report}
\usepackage{fancyhdr}
\usepackage{xpatch}
\makeatletter
%this is a hack to get the math in titles bold & italic, but not bold in table of contents
\xpatchcmd{\@sect}{#8}{\boldmath#8}{}{}
\let\ori@chapter\@chapter
\def\@chapter[#1]#2{%
\addtocontents{toc}{\protect\boldmath}
\ori@chapter[#1]{\boldmath#2}
% \addtocontents{toc}{\protect\unboldmath} % Use this for switch of bold math after chapter
}
\AtBeginDocument{%
\unboldmath
}
\begin{document}
\setlength{\headheight}{15pt}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[L]{\nouppercase{\leftmark}}
\fancyhead[R]{\bfseries\thepage}
\tableofcontents
\pagenumbering{arabic}
\chapter{Proof that $x=y$}
\section{Proof that $x=y$}
This chapter proves that $x=y$
\newpage
$x=y$.
\verb!\boldmath foo -- will be normal text and not bold!!
\boldmath foo\unboldmath
\end{document}
答案2
你应该更小心地修补:
\documentclass[12pt,a4paper]{report}
\usepackage{fancyhdr}
\usepackage{etoolbox}
\setlength{\headheight}{14.5pt} % to comply with fancyhdr recommendation
\makeatletter
%this is a hack to get the math in titles bold & italic, but not bold in table of contents
\patchcmd{\@makechapterhead}% typesets the numbered chapter header
{\bfseries}
{\bfseries\boldmath}
{}{}
\patchcmd{\@makeschapterhead}% typesets the unnumbered chapter header
{\bfseries}
{\bfseries\boldmath}
{}{}
\patchcmd{\l@chapter}% typesets the chapter titles in the TOC
{\bfseries}
{\bfseries\boldmath}
{}{}
\patchcmd{\@sect}% for sectional titles
{#8}
{\boldmath #8}
{}{}
\makeatother
\begin{document}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[L]{\nouppercase{\leftmark}}
\fancyhead[R]{\thepage}
\tableofcontents
\setcounter{page}{1}
\chapter{Proof that $x=y$}
\section{Proof that $x=y$}
This chapter proves that $x=y$
\newpage
$x=y$.
\end{document}
\boldmath
我没有将其添加到 的参数中,而是\@chapter
在排版时添加它,此时涉及\@makechapterhead
或。 补丁到有效,因为所有分段命令都使用;在其他情况下,应应用与其他补丁(即 )类似的补丁。\@makeschapterhead
\@sect
\bfseries
\bfseries
\bfseries\boldmath