3

I am working with the following document:

\documentclass[a4paper,11pt]{article}
\usepackage[utf8]{inputenc}

\usepackage{fancyhdr}
\setlength{\headheight}{13.6pt}
\fancypagestyle{plain}{
    \fancyhf{}
    \cfoot{\thepage}
}
\pagestyle{fancy}

\begin{document}

\pagestyle{plain}
\tableofcontents
\clearpage

\pagestyle{fancy}

\include{text/intro}
\include{text/methods}
\include{text/results}
\include{text/discussion}

\end{document}

I want the table of contents to be formatted with my own definition of the plainstyle, but the rest of the document should be in the fancy style.

The problem is that the second \pagestyle{fancy} call doesn't seem to do anything. All the subsequent pages are formatted as \pagestyle{plain}. The problem seems to go away when i remove my redefinition of plain. I don't understand this behavior, can anyone shed some light?

  • What if you give another name to your version of plain? – Bernard Apr 26 '15 at 18:50
  • @Bernard: Should do. Go on, provide an answer ;-) – user31729 Apr 26 '15 at 19:03
  • @Christian Hupfer: I'll do it in a moment. I want to test first if the same problem happens with titleps, which I find easier to use. – Bernard Apr 26 '15 at 19:12
  • @hristian Hupfer: Didn't work as suggested with fancyhdr :o( So I proposed an alternative solution, based on titleps. – Bernard Apr 26 '15 at 20:01
2

I propose a solution with the titleps package, from the titlesec bundle. You even don't have to refine plain. I defined a myfancy page style, that mimics the default fancy pagestyle from fancyhdr:

\documentclass[a4paper,twoside, 11pt]{article}
\usepackage[utf8]{inputenc}

 \usepackage{titleps}
 \newpagestyle{myfancy}{%
 \headrule
 \sethead[][][\itshape\MakeUppercase{\thesection\enspace\sectiontitle}]{\itshape \MakeUppercase{\thesection\enspace \sectiontitle}}{}{}
 \setfoot{}{\thepage}{}
 }

\renewpagestyle{plain}{%
\headrule
\sethead{}{}{}
\setfoot{}{\thepage}{}
}

\begin{document}

\pagestyle{plain}
\tableofcontents
\clearpage

\pagestyle{myfancy}

\section{text/intro}
\newpage
\section{text/methods}
\newpage
\section{text/results}
\newpage
\section{text/discussion}


\end{document} 

enter image description here

enter image description here

  • While this takes care of the issue, it is no different from removing the redefinition of plain in my original example. I need to redefine it because I want the rule on top of the plain pages as well. – lydhvin Apr 26 '15 at 21:35
  • 1
    Not exactly, since what worked for me was only \thispagestyle{plain], which is problematic if the table of contents is several pages long. Anyway, \renewpagestyle works well with titleps. See my updated answer. – Bernard Apr 26 '15 at 22:02
  • Yep, that does the trick, I will use that for now. However, if anyone can explain why my original attempt did not work I would be very grateful. I would prefer to use fancyhdr if I can, as I found the documentation to be much more helpful. – lydhvin Apr 27 '15 at 8:24
  • @lydhvin: I don't know fancyhdr details well enough to explain. What's funny is I could say the same as you about titleps vs fancyhdr… It is much more transparent to use \sectiontitle, \chaptertitle and the like than \leftmark and \rightmark. Furthermore, if necessary you can have a sophisticated use of marks. – Bernard Apr 27 '15 at 9:27
  • I'll take your word for it:) I am sure titleps is a better package, but the documentation was somewhat cryptic for a casual user like myself. I find the fancyhdr documentation to be clearer. But anyway, I'll mark the question as answered for now, at least until I can find a solution using fancyhdr. Thanks! – lydhvin Apr 27 '15 at 10:13
2

I do not know why your example does not work. But there are two suggestions with fancyhdr.

You can change the pagestyle locally:

\documentclass[a4paper,11pt]{article}
\usepackage{fancyhdr}
\setlength{\headheight}{13.6pt}
\fancypagestyle{plain}{
    \fancyhf{}
    \cfoot{\thepage}
}
\pagestyle{fancy}

\usepackage{blindtext}% dummy text
\begin{document}

{\pagestyle{plain}
\tableofcontents
\clearpage}

\Blinddocument\Blinddocument\Blinddocument\Blinddocument
\end{document}

Or you can define a new style for the main part:

\documentclass[a4paper,11pt]{article}
\usepackage{fancyhdr}
\setlength{\headheight}{13.6pt}
\fancypagestyle{plain}{
    \fancyhf{}
    \cfoot{\thepage}
}
\fancypagestyle{myfancy}{
\fancyhead[L]{\slshape\rightmark}
\fancyhead[R]{\slshape\leftmark}
\fancyfoot[C]{\thepage}}

\usepackage{blindtext}% dummy text
\begin{document}

\pagestyle{plain}
\tableofcontents
\clearpage

\pagestyle{myfancy}
\Blinddocument\Blinddocument\Blinddocument\Blinddocument
\end{document}
  • The first workaround works beautifully, thanks. The second one is problematic. It works if I call \pagestyle{myfancy} before \pagestyle{plain} and \tableofcontents and then \pagestyle{myfancy} again. But if I omit the first \pagestyle{myfancy} it makes all the headers for the rest of the documents say CONTENT instead of the current section and subsection. This is getting weird. – lydhvin Apr 27 '15 at 17:40

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.