0

Why does that not work? And how to fix it?

\begin{document}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[LE,RO]{\textbf{Name}}
\fancyhead[RE,LO]{\textit{\leftmark}}
\fancyfoot[CE,CO]{{\parbox{\textwidth}{\hrulefill\kern-0.6ex\raisebox{-0.5ex}{$\Bigl[$\makebox[\widthof{1.2{\thepage
of \pageref{LastPage}}}][c]{\thepage \hspace{1pt} of \pageref{LastPage}}$\Bigr]$}\kern-0.5ex\hrulefill}}}

\end{document}

It should be a header with my name and the chapter/section. In the footer it should be a nice page-numbering. Could anyone help me please?

1
  • How is this different to the previous question?
    – Joseph Wright
    Jun 17, 2014 at 7:17

2 Answers 2

3

Again a solution using the modern scrlayer-scrpage:

\documentclass{book}
\usepackage{blindtext}
\usepackage{lastpage}

\usepackage{scrlayer-scrpage}
\clearscrheadfoot
\ihead{\headmark}
\ohead{\textbf{Your Name}}
\usepackage{calc}
\newcommand\footy{\parbox{\textwidth}{\hrulefill\kern-0.6ex\raisebox{-0.5ex}{$\Bigl[$\makebox[\widthof{1.2{\thepage\,
of \pageref{LastPage}}}][c]{\thepage\, of \pageref{LastPage}}$\Bigr]$}\kern-0.5ex\hrulefill}}
%Thanks to Pouya
\cfoot[\footy]{\footy}
\begin{document}
\blinddocument
\blinddocument
\end{document}

This is almost the same answer i gave in Problems with header and footer in documentclass book.

2

The main reason that your sample won't work is that fancyheader is set up to use \leftmark as the chapter info and \rightmark as the section info. It then puts those into the headings where you tell it to. Since you are putting NAME into the heading in the place where one of those would normally go, you get your name and only one of the other of \leftmark (chapter info) or \rightmark (section info). To get name and section and chapter info in the heading, you need to either \renewcommand to change the meaning of either \leftmark or \rightmark or both, or you need to tell fancy header to put both \leftmark and \rightmark in the same place in the header. Below I show one way of doing this.

In your footer, you appear to be trying to tell latex how wide to make the box with the page number info. You can do what you are trying to do using the calc package and \settowidth, but you can create a simpler and acceptable result just using \width instead of \widthof. Then you don't need to input the \pageref{lastpage}etc info twice. With \width, latex will make the box the width necessary to fit the material being placed in it.

To get your example to compile, I had to add:

\usepackage{fancyhdr}
\usepackage{nameref}%To allow creation of a new command for section name

and

\usepackage{lastpage}%To make the "last page" part of your footer work

Here's a working sample idea.

\documentclass{book}
\usepackage{fancyhdr}
\usepackage{lipsum}
\usepackage{lastpage}
\begin{document}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[LE,RO]{\textbf{Name}}
\makeatletter%to give access to "@" command
\newcommand*{\currentname}{\@currentlabelname}%Assigns the current section name to "\currentname."
\makeatother%to give access to "@" command
\fancyhead[RE,LO]{\textit{\leftmark - \currentname}}%Puts section head after Chaptername (left mark is the Chapter Name)
\chapter{Example Chapter}
\section{Section Head}
\lipsum
\fancyfoot[CE,CO]{{\parbox{\textwidth}{\hrulefill\kern-.6ex\raisebox{-0.5ex}     
{$\Bigl[$\makebox[\width{{
}}][c]{\thepage \hspace{1pt} of \pageref{LastPage} }$\Bigr]$}\kern-    0.5ex\hrulefill}}}%removed second instance of the page numbers.
\section{two.}
\lipsum
\end{document}

You weren't clear on exactly what you wanted to show up as the chapter name in the header. This has both Chapter # and the chapter name follwed by the section heading.

I suspect that in practice you might find the overall text way too long.

Instead of creating \currentlabel, like I did, you could also do:

\fancyhead[RE,LO]{\textit{\leftmark - \rightmark}}

That would include even more info with both chapter number and section number.

1
  • Chapter 1. [1] ! Undefined control sequence. \currentname ->\@currentlabelname l.21 \end {document} ? Aug 14, 2019 at 5:35

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