[Tex/LaTex] How to make leftmark/rightmark next to Page number

fancyhdrheader-footer

The desired header style is like: page number is sans serif even in the first page of the chapter, because

Using

\fancyhead[LE]{\sffamily \thepage  }
\fancyhead[RO]{\sffamily \thepage  }

could not change the font of the page number in the first page of each chapter.

In even page: Page number | Chapter title

enter image description here

In odd page: Section title | Page number

enter image description here

Using \fancyheadoffset[RE,LO]{-0.5\textwidth} could not get this effect, so how to achieve this style?

Minimal page:

\documentclass{book}
\usepackage{kantlipsum}    
\usepackage{libertine}
\usepackage[ ]{titlesec} 
\usepackage[]{fancyhdr}
\pagestyle{fancy}
\makeatletter
\makeatother
\fancyhf{}     
\fancyhead{} 

\makeatletter
\renewcommand{\sectionmark}[1]{\markright{\thesection~~~#1}}
\renewcommand{\chaptermark}[1]{\markboth{\if@mainmatter \fi#1}{}}
\makeatother

% head setting
\fancyhead[LE]{ \sffamily \thepage $|$ } 
\fancyhead[RE]{  \leftmark  }
\fancyhead[LO]{  \rightmark  }
\fancyhead[RO]{ $|$ \sffamily\thepage}   %odd page
\fancyheadoffset[RE,LO]{-0.5\textwidth}

\renewcommand{\headrulewidth}{ 0.5 pt}

\begin{document}
\tableofcontents
\chapter{Introduction}
 \kant[1-5]
\section{This is a extremely  long ........... title}
  \kant[6-10]
\section{Short title}
  \kant[11-15]
\end{document}

In the MWE, you will find the font of the page number, is still serif in the first page of Introduction.

Another style (see below) which is similar to the first one, the only difference is that the vertical line | up to the top edge.

Even page:
enter image description here

Odd page:
enter image description here

Is it possible to make the second style?

Best Answer

I would suggest using titleps for this, since it allows easy access to sectional titles:

enter image description here
Click to enlarge

\documentclass[twoside]{book}
\usepackage{titleps,lipsum}% http://ctan.org/pkg/{titleps,lipsum}
\newpagestyle{main}{%
  \sethead
    [\thepage\quad$\vert$\quad\chaptertitle]% even-left
    []% even-centre
    []% even-right
    {}% odd-left
    {}% odd-centre
    {\thesection~\sectiontitle\quad$\vert$\quad\thepage}% odd-right
}
\pagestyle{main}
\begin{document}
\chapter{A chapter}\lipsum[1-10]
\section{A section}\lipsum[1-10]
\end{document}

Of course, you can add your own formatting, as needed. For example, replacing $\vert$ with \smash{\rule[-.5ex]{1pt}{5in}} produces a vertical rule that sticks down/up -0.5ex/5in. With everything outside the page boundary being clipped, a long enough vertical rule is okay.

Related Question