[Tex/LaTex] Format of the header in the table of contents

header-footertable of contents

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{...}

Best Answer

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.