节标题元素应占用零垂直空间

节标题元素应占用零垂直空间

我的目标是将图表分成两页相对的页面,如下所示:

分割图

左侧页面的图表上方还会有一个\section\subsection。为了使图表的两半正确对齐,左侧页面上的节或子节标题不占用任何垂直空间非常重要。否则,左半部分将被推下,与右半部分不匹配。

因此,我试图设计一种方法来强制节标题(或理想情况下任意内容)占用零空间。我在某处找到了以下解决方案,不幸的是我不记得在哪里:

\documentclass{article}
\usepackage{calc}
\newlength{\noverticalspacelen}

% Insert a command so that it occupies no vertical space, future text overlaps
\newcommand{\noverticalspace}[1]{%
\setbox0=\vbox{{#1}}%
\setlength\noverticalspacelen{\ht0}%
\box0
\vspace{-\noverticalspacelen}%
}


\begin{document}

% Should overlap XXXX on top of Header
\noverticalspace{\section{Header}}
\section{XXXXX}


% Should overlap XXXX on top of Content
\noverticalspace{Content}
\par XXXXX

\end{document}

上面的代码对我来说很有意义:我们测量相关内容,然后使用它\vspace来删除它占用的空间。如果此代码有效,我希望看到 XXXX 与标题和内容文本重叠。它几乎有效:

不重叠

我不确定这是为什么。我尝试了另一个版本,首先将各种间距度量设置为零:

\documentclass{article}
\usepackage{calc}
\newlength{\noverticalspacelen}

% Insert a command so that it occupies no vertical space, future text overlaps
\newcommand{\noverticalspace}[1]{%
\parskip=0pt
\setlength{\parskip}{0pt}
\setlength{\baselineskip}{0pt}
\parindent=0pt
\renewcommand\bigskip{}
\renewcommand\medskip{}
\renewcommand\smallskip{}
\renewcommand\bigbreak{}
\renewcommand\medbreak{}
\renewcommand\smallbreak{}

\setbox0=\vbox{{#1}}%
\setlength\noverticalspacelen{\ht0}%
\box0
\vspace{-\noverticalspacelen}%
}


\begin{document}

% Should overlap XXXX on top of Header
\noverticalspace{\section{Header}}
\section{XXXXX}


% Should overlap XXXX on top of Content
\noverticalspace{Content}
\par XXXXX

\end{document}

这个版本非常接近:

在此处输入图片描述

我无法解释为什么这些方法不起作用。对于我试图做的事情,是否有“正确”的方法?我尝试过一些方法,zref-savepos似乎有用,但实际上没有用。我也尝试过这个textpos库,但结果同样不成功。

任何帮助,将不胜感激。

答案1

以不影响周围内容的方式放置框可能非常棘手,仅将其尺寸设为零是不够的。特别是如果它后面跟着标题或其他显示结构,这些结构明确回顾前一个项目并调整它们添加的空间。例如,框后面的标题将比后面跟着标题(已经添加了空间)的标题添加更多空间。

另外,给标题加框也会干扰其对下一段落的控制。

所以我不会使用那种策略,而是简单地添加负空间来解释标题。您可以剖析标题命令的代码以查看需要多少空间,但更简单的方法是让 TeX 向您显示。

\documentclass{article}

\showoutput
\begin{document}

% Should overlap XXXX on top of Header
\section{Header}
\section{XXXXX}


% Should overlap XXXX on top of Content
Content
\par XXXXX

\section{Header followed by text not header}
ZZZ

\end{document}

生成日志显示

....\OT1/cmr/bx/n/14.4 r
....\penalty 10000
....\glue(\parfillskip) 0.0 plus 1.0fil
....\glue(\rightskip) 0.0
...\write1{\@writefile{toc}{\protect \contentsline {section}{\protect \numberli
ne \ETC.}
...\penalty 10000
...\glue 9.90276 plus 0.86108
...\glue(\parskip) 0.0 plus 1.0
...\glue(\parskip) 0.0
...\glue(\baselineskip) 8.12001
...\hbox(9.87999+0.0)x345.0, glue set 259.53334fil
....\hbox(9.27998+0.0)x24.29997
.....\glue 0.0
.....\OT1/cmr/bx/n/14.4 2
.....\glue 16.19998
....\OT1/cmr/bx/n/14.4 X

在第一个标题之后

....\OT1/cmr/m/n/10 t
....\penalty 10000
....\glue(\parfillskip) 0.0 plus 1.0fil
....\glue(\rightskip) 0.0
...\glue(\parskip) 0.0 plus 1.0
...\glue(\parskip) 0.0
...\glue(\baselineskip) 5.16669
...\hbox(6.83331+0.0)x345.0, glue set 292.49992fil
....\hbox(0.0+0.0)x15.0
....\OT1/cmr/m/n/10 X

内容之后

....\OT1/cmr/bx/n/14.4 r
....\penalty 10000
....\glue(\parfillskip) 0.0 plus 1.0fil
....\glue(\rightskip) 0.0
...\write1{\@writefile{toc}{\protect \contentsline {section}{\protect \numberli
ne \ETC.}
...\penalty 10000
...\glue 9.90276 plus 0.86108
...\glue(\parskip) 0.0 plus 1.0
...\glue(\parskip) 0.0
...\glue(\baselineskip) 2.3667
...\hbox(6.83331+0.0)x345.0, glue set 326.66666fil
....\OT1/cmr/m/n/10 Z

在最后一个节头之后。

所以

在此处输入图片描述

\documentclass{article}

\showoutput
\begin{document}

% Should overlap XXXX on top of Header
\section{Header}

\vspace*{-9.90276pt plus -0.86108pt}
\vspace{0pt plus -1pt}
\vspace{-8.12001pt}
\vspace{-9.87999pt}% height of next box as shown in log
\section{XXXXX}


% Should overlap XXXX on top of Content
Content

% in the next two could also compensate for parskip,
% but as that has natural length 0pt here don't bother

\vspace{-\baselineskip}% easier here as XXX isn't "clever" about adding space
XXXXX

\section{Header followed by text not header}

\vspace{-9.90276pt plus -0.86108pt}% glue after heading
\vspace{-\baselineskip}
ZZZ

\end{document}

相关内容