我正在尝试计算使用 创建的标题的高度fancyhdr
。实际格式有点复杂,取决于某些命令的返回值(无法进行硬编码)。我尝试根据字体大小和已知间距计算所需的高度。然而,这与预期不符fancyhdr
。
如果我故意将 headerheight 设置为太小,fancyhdr 会建议 header 的大小。此值每次都有效,但不随行数线性缩放。下面写了一个 MWE。
我有三个问题:
- 为什么这些价值观是这样的?
- 我该如何正确计算身高?
- 是否可以通过 LaTeX 代码获取 fancyhdr 的估计(预渲染)?
\documentclass[12pt,onecolumn,oneside]{article}
\usepackage[lmargin=1.5in, rmargin=1.0in, tmargin=1.0in, bmargin=1.0in]{geometry}
\usepackage{fancyhdr}
\renewcommand{\headrulewidth}{0pt}
\fancypagestyle{customone}{
\fancyhf{}
\lhead{
Line 1
}
}
\fancypagestyle{customtwo}{
\fancyhf{}
\lhead{
Line 1\\
Line 2
}
}
\fancypagestyle{customthree}{
\fancyhf{}
\lhead{
Line 1\\
Line 2\\
Line 3
}
}
\pagestyle{customtwo}
\begin{document}
\noindent The minimum required \textit{headheight} for \textit{customone} is 14.49998pt.\\
The minimum required \textit{headheight} for \textit{customtwo} is 27.04604pt.\\
The minimum required \textit{headheight} for \textit{customthree} is 41.5604pt.\\
The height difference between \textit{customone} and \textit{customtwo} is 12.54606pt.\\
The height difference between \textit{customtwo} and \textit{customthree} is 14.51436pt.\\
The heights divided by the number of lines are: 14.49998pt, 13.52302pt, 13.853466pt.
\end{document}
答案1
页眉/页脚内容设置在框内,而此框实际上并不构成常规文本框/内容的一部分。因此,它不会使用相同的基线跳过(行高和深度)进行设置。因此,为了让您看到页眉使用的一致头部高度,请将内容设置在 内tabular
,或使用\strut
s:
\documentclass[12pt]{article}
\usepackage[lmargin=1.5in, rmargin=1.0in, tmargin=1.0in, bmargin=1.0in]{geometry}
\usepackage{fancyhdr}
\renewcommand{\headrulewidth}{0pt}
\fancypagestyle{customone}{%
\fancyhf{}%
\lhead{%
\strut Line 1%
}%
}
\fancypagestyle{customtwo}{%
\fancyhf{}%
\lhead{%
\begin{tabular}{ c }
Line 1\\
Line 2
\end{tabular}%
}%
}
\fancypagestyle{customthree}{%
\fancyhf{}%
\lhead{%
\begin{tabular}{ c }
pine 1\\
Line 2\\
Line 3
\end{tabular}%
}%
}
\pagestyle{customone}
%\pagestyle{customtwo}
%\pagestyle{customthree}
\setlength{\parindent}{0pt}% Just for this example
\begin{document}
The minimum required \texttt{\string\headheight} for \texttt{customone} is 14.49998pt. \par
The minimum required \texttt{\string\headheight} for \texttt{customtwo} is 28.99997pt. \par
The minimum required \texttt{\string\headheight} for \texttt{customthree} is 43.49995pt. \par
The height difference between \texttt{customone} and \texttt{customtwo} is 14.49999pt. \par
The height difference between \texttt{customtwo} and \texttt{customthree} is 14.49998pt. \par
The heights divided by the number of lines are: 14.49998pt, 14.499985pt, 14.49998333pt.
%\texttt{\string\baselineskip}: \the\baselineskip% 14.5pt under 12pt base font size
\end{document}
因此,如果您使用一致的基线跳过设置内容,则可以使用的倍数\baselineskip
,并确保内容适合;基本字体大小为\baselineskip
。的偏差是由于四舍五入造成的。12pt
14.5pt
\headheight