![编辑](https://linux22.com/image/321823/%E7%BC%96%E8%BE%91.png)
有时我的章节和部分名称很长。将它们写在页眉中会导致重叠问题。
所以我希望标题中有两行。我希望章节号和名称在上面一行,节号和名称在第二行,两者都左对齐。
我该怎么做?我愿意接受任何其他解决方案。
我的问题的最小工作示例:
\documentclass[12pt,a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage{fancyhdr} % Header
\pagestyle{fancy}
\setlength\headheight{26pt}
\renewcommand{\headrulewidth}{2pt}
\begin{document}
\chapter{A VERY VERY VERY LONG CHAPTER NAME}
\section{ALSO VERY LONG CHAPTER NAME}
Some text...
\newpage
\section{ANOTHER SECTION}
Some text...
\end{document}
我的问题图片:
答案1
编辑
如果我这样做,我会考虑两种选择。
选项1
如果要双面打印,请将章节放在偶数页,将部分放在奇数页。例如:
\documentclass[12pt,a4paper,twoside]{report}
\usepackage[utf8]{inputenc}
\usepackage[headheight=14pt]{geometry}% don't set these manually but, if you do, you need to ensure that you change the layout dimensions consistently e.g. if headheight is bigger, what else are you going to make smaller to compensate? geometry does this automagically
\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\headrulewidth}{2pt}
\fancyhf[lh,rh,ch]{}
\fancyhf[leh]{\leftmark}
\fancyhf[roh]{\rightmark}
\begin{document}
\chapter{A VERY VERY VERY LONG CHAPTER NAME}
\section{ALSO VERY LONG CHAPTER NAME}
Some text...
\newpage
\section{ANOTHER SECTION}
Some text...
\newpage
More text.
\end{document}
选项 2
[如果需要可以与选项 1 结合使用。]
使用较短版本的章节或节标题作为页眉:
\documentclass[12pt,a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage[headheight=14pt]{geometry}% don't set these manually but, if you do, you need to ensure that you change the layout dimensions consistently e.g. if headheight is bigger, what else are you going to make smaller to compensate? geometry does this automagically
\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\headrulewidth}{2pt}
\fancyhf[lh,rh,ch]{}
\fancyhf[lh]{\leftmark}
\fancyhf[rh]{\rightmark}
\begin{document}
\chapter[Shortened Long Name]{A VERY VERY VERY LONG CHAPTER NAME}
\section[Abbreviated Name]{ALSO VERY LONG CHAPTER NAME}
Some text...
\newpage
\section{ANOTHER SECTION}
Some text...
\newpage
More text.
\end{document}
笔记
无论选择哪种方法,都不应将章节标题的内容全部大写。默认情况下,它们在运行标题中都是大写的。如果您需要将实际章节标题大写,请适当自定义这些章节标题的格式。titlesec
是一个流行的软件包,可以执行此操作。
例如:
\documentclass[12pt,a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage[headheight=14pt]{geometry}% don't set these manually but, if you do, you need to ensure that you change the layout dimensions consistently e.g. if headheight is bigger, what else are you going to make smaller to compensate? geometry does this automagically
\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\headrulewidth}{2pt}
\fancyhf[lh,rh,ch]{}
\fancyhf[lh]{\leftmark}
\fancyhf[rh]{\rightmark}
\usepackage[uppercase]{titlesec}
\begin{document}
\chapter[Shortened Long Name]{A Very Very Very Long Chapter Name}
\section[Abbreviated Name]{Also Very Long Chapter Name}
Some text..
\end{document}
生产
原始答案
请注意,我确实不是推荐这个。我认为对这个问题有更好的答案。但是,如果你必须这样做,你可以。
不要手动更改布局尺寸。如果这样做,您需要确保所有内容仍然构成合理的布局。如果增加了标题高度,您真的希望所有额外空间都来自底部边距吗?geometry
会做一些更明智的事情……
为此,您需要至少 28pt 的标题高度 - 26pt 是不够的。
\documentclass[12pt,a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage[headheight=28pt]{geometry}% don't set these manually but, if you do, you need to ensure that you change the layout dimensions consistently e.g. if headheight is bigger, what else are you going to make smaller to compensate? geometry does this automagically
\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\headrulewidth}{2pt}
\fancyhf[lh,rh,ch]{}
\fancyhf[lh]{\begin{minipage}[b]{\textwidth}\raggedright\leftmark\\\rightmark\\\end{minipage}}
\begin{document}
\chapter{A VERY VERY VERY LONG CHAPTER NAME}
\section{ALSO VERY LONG CHAPTER NAME}
Some text...
\newpage
\section{ANOTHER SECTION}
Some text...
\end{document}
答案2
更简单:只需\markboth
在章节或部分后添加:
\documentclass[12pt,a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage{fancyhdr} % Header
\pagestyle{fancy}
\setlength\headheight{27.06pt}
\renewcommand{\headrulewidth}{2pt}
\begin{document}
\chapter{A VERY VERY VERY LONG CHAPTER NAME}
\markboth{A VERY VERY LONG \\ CHAPTER NAME}{}
\section{ALSO VERY LONG CHAPTER NAME}
Some text...
\newpage
\section{ANOTHER SECTION}
Some text...
\end{document}
我认为这是你最初的想法:
答案3
使用以下代码。它将自行处理短章节名称和长章节名称的长度。
\documentclass[12pt,a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage{fancyhdr} % Header
\usepackage{tabularx}
\pagestyle{fancy}
\renewcommand{\headrulewidth}{2pt}
\fancyhf[lh,rh,ch]{}
\fancyhf[lh]{\begin{minipage}[b]{\textwidth}
\begin{tabularx}{1\textwidth} {
>{\raggedright\arraybackslash}X
>{\raggedleft\arraybackslash}X
}
\textit{\rightmark}&\textit{\leftmark}\\
\end{tabularx}
\end{minipage}}
\begin{document}
\chapter{A VERY VERY VERY LONG CHAPTER NAME}
\section{ALSO VERY LONG CHAPTER NAME}
Some text...
\newpage
\section{ANOTHER VERY VERY LONG SECTION NAME}
Some text...
\chapter{SHORT}
\section{ALSO VERY SHORT CHAPTER NAME}
Some text...
\newpage
\section{SHORT SECTION}
Some text...
\newpage
\section{A VERY LONG SECTION NAME IN A SHORT CHAPTER}
Some text...
\end{document}