0

Currently, the Section header text indentation is hanged thanks to @Simon Dispa solution at Misalignment caused by hyphenat package and header text font settings. I also want to have my Chapter header text indentation be hanged just like that of the Section. Below is the MWE:

\documentclass[12pt,openany,twoside]{book}
\raggedbottom
\usepackage[ 
left=2.5cm,
right=2.5cm, 
top=3cm, 
bottom=3cm,
headheight = 3\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}


\newcommand*\myheaderfooterfont{\normalfont\bfseries\selectfont}
\newlength{\leftspace}% 
\setlength{\leftspace}{18ex}%
\newlength{\xindent}
\settowidth{\xindent}{\myheaderfooterfont\thechapter.\thesection\:}
\usepackage{varwidth}% 


\fancypagestyle{fancy}{%
    \fancyhf{}
    \fancyhead[OL]{\leftmark}
    \fancyhead[ER]{\begin{varwidth}{\dimexpr\textwidth-\leftspace}\myheaderfooterfont\raggedright\parindent-\xindent\rightmark\end{varwidth}}
    \fancyfoot{}
    \fancyfoot[RE,RO]{\thepage}
    \renewcommand\headrule
    {{     \color{blue}\rule{1\headwidth}{2.5pt}%       
    }}
}

\fancypagestyle{plain}{%
    \fancyhf{}%
    \renewcommand\headrule
    {{     \color{blue}\rule{1\headwidth}{0pt}%         
    }}      
}

\pagestyle{fancy}   

\usepackage{showframe}

\usepackage[none]{hyphenat}
\raggedright

\begin{document} 
    %<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<added
    \frontmatter
    \chapter*{ABC}
    \markboth{ABC}{ABC}
    eeeheeyeyeyeyeyeeyeye
    \section*{DEG}
    dddheheheheheheheehehehe
    \pagebreak
    \section*{KJ}
    ddhheheheheheehehehheheheheheh
    \pagebreak
    \section*{KDH}
    dhddhhehehehehehehehehehehe

    \mainmatter
    %<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<added
    \chapter{ABCD ffhfgrhr ejejejeje fhfhfhf ehehehehe dhddhdhellgl rkttkgkhihi eeeejejwnwn hhlhlnnmgjgj rjrrjoaoaoao rjjrjrrrj wwjwwwjwjw}
    
    eehehdhdhdhdddddd ddddddddddddddddddddd
    
    \pagebreak
    
    \section{Special keyboard characters gghthr rhrrhd fheheh fhehuw fhewiwi feiwiwd fiiwiwid fiiwiwi firiifn rokfkkvfvv fjjjeje jejeje djej}   
    
    
    \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. 
    
    \clearpage
    
    Special keyboard characters dhgdgdg dgdgdgdg ggdgdgd egegegege dgddgdgd eggegegeg dgdgdgdgd egegegeg dggeieieirr tktii43iwje rfrhrhrhr gtktotyiyighgjgj fkf      
Some text.
\end{document}

1 Answer 1

1

UPDATE Changing the header font.

Try this code. aa

bb

zz

Last update: matching spaces and taller headheight.

cc

% !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}% added <<<<<      
\newcommand*\myheaderfooterfont{\sffamily\bfseries\selectfont}  

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

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

\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[RE,RO]{\thepage}
    \renewcommand\headrule{\color{blue}\rule{1\headwidth}{2.5pt}}
}
%****************************************

\fancypagestyle{plain}{%
    \fancyhf{}%
    \renewcommand\headrule
    {{     \color{blue}\rule{1\headwidth}{0pt}%         
    }}      
}

\pagestyle{fancy}   

\usepackage{showframe}

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

\begin{document} 
    
    \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}
8
  • 1
    Using pdflatex and \usepackage{helvet} I fail to see any misalignment. Try the last update wit the dot removed. Aug 6, 2022 at 19:28
  • The font issue has been solved by the recently updated code. However the following 2 issues remain 1. However, long chapter headers are shifting the head rule downwards which is something I do not like. Long section headers are not causing this shifting of head rules which is good. 2. The space (in the headers) between the section number and the title seems to be a little big, is it possible to reduce it so as to match that (in the header) between the chapter number and title? Can you please assist?
    – itc
    Aug 6, 2022 at 19:58
  • @itc Increase the headheight. For example headheight = 3.5\baselineskip,. or more if you want to have more lines in the header Aug 7, 2022 at 16:57
  • 1
    @itc I do not see that effect. Just in case use the last version of my answer. Aug 7, 2022 at 17:27
  • 1
    @itc Could be a topic for another question. Aug 7, 2022 at 18:23

You must log in to answer this question.

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