添加徽标会导致页面布局不一致

添加徽标会导致页面布局不一致

以下 MWE 显示了我的问题。如果我将其发送两次,我会得到两页。两页上的文本不是从页面顶部相同的垂直距离开始的。因此,看起来 9.(恰好是第 2 页的开头)比 1 位于页面的上方,如下图所示。

enter image description here

如果tikzpicture环境被注释掉那么它就可以起作用。

两个问题:

1:这是怎么回事?我以为这些选项[remember picture,overlay]不会影响页面的其余部分(但显然它们会)

2:如何最好地解决在页面上添加徽标的问题而不影响其他任何内容?

\documentclass[12pt]{article}
\usepackage[demo]{graphicx}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[remember picture,overlay]
\node[anchor=north west] at (current page.north west) {\includegraphics[height=3\baselineskip]{image.png}};
\end{tikzpicture}%

\begin{enumerate}
\item This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. 

\item This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. 

\item This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. 

\item This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. 

\item This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. 

\item This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. 

\item This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. 

\item This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. 

\item This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. 

\item This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. 

\end{enumerate}

\end{document}

答案1

  1. 这里发生了什么?

    该问题与页面的构建方式以及页面以列表开头的特定情况(在您的情况下为枚举)有关。

    TikZ 使用的技巧overlay是将其所有内容排版在宽度和高度为零的框中,以便该框的内容在 TeX 眼中不会“占用空间”,因此它“重叠”在周围环境之上。

    问题是,当页面开始时,TeX 处于“垂直模式”,即使你插入一个空框,这也会使 TeX 离开垂直模式,因为第一个框(即使是空的)被视为新段落的开头。你可以用命令替换 tikzpicture 来产生相同的效果\leavevmode

    如果之后的标记是“常规”段落的一部分,则不会产生明显的影响。只是,第一个段落以一个空框开头,这不会改变段落缩进或形状。但是,如果空框后的下一个标记是列表(在您的情况下是枚举),LaTeX 会插入一个垂直空格将其与上一个段落分开。通常,当垂直空格是页面中出现的第一个东西时,它会被丢弃,但您不是这种情况,因为您已经有一个由 TikZ 生成的先前的空框。

  2. 如何解决?

    我尝试\unskip在枚举之前或枚举内部添加一些内容,但没有结果。我不太明白这是什么\unskip意思,但显然一旦添加了空框,它就没有效果了,所以需要采用不同的方法。

    诀窍是避免将 tikz 环境作为页面中的第一项发出。请注意,由于您在其中使用绝对坐标,因此将代码放在哪里并不重要,只要它在当前页面发出之前执行即可。例如,您可以将它放在第一个之后\item

    atbegshi作为一种更通用的解决方案,您可以使用允许您在页面准备发送时向其添加代码的包。该命令\AtBeginShipoutFirst{}将代码添加到文档的第一页。如果您希望徽标出现在每一页上,请使用\AtBeginShipout{}。如果您只希望它出现在某些特定页面上,请\AtBeginShipoutNext{}在这些页面上使用。

使用这个想法,你的例子将是:

\documentclass[12pt]{article}
\usepackage[demo]{graphicx}
\usepackage{atbegshi}
\usepackage{tikz}

\AtBeginShipoutFirst{
\begin{tikzpicture}[remember picture,overlay]
\node[anchor=north west] at (current page.north west) {\includegraphics[height=3\baselineskip]{image.png}};
\end{tikzpicture}
}

\begin{document}

\begin{enumerate}
\item This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. 

\item This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. 

\item This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. 

\item This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. 

\item This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. 

\item This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. 

\item This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. 

\item This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. 

\item This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. 

\item This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. This is some dummy text. 

\end{enumerate}
\end{document}

导致:

Result

相关内容