标题文字的对齐

标题文字的对齐

我希望我的长标题文本能够对齐。如下图所示:
插图
我插入了一条红线,这样我希望所有文本都位于该线的右侧(对齐)。我该如何实现?以下是我的 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

将右侧标记放置在tabularx比线宽更窄的位置,左边距应为所需的范围(14ex在此示例中)。

更新在后续问题之后。

长章节标题和短章节标题。

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}

相关内容