我正在尝试自动设置正确的\headheight
以下内容Heiko Oberdiek 的方法。这是基于这样的事实:fancyhdr
包通常会根据需要增加\headheight
并将该值保存到文件的最后。问题是,当使用该calc
包时,该值似乎没有改变(尽管日志文件仍然声称“我们现在将其设置为文档其余部分的大小。”)。
我知道我可以通过使用和朋友来避免该calc
包,但我需要需要该包的包。\dimexpr
mathtools
calc
有人知道如何获取正确的值,\headheight
即使calc
使用了包?
\documentclass{article}
\usepackage{blindtext}
\usepackage{calc} % this suppresses fancyhdr's feature of increasing \headheight as required
% ===== save header and footer height =====
% https://tex.stackexchange.com/a/117810/120953
\usepackage{atveryend}
\makeatletter
\AtVeryEndDocument{%
\if@filesw % respect \nofiles
\begingroup
% same write register as environment `filecontents` uses
\chardef\reserved@c=15 %
\immediate\openout\reserved@c=\jobname.heights\relax
\immediate\write\reserved@c{%
\string\setlength{\string\headheight}{\the\headheight}%
}%
\immediate\write\reserved@c{%
\string\setlength{\string\footskip}{\the\footskip}%
}%
\immediate\closeout\reserved@c
\endgroup
\showthe\headheight
\fi
}
\makeatother
%\InputIfFileExists{\jobname.heights}{}{} % I have commented this out for testing
\usepackage[
includehead,
includefoot,
showframe,
]{geometry}
% ===== header & footer =====
\usepackage{fancyhdr}
\usepackage{lastpage}
\usepackage{graphicx}
\pagestyle{fancy}
\fancyhead{}
\fancyhead[L]{a rather long header\\ which needs to be broken into several lines}
%\fancyhead[R]{\includegraphics[height=\dimexpr\the\headheight-4.3501pt\relax]{icon}}
\fancyfoot{}
\fancyfoot[R]{page \thepage \ of \pageref{LastPage}}
\fancyfoot[L]{some footer}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
% ===== test document =====
\begin{document}
\blinddocument
\end{document}
答案1
fancyhdr
在一个不支持 LaTeX 语法的地方使用\global\setlength
,如果加载,它根本不起作用calc
。在这种情况下很容易避免它,只需删除\setlength
并使用原始分配即可。
\documentclass{article}
\usepackage{blindtext}
\usepackage{calc} % this suppresses fancyhdr's feature of increasing \headheight as required
% ===== save header and footer height =====
% http://tex.stackexchange.com/a/117810/120953
\usepackage{atveryend}
\makeatletter
\AtVeryEndDocument{%
\if@filesw % respect \nofiles
\begingroup
% same write register as environment `filecontents` uses
\chardef\reserved@c=15 %
\immediate\openout\reserved@c=\jobname.heights\relax
\immediate\write\reserved@c{%
\string\setlength{\string\headheight}{\the\headheight}%
}%
\immediate\write\reserved@c{%
\string\setlength{\string\footskip}{\the\footskip}%
}%
\immediate\closeout\reserved@c
\endgroup
\showthe\headheight
\fi
}
\makeatother
%\InputIfFileExists{\jobname.heights}{}{} % I have commented this out for testing
\usepackage[
includehead,
includefoot,
showframe,
]{geometry}
% ===== header & footer =====
\usepackage{fancyhdr}
\makeatletter
\def\@fancyvbox#1#2{\setbox0\vbox{#2}\ifdim\ht0>#1\@fancywarning
{\string#1 is too small (\the#1): ^^J Make it at least \the\ht0.^^J
We now make it that large for the rest of the document.^^J
This may cause the page layout to be inconsistent, however\@gobble}%
\dimen0=#1\relax
\global#1\ht0\ht0=\dimen0\fi
\box0}
\makeatother
\usepackage{lastpage}
\usepackage{graphicx}
\pagestyle{fancy}
\fancyhead{}
\fancyhead[L]{a rather long header\\ which needs to be broken into several lines}
%\fancyhead[R]{\includegraphics[height=\dimexpr\the\headheight-4.3501pt\relax]{icon}}
\fancyfoot{}
\fancyfoot[R]{page \thepage \ of \pageref{LastPage}}
\fancyfoot[L]{some footer}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
% ===== test document =====
\begin{document}
\blinddocument
\end{document}