
我想將章節中的幾個單字的第一個字母和章節標記大寫。雖然章節本身的文字是大寫的,但章節標記中的文字卻不是大寫的。
這是我目前程式碼的 MWE:
\documentclass{book}
\usepackage{mfirstuc,fancyhdr,lipsum}
\fancypagestyle{mainmatter}{%
\fancyhf{}
\fancyhead[LE]{\thepage}
\fancyhead[RO]{\thepage}
\fancyhead[RE]{\nouppercase{\leftmark}}
\fancyhead[LO]{\nouppercase{\rightmark}}
}
\begin{document}
\pagestyle{mainmatter}
\chapter{\ecapitalisewords{some thing} with Text}
\lipsum[1-5]
\end{document}
我怎樣才能fancyhdr
與 合作\ecapitalisewords
?
編輯:
我注意到這\nouppercase
會殺人\MakeUppercase
\def\nouppercase##1{{\let\uppercase\relax\let\MakeUppercase\relax
\expandafter\let\csname MakeUppercase \endcsname\relax##1}}%
\ecapitalisewords
然而,哪些用途需要哪一個
\def\@gls@makefirstuc#1{\MFUapplytofirst\mfirstucMakeUppercase{#1}}
\newcommand*{\mfirstucMakeUppercase}{\MakeUppercase}
我進一步注意到,它fancyhdr.sty
定義\chaptermark
為\MakeUppercase
.
為此,我嘗試了
\documentclass{book}
\usepackage{mfirstuc,fancyhdr,lipsum}
\fancypagestyle{mainmatter}{%
\fancyhf{}
\fancyhead[LE]{\thepage}
\fancyhead[RO]{\thepage}
\fancyhead[RE]{\leftmark}
\fancyhead[LO]{\rightmark}
}
%
\let\orgMakeUppercase\MakeUppercase
\usepackage{xpatch}
\makeatletter
\xpatchcmd{\chaptermark}{\MakeUppercase}{}{}{}
\xpatchcmd{\sectionmark}{\MakeUppercase}{}{}{}
\xpatchcmd{\@gls@makefirstuc}{\mfirstucMakeUppercase}{\orgMakeUppercase}{}{}
\makeatother
\begin{document}
\pagestyle{mainmatter}
\chapter{\ecapitalisewords{some thing} with Text}
\lipsum[1-5]
\end{document}
但沒有成功,這次的章節標記是全部大寫,這意味著修補似乎不起作用。
答案1
您的解決方案不起作用的原因是\chaptermark
,\sectionmark
當您調用\pagestyle{mainmatter}
.發生這種情況是因為使用 建立的每個頁面樣式在設定時\fancypagestyle
也會調用\ps@fancy
,並且這會將 和 都設定\chaptermark
為\sectionmark
使用 的內容\MakeUppercase
。
要擺脫這些\MakeUppercase
,您可以添加這些行(我從您的問題中複製了這些行)
\xpatchcmd{\chaptermark}{\MakeUppercase}{}{}{}%
\xpatchcmd{\sectionmark}{\MakeUppercase}{}{}{}%
致電 後立即檢視您的文件\pagestyle{mainmatter}
。
將這些行新增至 的第二個參數將\fancypagestyle
不起作用,因為\ps@fancy
最後呼叫並將撤銷這些補丁。但是,您可以修補直接\ps@mainmatter
建立的巨集\fancypagestyle{mainmatter}{…}
以包含這些行。這將是/可能會如何進行:
\documentclass{book}
\usepackage{mfirstuc,fancyhdr,lipsum}
\fancypagestyle{mainmatter}{% %% <- defines \ps@mainmatter
\fancyhf{}%
\fancyhead[LE]{\thepage}%
\fancyhead[RO]{\thepage}%
\fancyhead[RE]{\leftmark}%
\fancyhead[LO]{\rightmark}%
}
\usepackage{xpatch}
\makeatletter
\appto\ps@mainmatter{% %% <- appends to \ps@mainmatter
\xpatchcmd{\chaptermark}{\MakeUppercase}{}{}{}%
\xpatchcmd{\sectionmark}{\MakeUppercase}{}{}{}%
}
\makeatother
\begin{document}
\pagestyle{mainmatter} %% <- calls \ps@mainmatter
\chapter{\ecapitalisewords{some thing} with Text}
\lipsum[1-5]
\end{document}
附錄:
您也可以重新定義\ps@fancy
以刪除那裡的所有實例\MakeUppercase
,但您要求的是侵入性最小的解決方案,而這可能不是這樣。不過,為了完整起見,這裡有個方法:
\documentclass{book}
\usepackage{mfirstuc,fancyhdr,lipsum}
\fancypagestyle{mainmatter}{% %% <- defines \ps@mainmatter
\fancyhf{}%
\fancyhead[LE]{\thepage}%
\fancyhead[RO]{\thepage}%
\fancyhead[RE]{\leftmark}%
\fancyhead[LO]{\rightmark}%
}
\usepackage{xpatch}
\newrobustcmd\exhaustivepatchcmd[3]{% %%<- patches as often as necessary
\patchcmd{#1}{#2}{#3}{\exhaustivepatchcmd{#1}{#2}{#3}}{}%
}
\makeatletter
\patchcmd{\ps@fancy}{\def\MakeUppercase{\uppercase}}{}{}{}
\exhaustivepatchcmd{\ps@fancy}{\MakeUppercase}{\@firstofone}
\makeatother
\begin{document}
\pagestyle{mainmatter}
\chapter{\ecapitalisewords{some thing} with Text}
\lipsum[1-5]
\end{document}
(我定義只是\exhaustivepatchcmd
因為手動調用\patchcmd
三次感覺不對。我刪除是因為不應修補\def\MakeUppercase{\uppercase}
該實例(並且沒有任何作用)。)\MakeUppercase
答案2
一個可能的解決方案是這樣的:
\documentclass{book}
\usepackage{mfirstuc,fancyhdr,lipsum}
\fancypagestyle{mainmatter}{%
\fancyhf{}
\fancyhead[LE]{\thepage}
\fancyhead[RO]{\thepage}
\fancyhead[RE]{\leftmark}
\fancyhead[LO]{\rightmark}
}
\newcounter{mychap}
\newcommand\mychapadd{\setcounter{mychap}{\value{chapter}}\stepcounter{mychap}}
\newcommand\mchapter[1]{\xdef\Arg{#1}\mychapadd\xdef\leftmark{Chapter~\themychap.\space\Arg}
\chapter{\Arg}}
\begin{document}
\pagestyle{mainmatter}
\mchapter{\ecapitalisewords{this is} a test}
\lipsum[1-5]
\end{document}
該解決方案使用命令\mchapter
,但您可以重新定義原始 \chapter 命令來執行此操作,即使它是加星號的命令等。這裡)