3

I am using the report class. And all I want that after the use of \chapter{...} the marco \thispagestyle{onlypagenumber} is added.

I fixed this with just defining a new macro \Chapter{...}

\newcommand{\Chapter}[1]{\chapter{#1}\thispagestyle{onlypagenumber}}

However I would rather like to still use \chapter{...} since people are used to typing \chapter{...} instead of \Chapter{...}. How can I make this small alteration? Note also that \thispagestyle{onlypagenumber} should not be added to star chapters e.g. \chapter*{...}

I tried also using the etoolbox which provides the some of "appto" commands on page 9.

\appto This command appends arbitrary hcodei to a hhooki. If the hcodei contains any parameter characters, they need not be doubled. This command is robust.

This failed however but probably I am doing something wrong. I would prefer a solution of the etoolbox.

Example

\documentclass{book}
\usepackage{fancyhdr}
\usepackage{lipsum}

\fancypagestyle{onlypagenumber}{%
  \fancyhf{}
  \fancyhfoffset[RO,EL]{2.5cm}
  \fancyhead[OR]{%
    \sffamily
    \quad\smash{\rule[-.2ex]{1pt}{4cm}}\quad
    \thepage
  }
  \fancyhead[EL]{%
    \sffamily\thepage
    \quad\smash{\rule[-.2ex]{1pt}{4cm}}\quad
  }
}

\pagestyle{fancy}
\fancyhf{}
\fancyhfoffset[OR,EL]{2.5cm}
\fancyhead[OR]{%
  \sffamily\MakeUppercase{\leftmark}
  \quad\smash{\rule[-.2ex]{1pt}{4cm}}\quad
  \thepage
}
\fancyhead[EL]{
  \sffamily\MakeUppercase{\thepage}
  \quad\smash{\rule[-.2ex]{1pt}{4cm}}\quad
  \MakeUppercase{\chaptername}~\thechapter
}
\renewcommand\headrulewidth{0pt}
\renewcommand{\chaptermark}[1]{\markboth{\MakeUppercase{#1}}{}}

\newcommand{\Chapter}[1]{\chapter{#1}\thispagestyle{onlypagenumber}}

\begin{document}

\Chapter{Test chapter}
\lipsum[1-40]

\end{document}
3

An easy way is to patch \chapter:

\usepackage{etoolbox}
\patchcmd{\chapter}{plain}{onlypagenumber}{}{}

because \chapter does \thispagestyle{plain} and the patch will replace plain with onlypagenumber.

However, an easier way is just doing

\fancypagestyle{plain}{%
  \fancyhf{}
  \fancyhfoffset[RO,EL]{2.5cm}
  \fancyhead[OR]{%
    \sffamily
    \quad\smash{\rule[-.2ex]{1pt}{4cm}}\quad
    \thepage
  }
  \fancyhead[EL]{%
    \sffamily\thepage
    \quad\smash{\rule[-.2ex]{1pt}{4cm}}\quad
  }
}

so as to redefine the plain page style, instead of defining a new one: you probably don't need the plain page style any more, do you?


For changing the style only for numbered chapters, use

\usepackage{etoolbox}

\makeatletter
\pretocmd{\@makechapterhead}{\thispagestyle{onlypagenumber}}{}{}
\makeatother

so that \thispagestyle{plain} issued by \chapter will be countermanded when \@makechapterhead (which is executed only for numbered chapters) is processed.

  • If I use the patchcmd it also replaced the page style of chapters with a star (\chapter*{...}) which I do not want. And I cannot use the plain pagestyle since I already have defined that. – WG- May 26 '14 at 10:35

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.