多行表头的垂直对齐

多行表头的垂直对齐

在撰写论文时,我有一些章节的名称很长。使用 fancyhdr 指定页眉时,我将左侧(偶数)页设置为包含章节名称,将右侧(奇数)页设置为包含节名称。问题是,有时我的标题很长,页眉会分成两行。结果,第二行与页面左侧的页码对齐,而不是与文本对齐。像这样:

在此处输入图片描述

这是奇数页

在此处输入图片描述

因此,我想根据页眉上的红色垂直线对齐文本,然后将页码保留在最左侧(偶数页)或最右侧(奇数页)。偶数页的问题更为突出,但我也想解决奇数页的问题。

我的MWE

\documentclass[a4paper,10pt, twoside,titlepage,english]{book}                                       %
\usepackage{fancyhdr}
\usepackage[english]{babel} % for proper hyphenation patterns
\usepackage{titlesec} % 


\usepackage{geometry}
    \setlength{\textwidth}{12.5cm}
    \setlength{\textheight}{19.7cm}
    \setlength{\oddsidemargin}{3.8cm} % Margen de las paginas impares  
    \setlength{\evensidemargin}{-0.74cm} % Margen de las paginas pares 
    \setlength{\topmargin}{-1.3cm} 
    \setlength{\headheight}{8pt}
    \setlength{\footskip}{0cm}

\pagestyle{fancy}%
\addtolength{\headheight}{10pt}%
\renewcommand{\chaptermark}[1]{\markboth{Chapter  \thechapter.\ #1 }  {}}%
\renewcommand{\sectionmark}[1]{\markright{\thesection.\ #1}}%

\fancyhf{}%

\fancyhead[LE]{\fontfamily{phv} \footnotesize  \thepage \qquad \textsc \leftmark}%
\fancyhead[RO]{\fontfamily{phv} \footnotesize \textsc \rightmark \qquad \thepage}%

\renewcommand{\headrulewidth}{0pt}% 
\renewcommand{\footrulewidth}{0pt}% 
\addtolength{\headheight}{0.5pt}%




\titleformat{\chapter}[display] % cambiamos el formato de los capítulos
{\bfseries \Huge} % por defecto se usarán caracteres de tamañao \Huge en negrita
{ % contenido de la etiqueta
\filleft %texto alineado a la derecha
\fontfamily{phv}\Large\chaptertitlename \hspace{0.1cm}   % Formato de la palabra "Capítulo"
\Huge\thechapter % Formato del número del capítulo
}
{0mm} % espacio mínimo entre etiqueta y cuerpo
{\filleft} % Formato del texto del capítulo


\titlespacing*{\section}{0in}{12pt}{8pt}
\titlespacing*{\chapter}{0cm}{0cm}{0cm}
\titlespacing*{\subsection}{0in}{12pt}{8pt}

\parskip=8.5pt

\begin{document}


\chapter{A very very long chapter that will definitely need to be shown in two or more lines in the header}

And this is what the header would look like.
\newpage
\section{And this is a new section that hopefully will also expand in two or more lines in the header section, so that you can definitely see what I am talking about}

This is the new section.
\newpage 

Same as before for odd pages.

\end{document}

非常感谢您的帮助

答案1

你可以重新定义花式头部:

\fancyhead[LE,RO]{\fontfamily{phv} \footnotesize \textsc \thepage }%
\fancyhead[RE]{\parbox[t]{0.95\textwidth}{\fontfamily{phv} \footnotesize  \textsc \leftmark}}%
\fancyhead[LO]{\parbox[b]{0.95\textwidth}{\fontfamily{phv} \footnotesize \textsc \rightmark}}%

这会将页码放在页面的左侧(偶数)或右侧(奇数),并将标记括在 中parbox以限制其宽度。 的内容parbox可以是顶部对齐[t]或底部对齐[b]

A

b

\documentclass[a4paper,10pt, twoside,titlepage,english]{book}                                       %
\usepackage{fancyhdr}
\usepackage[english]{babel} % for proper hyphenation patterns
\usepackage{titlesec} % 
    
\usepackage[showframe]{geometry}% changed to show the margins <<<<<<<<<<<<<<<<<<<<<<<<

\setlength{\textwidth}{12.5cm}
\setlength{\textheight}{19.7cm}
\setlength{\oddsidemargin}{3.8cm} % Margen de las paginas impares  
\setlength{\evensidemargin}{-0.74cm} % Margen de las paginas pares 
\setlength{\topmargin}{-1.3cm} 
\setlength{\headheight}{8pt}
\setlength{\footskip}{0cm}

\pagestyle{fancy}%
\addtolength{\headheight}{10pt}%
\renewcommand{\chaptermark}[1]{\markboth{Chapter  \thechapter.\ #1 }  {}}%
\renewcommand{\sectionmark}[1]{\markright{\thesection.\ #1}}%

\fancyhf{}%

\fancyhead[LE,RO]{\fontfamily{phv} \footnotesize \textsc \thepage }% added <<<<<<<<<<<<<<<<<<<<<<<<
\fancyhead[RE]{\parbox[t]{0.95\textwidth}{\fontfamily{phv} \footnotesize  \textsc \leftmark}}% added <<<<<<<<<<<<<<<<<<<<<<<<
\fancyhead[LO]{\parbox[b]{0.95\textwidth}{\fontfamily{phv} \footnotesize \textsc \rightmark}}%a added <<<<<<<<<<<<<<<<<<<<<<<<

\renewcommand{\headrulewidth}{0pt}% 
\renewcommand{\footrulewidth}{0pt}% 
\addtolength{\headheight}{0.5pt}%


\titleformat{\chapter}[display] % cambiamos el formato de los capítulos
{\bfseries \Huge} % por defecto se usarán caracteres de tamañao \Huge en negrita
{ % contenido de la etiqueta
    \filleft %texto alineado a la derecha
    \fontfamily{phv}\Large\chaptertitlename \hspace{0.1cm}   % Formato de la palabra "Capítulo"
    \Huge\thechapter % Formato del número del capítulo
}
{0mm} % espacio mínimo entre etiqueta y cuerpo
{\filleft} % Formato del texto del capítulo


\titlespacing*{\section}{0in}{12pt}{8pt}
\titlespacing*{\chapter}{0cm}{0cm}{0cm}
\titlespacing*{\subsection}{0in}{12pt}{8pt}

\parskip=8.5pt

\begin{document}
    
    
    \chapter{A very very long chapter that will definitely need to be shown in two or more lines in the header}
    
    And this is what the header would look like.
    \newpage
    \section{And this is a new section that hopefully will also expand in two or more lines in the header section, so that you can definitely see what I am talking about}
    
    This is the new section.
    \newpage 
    
    Same as before for odd pages.
    
\end{document}

相关内容