0

Problem Description

I wish to set a fixed space between header/footer text and the headrule/footrule. I have gone through Setting same distance between header rule, footer rule and text and it is not what I want exactly. For the headers, the space between the last line of the long header text and the headrule is different from the space between single lined header text and the corresponding headrule. In other words, can we fix this spacing so that it remain the same for both single line (short header text) and multiline (long header text) header text? Given that the solution to the header text and headrule has been found, I want to apply that fixed spacing used between headrule and header text to footrule and footer text as well. Below is my MWE:

% !TeX TS-program = pdflatex    
\documentclass[12pt,openany,twoside]{book}
\raggedbottom
\usepackage[ 
left=2.5cm,
right=2.5cm, 
top=3cm, 
bottom=3cm,
headheight = 3.5\baselineskip,
headsep = 5mm,
a4paper
]{geometry}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage[table]{xcolor}
\usepackage{graphicx}
\newcommand\HRule{\noindent\rule{\linewidth}{1.5pt}}
\usepackage{background}
\backgroundsetup{contents={}}
\usepackage{fancyhdr}
\usepackage{hhline} 

\usepackage{helvet}


\usepackage{tabularx}%      
\newcommand*\myheaderfooterfont{\sffamily\bfseries\selectfont}  

\newlength{\leftspace}% for sections <<<<<<<<<
\setlength{\leftspace}{20ex}%set

