1

I am using the fancyhdr package to customize my header and would like to display it with the section number, but without the section name, i.e. :

 ______________________________________________
| Section ##                      Page #/total |
|                                              |

I am using the following code to custom the header:

\usepackage{fancyhdr}
\fancyhead[C]{\begin{tikzpicture}[remember picture,overlay]
\node[yshift=-.05\paperheight] at (current page.north west)
{\begin{tikzpicture}[remember picture, overlay]
    \draw[fill=MainColor] (0,0) rectangle
    (\paperwidth,.05\paperheight);;
    \node[anchor=west,xshift=.01\paperwidth,yshift=.025\paperheight,text width=\paperwidth]
    {\normalfont\huge\color{white}\quad{Section \leftmark}};
    \node[anchor=west,xshift=.92\paperwidth,yshift=.025\paperheight,text width=\paperwidth]
    {\normalfont\normalsize\bfseries\textcolor{white}{\thepage/\hypersetup{linkcolor=white}\pageref{LastPage}}};
    \end{tikzpicture}
};
 \end{tikzpicture}}

Any ideas?

Thanks

EDIT: Added MWE

\documentclass{article}

\usepackage{tikz}
\usepackage{fancyhdr}
% HEADER %
\pagestyle{fancy}
\fancyhf{}
\fancyhead[C]{
\begin{tikzpicture}[remember picture, overlay]
\draw[fill=blue,xshift=-.5\paperwidth,yshift=-.5cm] (0,0) rectangle
(1.1\paperwidth,1.5cm);;
\node[anchor=north west,xshift=-.5\paperwidth,yshift=.5cm,text width=\paperwidth]
{\normalfont\huge\color{white}\quad{Section. \leftmark}};
\end{tikzpicture}
}

\usepackage{lipsum}

\begin{document}
\section{blah}

\lipsum
\subsection{asdf}
\lipsum \lipsum \lipsum

    \section{blahblah}

    \lipsum

    \lipsum

\end{document}

and it works. :)

  • replace \leftmark with \thesection – touhami Jun 28 '16 at 20:51
  • @touhami: That would do the trick, but when I do it, it gives the section number of the next section, i.e., for the first page of Section 5 it prints Section 6. Any idea why? – jorgehumberto Jun 28 '16 at 20:59
  • where does Section 6 start? same page, next? – touhami Jun 28 '16 at 21:07
  • 3
    Please, make the code into a compilable example. In any case, you need \renewcommand{\sectionmark}[1]{\markright{\thesection}} – egreg Jun 28 '16 at 21:10
  • 1
    Please don't edit questions to include answers. This is not the way the site works. Edit your question to include an MWE. Then ask whoever helped you to write an answer which you can accept. – cfr Jun 30 '16 at 2:08
1

If you want that the header contains just “Section 1” (or whatever the section number is), redefine \sectionmark:

\renewcommand{\sectionmark}[1]{\markright{\thesection}}

(the word “Section” is already supplied by the other code). This way, \rightmark will contain the current section number.

1

You can redefine \sectionmark to suit your needs. Additionally, I've added what's needed for the Page ##/total (see How can I add "page # of ##" on my document?).

enter image description here

\documentclass{article}

\usepackage{fancyhdr,lastpage,lipsum}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[L]{Section~\leftmark}
\fancyhead[R]{Page \thepage/\pageref{LastPage}}

\makeatletter
\renewcommand{\sectionmark}[1]{\markboth{\ifnum\c@secnumdepth>\z@\thesection}{}}
\makeatother

\begin{document}

\section{blah}\lipsum

\subsection{asdf}\lipsum\lipsum\lipsum

\section{blahblah}\lipsum\lipsum

\end{document}

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.