This is the header of my two-side document. As you can see when the number increases the vertical line that separates the pagenumber from the chapter name is shifted a bit inward. I would like to have the position of this line fixed and aligned with the text box of the page. This is my code:

\documentclass [11pt, a4paper, twoside, openright]{book}

\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{geometry}
\usepackage{xcolor}

\geometry{
inner=2.4cm, % Inner margin
outer=4cm, % Outer margin
top=3.5cm, % Top margin
bottom=3cm, % Bottom margin
heightrounded,
bindingoffset=4mm,
showframe,
}

\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}}
\fancyhf{}

• I can't reproduce the picture: the page numbers are flush with the left margin (on even numbered pages). – egreg May 29 '18 at 21:21
• @egreg try now, I added the geometry part – Andrea May 29 '18 at 21:33
• The same. You're missing headheight=13.6pt, by the way. You must have some fancyhdr command to offset the page number. – egreg May 29 '18 at 21:37
• @egreg I don't have headheight=13.6pt in my code and the pagenumber offest is obtained with the bindingoffset=4mm – Andrea May 29 '18 at 22:00
• You should have headheight=13.6pt (look in the log file to know why). The offset of the page numbers can't be obtained with bindingoffset. – egreg May 29 '18 at 22:12

If you want to offset the page number in the margin, you need to use \fancyhfoffset. Enclose the page number in a box as wide as the offset (and big enough to contain the widest Roman page number).

\documentclass [11pt, a4paper, twoside, openright]{book}

\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{geometry}
\usepackage{xcolor}
\usepackage{fancyhdr}

\usepackage{kantlipsum}

\geometry{
inner=2.4cm, % Inner margin
outer=4cm, % Outer margin
top=3.5cm, % Top margin
bottom=3cm, % Bottom margin
heightrounded,
bindingoffset=4mm,
showframe,
}

\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}}
\fancyhf{}
\fancyhfoffset[LE]{20pt}
\fancyhfoffset[RO]{20pt}
\makebox[20pt][l]{\thepage}%
{\color{gray}\makebox[0pt]{\textbar}\hspace{5pt}\nouppercase{\leftmark}}%
}
{\color{gray}\nouppercase{\rightmark}\hspace{5pt}\makebox[0pt]{\textbar}}%
\makebox[20pt][r]{\thepage}%
}

\begin{document}

\frontmatter

\chapter{Introduction}
\section{Abstract}

\kant[1-40]

\end{document}


I also set the bar in a zero width box.