4

I am using Latex for the first time, to write my thesis.

I organized my work as a main file that calls several others using subfiles, since I like to compile a single chapter while I am working on it. However, the chapters produced in this way do not have the same layout of the main file (this is a problem only for the final version, that I have to send to my supervisor). I overcame this problem compiling the main file with all the code commented but the one necessary to produce the chapter I an intersted in, and changing \setcounter{chapter}{} in order to have the right number.

It works fine, but I saw that if I comment \pagestyle{plain} in the code below, the package fancyhdr does not apply the page style I defined. May someone explain me why? I expected that since I entered \pagestyle{MyStyle} then the style of the pages following this command should be MyStyle.

\documentclass[12pt,twoside,openright]{report}

\usepackage[english]{babel}

% Page layout
\usepackage[a4paper,
            inner=41mm,
            outer=32mm,
            top=43mm,
            bottom=44mm,
            headheight=20pt,
            headsep=\baselineskip,
            bindingoffset=0mm]{geometry}

\usepackage{emptypage}

\usepackage[T1]{fontenc}
\usepackage{mathptmx}

\usepackage{lipsum}

\usepackage{subfiles}

% Page style
\usepackage{fancyhdr}
% Defining MyStyle
\fancypagestyle{MyStyle}{%
    \fancyhead{}
    \fancyhead[RO]{\rightmark \ \ {\vrule height 13pt width 1pt} \ \textbf{\thepage}}
    \fancyhead[LE]{\textbf{\thepage} \ {\vrule height 13pt width 1pt} \ \leftmark}
    \fancyfoot{}
    \fancyfoot[RO,LE]{}
    \renewcommand{\headrulewidth}{0pt}
    \renewcommand{\chaptermark}[1]{\markboth{\thechapter. \ \textsc{##1}}{}}
    \renewcommand{\sectionmark}[1]{\markright{\thesection\ {##1}}}
}
% Redefining the PLAIN style
\fancypagestyle{plain}{%
    \fancyhead{}
    \fancyhead[RO]{{\vrule height 13pt width 1pt} \ \textbf{\thepage}}
    \fancyhead[LE]{\textbf{\thepage} \ {\vrule height 13pt width 1pt}}
    \fancyfoot{}
    \fancyfoot[RO,LE]{}
    \renewcommand{\headrulewidth}{0pt}
}

\begin{document}

\pagenumbering{Roman}
\pagestyle{plain}

%Abstract

%Table of Contents

%Lists of Figures

%Lists of Tables

\clearpage

\pagenumbering{arabic}
\pagestyle{MyStyle}

% Previous chapters...

\setcounter{chapter}{3}
\chapter{Chapter Title}
\section{First section title}
\lipsum
\section{Second section title}
\lipsum

% Following chapters...

\end{document}
6
  • Maybe if you add \pagestyle{} before apply your page style could woks: \usepackage{fancyhdr} \pagestyle{} \fancypagestyle...
    – juanuni
    Commented Jun 14, 2015 at 17:33
  • It's possible to switch temporarily to \pagestyle{plain} within \fancypagestyle{MyStyle}, before the changes are made
    – user31729
    Commented Jun 14, 2015 at 18:23
  • To be honest, i don't see anything strange looking at your output. With or without plain commented.
    – Johannes_B
    Commented Jun 14, 2015 at 18:31
  • If \tableofcontents is used without \pagestyle{plain} the whole stuff works too. I assume that it has something to do with the effective non-page right at the beginning of the document
    – user31729
    Commented Jun 14, 2015 at 18:33
  • I believe \tableofcontents activates \thispagestyle{empty}. Anyway, aren't you supposed to use \pagestyle{fancy} with fancyhdr? Commented Jun 14, 2015 at 22:07

1 Answer 1

3

From the package manual section 9 "The scoop on LATEX’s marks" (page 9):

You can influence how chapter, section, and subsection information (only two of them!) is displayed by redefining the \chaptermark, \sectionmark, and \subsectionmark commands. You must put the redefinition after the first call of \pagestyle{fancy} as this sets up the defaults.

More specifically the first call of \ps@fancy sets the defaults.

The definition of \fancypagestyle (from the fancyhdr.sty):

\newcommand{\fancypagestyle}[2]{%
  \@namedef{ps@#1}{\let\fancy@gbl\relax#2\relax\ps@fancy}}

Note that \fancypagestyle{MyStyle} is not executed until the first call of \pagestyle{MyStyle} is done. So if the \pagestyle{MyStyle} is the first call of a fancyhdr pagestyle it is also the first call of \ps@fancy. Hence the redefinitions of \chaptermark and \sectionmark in the second argument of \fancypagestyle are overwritten by the defaults. But if you call an other fancyhdr pagestyle before MyStyle or you use \pagestyle{MyStyle} again it works fine.

\documentclass[12pt,twoside,openright]{report}
\usepackage{emptypage}
\usepackage[T1]{fontenc}
\usepackage{lipsum}

% Page style
\usepackage{fancyhdr}
\pagestyle{fancy}% <- !
% Defining MyStyle
\fancypagestyle{MyStyle}{%
    \fancyhead{}
    \fancyhead[RO]{\rightmark \ \ {\vrule height 13pt width 1pt} \ \textbf{\thepage}}
    \fancyhead[LE]{\textbf{\thepage} \ {\vrule height 13pt width 1pt} \ \leftmark}
    \fancyfoot{}
    \fancyfoot[RO,LE]{}
    \renewcommand{\headrulewidth}{0pt}
    \renewcommand{\chaptermark}[1]{\markboth{\thechapter. \ \textsc{##1}}{}}
    \renewcommand{\sectionmark}[1]{\markright{\thesection\ {##1}}}
}
% Redefining the PLAIN style
\fancypagestyle{plain}{%
    \fancyhead{}
    \fancyhead[RO]{{\vrule height 13pt width 1pt} \ \textbf{\thepage}}
    \fancyhead[LE]{\textbf{\thepage} \ {\vrule height 13pt width 1pt}}
    \fancyfoot{}
    \fancyfoot[RO,LE]{}
    \renewcommand{\headrulewidth}{0pt}
}

\begin{document}
\pagestyle{MyStyle}
% Previous chapters...
\setcounter{chapter}{3}
\chapter{Chapter Title}
\section{First section title}
\lipsum
\section{Second section title}
\lipsum
% Following chapters...
\end{document}

It is also possible to use

\documentclass[12pt,twoside,openright]{report}
\usepackage{emptypage}
\usepackage[T1]{fontenc}
\usepackage{lipsum}

% Page style
\usepackage{fancyhdr}

% Defining MyStyle
\fancypagestyle{MyStyle}{%
    \fancyhead{}
    \fancyhead[RO]{\rightmark \ \ {\vrule height 13pt width 1pt} \ \textbf{\thepage}}
    \fancyhead[LE]{\textbf{\thepage} \ {\vrule height 13pt width 1pt} \ \leftmark}
    \fancyfoot{}
    \fancyfoot[RO,LE]{}
    \renewcommand{\headrulewidth}{0pt}
    \renewcommand{\chaptermark}[1]{\markboth{\thechapter. \ \textsc{##1}}{}}
    \renewcommand{\sectionmark}[1]{\markright{\thesection\ {##1}}}
}
% Redefining the PLAIN style
\fancypagestyle{plain}{%
    \fancyhead{}
    \fancyhead[RO]{{\vrule height 13pt width 1pt} \ \textbf{\thepage}}
    \fancyhead[LE]{\textbf{\thepage} \ {\vrule height 13pt width 1pt}}
    \fancyfoot{}
    \fancyfoot[RO,LE]{}
    \renewcommand{\headrulewidth}{0pt}
}

\begin{document}
\pagestyle{MyStyle}% or \pagestyle{fancy} or \pagestyle{plain}
\pagestyle{MyStyle}
% Previous chapters...
\setcounter{chapter}{3}
\chapter{Chapter Title}
\section{First section title}
\lipsum
\section{Second section title}
\lipsum
% Following chapters...
\end{document}

Note that \pagestyle{plain}\pagestyle{MyStyle} only works if plain is redefined by \fancypagestyle{plain}{...}.


Additionally here is another suggestion using scrlayer-scrpage

\documentclass[12pt,twoside,openright]{report}
\usepackage{emptypage}
\usepackage[T1]{fontenc}
\usepackage{lipsum}

% Page style
\usepackage[automark,markcase=noupper]{scrlayer-scrpage}
\addtokomafont{pagehead}{\normalfont}
\addtokomafont{pagenumber}{\bfseries}
\providepairofpagestyles{MyStyle}{
  \rohead%
    [{\vrule height 13pt width 1pt} \ \pagemark]%
    {\headmark \ \ {\vrule height 13pt width 1pt} \ \pagemark}
  \lehead%
    [\pagemark \ {\vrule height 13pt width 1pt}]%
    {\pagemark \ {\vrule height 13pt width 1pt} \ \scshape{\headmark}}
  \renewcommand\chaptermarkformat{\thechapter. \ }
}
\DeclarePageStyleAlias{plain}{plain.MyStyle}

\begin{document}
\pagestyle{MyStyle}
% Previous chapters...
\setcounter{chapter}{3}
\chapter{Chapter Title}
\section{First section title}
\lipsum
\section{Second section title}
\lipsum
% Following chapters...
\end{document}
1
  • Thank you very much! Now everything is clear, I must have missed that line when I read the fancyhdr package!
    – MarcoG
    Commented Jun 15, 2015 at 7:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .