在加载 hyphenat 包并更改标题文本字体设置之前

在加载 hyphenat 包并更改标题文本字体设置之前

在加载 hyphenat 包并更改标题文本字体设置之前

在加载hyphenat包并更改标题文本字体设置之前,我的标题文本已正确对齐,如下图所示:
加载 hyphenat 包之前的输出
以下是 MWE,这是@Simon Dispa 提供的针对我的问题的解决方案标题文字的对齐

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

\newlength{\leftspace}% added <<<<<<<<<<<<<<<<
\setlength{\leftspace}{18ex}% added <<<<<<<<<<<<<<<<    
\newlength{\xindent}
\settowidth{\xindent}{\thechapter.\thesection\:}% added <<<<<<<<<<<<<<<<
\usepackage{varwidth}% added <<<<<<<<<<<<<<<<
\fancypagestyle{fancy}{%
    \fancyhf{}
    \fancyhead[OL]{\leftmark}
    \fancyhead[ER]{\begin{varwidth}{\dimexpr\textwidth-\leftspace}\parindent-\xindent\rightmark\end{varwidth}}% changed <<<<<
    \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}
    
    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}

加载 hyphenat 包并更改标题文本字体设置后

hyphenat通过命令加载包后\usepackage[none]{hyphenat},我的文档的标题文本现在出现错位。此外,我认为标题文本字体设置\myheaderfooterfont是造成这种错位的原因。以下是 MWE,这是 @Simon Dispa 在以下位置提供的解决方案:标题文字的对齐已加载包hyphenat并包含字体设置。

\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[none]{hyphenat} %added package
\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}

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


\fancypagestyle{fancy}{%
    \fancyhf{}
    \fancyhead[OL]{\leftmark}
    \fancyhead[ER]{\begin{varwidth}{\dimexpr\textwidth-\leftspace}\parindent-\xindent\myheaderfooterfont\rightmark\end{varwidth}}% changed <<<<<
    \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}
    
    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}

答案1

摘自hyphenat手册第 2 页:

hyphenat软件包有一个名为 的选项none。使用此选项可防止整个文档中的任何连字符。如果阻止 TEX 连字符,它可能会抱怨换行不当,并且您可能会发现文本的片段伸出到页边距。如果您使用此选项,您还应该考虑使用\sloppyand/or \raggedright

使用\raggedright

b

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

\newlength{\leftspace}% 
\setlength{\leftspace}{18ex}%
\newlength{\xindent}
\settowidth{\xindent}{\thechapter.\thesection\:}% 
\usepackage{varwidth}% 


\fancypagestyle{fancy}{%
    \fancyhf{}
    \fancyhead[OL]{\leftmark}
    \fancyhead[ER]{\begin{varwidth}{\dimexpr\textwidth-\leftspace}\raggedright\parindent-\xindent\rightmark\end{varwidth}}% changed <<<<<
    \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}% added <<<<<<<<<<<<<
\raggedright % added <<<<<<<<<<<<<

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

更新更改标题的字体

F

\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 <<<<<          
\newcommand*\myheaderfooterfont{\normalfont\bfseries\selectfont}%font settings <<<<<<<<<<<<<<<<<<   

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

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

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

\usepackage[none]{hyphenat}% added <<<<<<<<<<<<<
\raggedright % added <<<<<<<<<<<<<

\begin{document} 
    
    \chapter{ABCD}
    
    eehehdhdhdhdddddd ddddddddddddddddddddd
    
    \pagebreak
    
    \section{Special keyboard characters gghthr rhrrhd fheheh fhehuw fhewiwi feiwiwd fiiwiwid fiiwiwi firiifn rokfkkvfvv fjjjeje}   
    
    
    \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}

相关内容