2

I would like to capitalize the first letter of several words in the chapter and the chapter mark. While text in the chapter itself is capitalized, in the chapter mark it is not.

Here is a MWE of my current code:

\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}

How can I make fancyhdr work with \ecapitalisewords ?

enter image description here

EDIT:

I noticed that \nouppercase kills \MakeUppercase

\def\nouppercase##1{{\let\uppercase\relax\let\MakeUppercase\relax
      \expandafter\let\csname MakeUppercase \endcsname\relax##1}}%

which however is needed by \ecapitalisewords which uses

\def\@gls@makefirstuc#1{\MFUapplytofirst\mfirstucMakeUppercase{#1}}
\newcommand*{\mfirstucMakeUppercase}{\MakeUppercase}

I further notices, that fancyhdr.sty defines \chaptermark with \MakeUppercase.

For this reason, I tried

 \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}

But without success, the chaptermark is this time ALL CAPITAL, meaning the patching did not seem to work to being with.

1
+50

The reason why your solution doesn't work is that \chaptermark and \sectionmark are redefined when you call \pagestyle{mainmatter}. This happens because every page style created with \fancypagestyle also calls \ps@fancy when set, and this sets both \chaptermark and \sectionmark to something that uses \MakeUppercase.

To get rid of these \MakeUppercases, you could add the lines (which I copied from your question)

\xpatchcmd{\chaptermark}{\MakeUppercase}{}{}{}%
\xpatchcmd{\sectionmark}{\MakeUppercase}{}{}{}%

to your document, right after calling \pagestyle{mainmatter}.

Adding these lines to the second argument of \fancypagestyle won't work because \ps@fancy is called last and would undo these patches. You could however patch the macro \ps@mainmatter that was created by \fancypagestyle{mainmatter}{…} directly to include these lines. Here's how that would/could go:

\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}

chapter title

header


Addendum:

You could also redefine \ps@fancy to remove all instance of \MakeUppercase there, but you're asking for the least invasive solution, and that probably wouldn't be it. For completeness, though, here's a way to do that:

\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}

(I only define \exhaustivepatchcmd because manually calling \patchcmd three times feels wrong. I remove \def\MakeUppercase{\uppercase} because that instance of \MakeUppercase shouldn't be patched (and serves no purpose).)

2

A possible solution would be this:

\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}

This solution uses the command \mchapter but you could redefine the original \chapter command to do it even if it is a starred command etc. (See here)

  • Thanks! is there also a less invasive method? I though the root of the problem is the \nouppercase macro, is that possible? – bonanza Feb 27 at 18:55
  • Sorry, but this is the way I thought when answering and now I have not the time to look for something else... May be another day will look about it again, or someone else could answer till then. – koleygr Feb 27 at 19:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.