0

I want my long header text to be justified. In the picture below:
Illustration
I have inserted a red line such that I want all the text to be on the right side of that line (justification). How do I achieve that? Below is my 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}
\fancypagestyle{fancy}{%
    \fancyhf{}
    \fancyhead[OL]{\leftmark}
    \fancyhead[ER]{\rightmark}
    \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}

\begin{document} 
    \chapter{ABCD}
    eehehdhdhdhddddddddddddddddddddddddddd
    \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. 

\end{document}

1 Answer 1

1

Put the right mark in a tabularx narrower than the line width by the desired left margin (14ex in this example).

UPDATE after follow up question.

Long and short section titles.

g

\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}


%****************************************
\usepackage{tabularx}% added <<<<<          

\newlength{\leftspace}% for sections <<<<<<<<<
\setlength{\leftspace}{14ex}%

\renewcommand{\sectionmark}[1]{\markright{\uppercase{#1}}}

\fancypagestyle{fancy}{%
    \fancyhf{}
    \fancyhead[OL]{\leftmark}
    \fancyhead[ER]{%
        \begin{tabularx}{\dimexpr\textwidth-\leftspace}{@{}lX}\thesection&\rightmark\end{tabularx}}% 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}

\begin{document} 
    
    \chapter{ABCD}
    
    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
    
    \setcounter{page}{4}
    
    \section{Special keyboard characters}   
    
    Some text.
\end{document}
9
  • I want all the text to be on the right of the red line. Just like the appearance of \section{Special keyboard characters gghthr rhrrhd fheheh fhehuw fhewiwi feiwiwd fiiwiwid fiiwiwi firiifn rokfkkvfvv fjjjeje jejeje djej}
    – itc
    Aug 4, 2022 at 13:14
  • @itc But it is that way now! (without a red line of course) Aug 4, 2022 at 13:21
  • The words SPECIAL, HEH & ROKFKKVFVV are not aligned properly in vertical sense.
    – itc
    Aug 4, 2022 at 13:32
  • 1
    @itc Please see the new updated answer. (Long and short titles) Aug 4, 2022 at 17:41
  • 1
    @itc Thank you for your feedback!! For the benefit of other readers if the answer meets your requirement as originally formulated, please accept by checking the green tickmark on left side of the answer. Then ask another question pointing to hyphenat Aug 4, 2022 at 19:20

You must log in to answer this question.

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