[Tex/LaTex] Tikzpicture with overlay takes up space

spacingtikz-pgf

Consider this MWE:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[remember picture,overlay]
    \node [circle, fill=red!90!black, minimum width=15em] at (current page.center) {};
\end{tikzpicture}

\section{Amazing movable type!}
\end{document}

The section title is moved even though the picture with overlay should take up no space. Try inputting just a section title without tikzpicture, and notice that there is less vertical space above section title:

\documentclass{article}

\begin{document}
\section{Amazing movable type!}
\end{document}

or see illustration:

Tikzpicture with overlay takes up space

The behavior is the same with comments which should eat up spaces:

\documentclass{article}
\usepackage{tikz}

\begin{document}%
%
\begin{tikzpicture}[remember picture,overlay]%
    \node [circle, fill=red!90!black, minimum width=15em] at (current page.center) {};%
\end{tikzpicture}%
%
\section{Amazing movable type!}
\end{document}

I noticed that extra vertical space is added when:

  • tikzpicture is inputted before section titles
  • tikzpicture is inputted before description environment
  • tikzpicture is inputted before itemize environment
  • tikzpicture is inputted before enumerate environment

Tikzpicture behaves as expected with normal text. For example, this will work fine:

\documentclass{article}
\usepackage{lipsum, tikz}

\begin{document}
\section{Amazing movable type!}
\begin{tikzpicture}[remember picture,overlay]
    \node [circle, fill=red!90!black, minimum width=15em] at (current page.center) {};
\end{tikzpicture}%but you must not leave some space here, otherwise, a single blank space is shown before text.
\lipsum[13]

\end{document}

Why is that happening and how can it be prevented? The \useasboundingbox (0,0); suggestion from Make tikz picture floating does not help.

edit

Caramdir's suggestion to input\nointerlineskip before tikzpicture seems to fix vertical space issue for section titles, but descriptions and similar environments still have undesired extra vertical space. See illustration:

nointerlineskip kludge

Best Answer

This tikzpicture environment must be embedded in a paragraph, for example at start or end

\section{Not amazing}

\begin{tikzpicture}[...]...\end{tikzpicture}%
Text of the paragraph

If it's put between paragraphs, it will interact badly with all spacing features. Vertical space won't disappear at page breaks, baselineskip space will be inserted, \addvspace won't know about a space before that environment.

It should be TikZ's job to recognize that such an overlay environment mustn't do anything to the current page and reserve it for the shipout. Even with atbegshi there are problems:

\AtBeginShipout{\begin{tikzpicture}[...]...\end{tikzpicture}}

causes the output to be shifted down (with an overfull vbox warning) again because the TikZ picture produces an hbox.