0

Help please, I'm writing a Project (document class=book), and I'm using fancyhdr to change the format of the headers. My problem is that that format doesn't apply to the first pages of the table of contents. I mean, this is the header format in pages 2 & 3, which is the standard one: enter image description here

while the header format in page 4 is the one I want (serif font, and no uppercase):

enter image description here

What can I do to have the format I want also in pages 2 and 3?

The first lines of my codes are:

\begin{document}
\afterpage{\markboth{\sf Índice}{\sf Índice}}
\tableofcontents
\markboth{\sf Índice}{\sf Índice}
\include{...}
1

In the future, please produce a proper minimal working example; not only does doing so often solve your problem all by itself, but it makes it much easier for others to help you.

As an aside, don't use \sf (along with \bf, \it, and so forth); these are deprecated font change commands. Rather, you should be using \sffamily or \textsf{text}, because these commands fit into the LaTeX2e New Font Selection Scheme.

For your headers: there's really no need to reinvent the wheel; you can try using a package to handle the headers. fancyhdr is probably the most popular; it can what you want in very few lines:

\documentclass{book}
\usepackage{lipsum}
\usepackage{fancyhdr}
    \fancyfoot{}
    \fancyhead{}
    \fancyhead[RO,LE]{\thepage}
    \fancyhead[C]{\textsf{\nouppercase{\leftmark}}}
    \pagestyle{fancy}
\begin{document}
\tableofcontents
\lipsum\lipsum
\end{document}

Here, we begin by clearing current headers and footers with \fancyfoot{} and \fancyhead{}; that gives us a clean slate. Then we set the page numbers on the left of even pages and the right of odd pages; then, in the middle of all pages, we put \leftmark, which is your chapter name in the book class. We style it with \textsf, for sans serif type; and we use \nouppercase to prevent LaTeX from capitalizing the whole thing. And that's it.

fancyhdr is really a superb package with excellent documentation; giving it a once-over will be quite helpful.

  • your "roll your own" version will fail on longer documents as the \markboth will come on the final page of the table of contents not the first. (\afterpage is in the core latex distribution, from package of same name) – David Carlisle Jun 13 '15 at 13:45
  • So it will! I'm just going to remove it, as it's a bad idea anyway. – dgoodmaniii Jun 13 '15 at 13:46
  • Thank you! I was already using fancyhdr, but imposing the serif font in another way. The command {\textsf{\nouppercase{\leftmark}}} solved my problem. – Manu Jun 13 '15 at 14:07
  • Glad it helps; but on TeX.SE, we usually don't post thank yous; upvoting or accepfing answers is our preferred way to say thanks. Happy TeXing! – dgoodmaniii Jun 13 '15 at 16:42

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.