\newlength{\Lheader}
\renewcommand{\chaptermark}[1]{\markboth{\uppercase{#1}}{}}%  
\renewcommand{\sectionmark}[1]{\markright{\uppercase{#1}}}%

\fancypagestyle{fancy}{%
    \fancyhf{}  
    \fancyhead[OL]{\myheaderfooterfont%
        \ifnum\value{chapter}>0%
        \begin{tabularx}{\textwidth}{@{}lX}\uppercase{Chapter~\thechapter}&\raggedright\leftmark\end{tabularx}% changed <<<<<<<<<<
        \else\raggedright\leftmark\relax\fi}
    \fancyhead[ER]{\myheaderfooterfont%
        \settowidth{\Lheader}{\thesection\hspace*{12pt}\rightmark}  
        \ifnum\value{chapter}>0%
        \ifdim\Lheader>\linewidth%          
        \begin{tabularx}{\dimexpr\textwidth-\leftspace}{@{}lX} \thesection &\raggedright\rightmark\end{tabularx}%
        \else\begin{tabular}{@{}ll@{}} \thesection &\raggedright\rightmark\end{tabular}%
        \fi\else\hfill\raggedright\rightmark\relax\fi
    }% changed <<<<<
    
    \fancyfoot{}
    \fancyfoot[RE,RO]{\myheaderfooterfont\thepage}%last added<<<<<<<
%<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<last added
    \renewcommand\headrule
{{     \color{blue}\rule{0.33\headwidth}{2.5pt}%   <---
       \color{red}\rule{0.34\headwidth}{2.5pt}%    <---
       \color{orange}\rule{0.33\headwidth}{2.5pt}% <---
}}
%<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<last added
}

\fancypagestyle{plain}{%
    \fancyhf{}%
    \renewcommand\headrule
    {{     \color{blue}\rule{1\headwidth}{0pt}%         
    }}   
    \fancyfoot{}
    \fancyfoot[RE,RO]{\myheaderfooterfont\thepage} %last added<<<<<<  
}

\pagestyle{fancy}   
\renewcommand{\footrulewidth}{1pt}

\usepackage{showframe}

\usepackage[none]{hyphenat}
\raggedright %  important with  none hyphenat + none !!!!!!!!!!!!!!!!!

\begin{document} 
%<<<<<<<<<<<<<<<<<<<<<<<<<last added
    \begin{titlepage}
        \setlength{\parindent}{0in} 
        \topskip 360pt
        \vspace*{\fill}
        
        \centering 
        
        \scshape 
     
        
        
        \begin{flushleft}
            
            { \textbf{HHHHHHHHHHHHHHHHHHHHHHHHGFFFFFFFFFFF}
            }
            
        \end{flushleft}
        
        \vspace*{3\baselineskip} 
        
        
        
        \textbf{AUTHOR}
        
        
        \vspace{0.5\baselineskip} 
        
        {\scshape\Large\selectfont LKJJHHHG} 
        
        \vspace{1.5\baselineskip} 
        
        
        
        \textbf{PFEYTYU}
        
        
        \vspace{1\baselineskip} 
        
        {\scshape\Large\selectfont JKIU} 
        
        \vspace{2\baselineskip} 
        
        
        
        \textbf{YUUUUH}
        
        
        
        
        \vspace{0.5\baselineskip} 
        
        {\scshape\Large\selectfont 12345} 
        
        \vspace{0.5\baselineskip} 
        
        \vfill 
        
        
        
        
        \vspace{4\baselineskip} 
        \scshape 
        Revised: \today 
        
        
    \end{titlepage}
    %<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<last added
    \frontmatter
    \chapter*{ABC}
    
    \markboth{ABC1}{ABC2}
    
    eeeheeyeyeyeyeyeeyeye
    \section*{DEG}
    dddheheheheheheheehehehe
    \pagebreak
    \section*{KJ}
    ddhheheheheheehehehheheheheheh
    \pagebreak
    \section*{KDH}
    dhddhhehehehehehehehehehehe
    
    \mainmatter
    
    \chapter{ABCD ffhfgrhr ejejejeje fhfhfhf ehehehehe dhddhdhellgl rkttkgkhihi eeeejejwnwn hhlhlnnmgjgj rjrrjoaoaoao  }        
    eehehdhdhdhdddddd ddddddddddddddddddddd     
    
    \newpage                
    \section{Special keyboard characters gghthr rhrrhd fheheh fhehuw fhewiwi feiwiwd fiiwiwid fiiwiwi}  
    
    \LaTeX{} hasmany symbols at its disposal. The majority of them are within the mathematical domain as shown in \eqref{eq:1} and \eqref{eq:2}. The ten special keyboard characters are: \verb|#|, \verb|$|, \verb|%|, \verb|&|, \verb|~|, \verb|_|, \verb|^|, \verb|\| and  \verb|{}|. If you simply want the character to be printed just as any other letter, include a \verb|\ | in front of the character. For example, \verb|\$| will produce \verb|$| in your output.
    The exception to the rule is the \verb|\ | itself because \verb|\\ | has its own special meaning. A \verb|\ | is produced by typing \verb|$\backslash$| in your file.       
    \newpage        
    \section{Special keyboard characters}       
    Special keyboard characters dhgdgdg dgdgdgdg ggdgdgd egegegege dgddgdgd eggegegeg dgdgdgdgd egegegeg dggeieieirr tktii43iwje rfrhrhrhr gtktotyiyighgjgj fkf     
    \newpage        
    Special keyboard characters dhgdgdg dgdgdgdg ggdgdgd egegegege dgddgdgd eggegegeg dgdgdgdgd egegegeg dggeieieirr tktii43iwje rfrhrhrhr gtktotyiyighgjgj fkf
    
    \chapter{One short line}        
    \LaTeX{} has many symbols at its disposal. The majority of them are within the mathematical domain as shown in \eqref{eq:1} and \eqref{eq:2}. The ten special keyboard characters are: \verb|#|, \verb|$|, \verb|%|, \verb|&|, \verb|~|, \verb|_|, \verb|^|, \verb|\| and  \verb|{}|. If you simply want the character to be printed just as any other letter, include a \verb|\ | in front of the character. For example, \verb|\$| will produce \verb|$| in your output.
    The exception to the rule is the \verb|\ | itself because \verb|\\ | has its own special meaning. A \verb|\ | is produced by typing \verb|$\backslash$| in your file. 
    
    \section{Special keyboard characters}   
    Special keyboard characters dhgdgdg dgdgdgdg ggdgdgd egegegege dgddgdgd eggegegeg dgdgdgdgd egegegeg dggeieieirr tktii43iwje rfrhrhrhr gtktotyiyighgjgj fkf     
    \newpage        
    Special keyboard characters dhgdgdg dgdgdgdg ggdgdgd egegegege dgddgdgd eggegegeg dgdgdgdgd egegegeg dggeieieirr tktii43iwje rfrhrhrhr gtktotyiyighgjgj fkf     
    \newpage        
    Special keyboard characters dhgdgdg dgdgdgdg ggdgdgd egegegege dgddgdgd eggegegeg dgdgdgdgd egegegeg dggeieieirr tktii43iwje rfrhrhrhr gtktotyiyighgjgj fkf     
    
\end{document}
2
  • 1
    When you say footer text is the footer baseline or the footer top? Aug 7, 2022 at 22:39
  • I mean the top of the footer text not the baseline. And thank you for asking.
    – itc
    Aug 7, 2022 at 22:43

1 Answer 1

1

EDIT: Code adapted to the latest MWE. Complete code added. See below.

I'll explain what is happening. Your \headrule uses the \rule command. This is a "horizontal" command, which means it becomes part of the paragraph that contains the header text. It goes to a new line of this paragraph because it wouldn't fit next to the header text, and of course, that is what you want. But TeX then makes sure that the distance of the baseline of the header text and the baseline of this rule is at least \baselineskip. That is why there is such a big distance between the header and the rule in the single line header.

In the case of the multiline header with \tabular(x), the baseline of the header is the first line of the multiline header. So the distance of the header baseline and the rule is already greater than \baselineskip and no extra space is added.

The standard definition of \headrule in fancyhdr use the \hrule command, which is a vertical mode command. Therefore it is not part of the paragraph and it is just tacked to the bottom of the header text. (The distance between the baseline and the rule is guaranteed by a \strut.)

So one solution would be to define your \headrule with \hrule:

\renewcommand\headrule{\color{blue}\hrule height 2.5pt}

Another possibility is to include the command \nointerlineskip before your \rule. This tells Tex not to add the extra \baselineskip.

\renewcommand\headrule{\nointerlineskip\color{blue}\rule{\headwidth}{2.5pt}}

Now for the footer:

The distance between the \footrule and the footer text is defined by footruleskip. This is not the distance between the rule and the top of the text, however, because there is also a \strut in the footer. This id done to get a uniform distance between the footrule and the baseline of the footer. Otherwise this distance would depend on whether there are capital letters in the footer or only lowercase letters. I think this would not be aesthetic. So I suggest to keep this. It would be difficult to remove the \strut anyway. In your case if you have only numbers in the footer (and in this font they all have the same height), it wouldn't matter.

You could measure the height of the digits, but I think just eyeballing is good enough. I suggest:

\renewcommand{\footruleskip}{3pt}

Complete code (my changes indicated by %%%%%%%% PVO %%%%%%%%%%)

\documentclass[12pt,openany,twoside]{book}
\raggedbottom
\usepackage[ 
left=2.5cm,
right=2.5cm, 
top=3cm, 
bottom=3cm,
headheight = 3.5\baselineskip,
headsep = 5mm,
a4paper
]{geometry}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage[table]{xcolor}
\usepackage{graphicx}
\newcommand\HRule{\noindent\rule{\linewidth}{1.5pt}}
\usepackage{background}
\backgroundsetup{contents={}}
\usepackage{fancyhdr}
\usepackage{hhline} 

\usepackage{helvet}


\usepackage{tabularx}%      
\newcommand*\myheaderfooterfont{\sffamily\bfseries\selectfont}  

\newlength{\leftspace}% for sections <<<<<<<<<
\setlength{\leftspace}{20ex}%set

\newlength{\Lheader}
\renewcommand{\chaptermark}[1]{\markboth{\uppercase{#1}}{}}%  
\renewcommand{\sectionmark}[1]{\markright{\uppercase{#1}}}%

\fancypagestyle{fancy}{%
    \fancyhf{}  
    \fancyhead[OL]{\myheaderfooterfont%
        \ifnum\value{chapter}>0%
        \begin{tabularx}{\textwidth}{@{}lX}\uppercase{Chapter~\thechapter}&\raggedright\leftmark\end{tabularx}% changed <<<<<<<<<<
        \else\raggedright\leftmark\relax\fi}
    \fancyhead[ER]{\myheaderfooterfont%
        \settowidth{\Lheader}{\thesection\hspace*{12pt}\rightmark}  
        \ifnum\value{chapter}>0%
        \ifdim\Lheader>\linewidth%          
        \begin{tabularx}{\dimexpr\textwidth-\leftspace}{@{}lX} \thesection &\raggedright\rightmark\end{tabularx}%
        \else\begin{tabular}{@{}ll@{}} \thesection &\raggedright\rightmark\end{tabular}%
        \fi\else\hfill\raggedright\rightmark\relax\fi
    }% changed <<<<<
    
    \fancyfoot{}
    \fancyfoot[RE,RO]{\myheaderfooterfont\thepage}%last added<<<<<<<
%<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<last added
    \renewcommand\headrule
{{     \nointerlineskip %%%%%%%% PVO %%%%%%%%%%
       \color{blue}\rule{0.33\headwidth}{2.5pt}%   <---
       \color{red}\rule{0.34\headwidth}{2.5pt}%    <---
       \color{orange}\rule{0.33\headwidth}{2.5pt}% <---
}}
%<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<last added
}

\fancypagestyle{plain}{%
    \fancyhf{}%
    \renewcommand\headrule
    {{     \color{blue}\rule{1\headwidth}{0pt}%         
    }}   
    \fancyfoot{}
    \fancyfoot[RE,RO]{\myheaderfooterfont\thepage} %last added<<<<<<  
}

\pagestyle{fancy}   
\renewcommand{\footrulewidth}{1pt}
\renewcommand{\footruleskip}{3pt} %%%%%%%% PVO %%%%%%%%%%

\usepackage{showframe}

\usepackage[none]{hyphenat}
\raggedright %  important with  none hyphenat + none !!!!!!!!!!!!!!!!!

\begin{document} 
%<<<<<<<<<<<<<<<<<<<<<<<<<last added
    \begin{titlepage}
        \setlength{\parindent}{0in} 
        \topskip 360pt
        \vspace*{\fill}
        
        \centering 
        
        \scshape 
     
        
        
        \begin{flushleft}
            
            { \textbf{HHHHHHHHHHHHHHHHHHHHHHHHGFFFFFFFFFFF}
            }
            
        \end{flushleft}
        
        \vspace*{3\baselineskip} 
        
        
        
        \textbf{AUTHOR}
        
        
        \vspace{0.5\baselineskip} 
        
        {\scshape\Large\selectfont LKJJHHHG} 
        
        \vspace{1.5\baselineskip} 
        
        
        
        \textbf{PFEYTYU}
        
        
        \vspace{1\baselineskip} 
        
        {\scshape\Large\selectfont JKIU} 
        
        \vspace{2\baselineskip} 
        
        
        
        \textbf{YUUUUH}
        
        
        
        
        \vspace{0.5\baselineskip} 
        
        {\scshape\Large\selectfont 12345} 
        
        \vspace{0.5\baselineskip} 
        
        \vfill 
        
        
        
        
        \vspace{4\baselineskip} 
        \scshape 
        Revised: \today 
        
        
    \end{titlepage}
    %<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<last added
    \frontmatter
    \chapter*{ABC}
    
    \markboth{ABC1}{ABC2}
    
    eeeheeyeyeyeyeyeeyeye
    \section*{DEG}
    dddheheheheheheheehehehe
    \pagebreak
    \section*{KJ}
    ddhheheheheheehehehheheheheheh
    \pagebreak
    \section*{KDH}
    dhddhhehehehehehehehehehehe
    
    \mainmatter
    
    \chapter{ABCD ffhfgrhr ejejejeje fhfhfhf ehehehehe dhddhdhellgl rkttkgkhihi eeeejejwnwn hhlhlnnmgjgj rjrrjoaoaoao  }        
    eehehdhdhdhdddddd ddddddddddddddddddddd     
    
    \newpage                
    \section{Special keyboard characters gghthr rhrrhd fheheh fhehuw fhewiwi feiwiwd fiiwiwid fiiwiwi}  
    
    \LaTeX{} hasmany symbols at its disposal. The majority of them are within the mathematical domain as shown in \eqref{eq:1} and \eqref{eq:2}. The ten special keyboard characters are: \verb|#|, \verb|$|, \verb|%|, \verb|&|, \verb|~|, \verb|_|, \verb|^|, \verb|\| and  \verb|{}|. If you simply want the character to be printed just as any other letter, include a \verb|\ | in front of the character. For example, \verb|\$| will produce \verb|$| in your output.
    The exception to the rule is the \verb|\ | itself because \verb|\\ | has its own special meaning. A \verb|\ | is produced by typing \verb|$\backslash$| in your file.       
    \newpage        
    \section{Special keyboard characters}       
    Special keyboard characters dhgdgdg dgdgdgdg ggdgdgd egegegege dgddgdgd eggegegeg dgdgdgdgd egegegeg dggeieieirr tktii43iwje rfrhrhrhr gtktotyiyighgjgj fkf     
    \newpage        
    Special keyboard characters dhgdgdg dgdgdgdg ggdgdgd egegegege dgddgdgd eggegegeg dgdgdgdgd egegegeg dggeieieirr tktii43iwje rfrhrhrhr gtktotyiyighgjgj fkf
    
    \chapter{One short line}        
    \LaTeX{} has many symbols at its disposal. The majority of them are within the mathematical domain as shown in \eqref{eq:1} and \eqref{eq:2}. The ten special keyboard characters are: \verb|#|, \verb|$|, \verb|%|, \verb|&|, \verb|~|, \verb|_|, \verb|^|, \verb|\| and  \verb|{}|. If you simply want the character to be printed just as any other letter, include a \verb|\ | in front of the character. For example, \verb|\$| will produce \verb|$| in your output.
    The exception to the rule is the \verb|\ | itself because \verb|\\ | has its own special meaning. A \verb|\ | is produced by typing \verb|$\backslash$| in your file. 
    
    \section{Special keyboard characters}   
    Special keyboard characters dhgdgdg dgdgdgdg ggdgdgd egegegege dgddgdgd eggegegeg dgdgdgdgd egegegeg dggeieieirr tktii43iwje rfrhrhrhr gtktotyiyighgjgj fkf     
    \newpage        
    Special keyboard characters dhgdgdg dgdgdgdg ggdgdgd egegegege dgddgdgd eggegegeg dgdgdgdgd egegegeg dggeieieirr tktii43iwje rfrhrhrhr gtktotyiyighgjgj fkf     
    \newpage        
    Special keyboard characters dhgdgdg dgdgdgdg ggdgdgd egegegege dgddgdgd eggegegeg dgdgdgdgd egegegeg dggeieieirr tktii43iwje rfrhrhrhr gtktotyiyighgjgj fkf     
    
\end{document}

enter image description here

enter image description here

enter image description here

6
  • Thank you @Pieter van Oostrum. Can you have a look at the recent changes I made to my MWE? And can you please make use of the MWE I provided when devising the answer?
    – itc
    Aug 9, 2022 at 12:14
  • I used your original MWE, and just made these two changes: changed the definition of \headrule, and added \footruleskip, that's all. So I could post the whole code if you want, but it doesn't reveal anything more. This new code makes it much more complicated in my opinion. Aug 9, 2022 at 12:37
  • You mean the multicolored headrule is making it now complicated?
    – itc
    Aug 9, 2022 at 12:39
  • No, that multicolored code was added after my response. I meant the code with the \vspaces. See the new code in my answer. Aug 9, 2022 at 14:10
  • Thank you very much @Pieter van Oostrum. I am having challenges with the geometry package as well. I am kindly requesting you to take a look at the modifications I made to my question.
    – itc
    Aug 9, 2022 at 15:45

You must log in to answer this question.

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