# Tikzpicture with overlay takes up space

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:

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:

-
I don't see anything in your illustration (the two lines look the same to me), but I guess what you mean is that additional vertical white space is inserted. This probably happens for the same reason that you have the comment in your last example. The tikzpicture does seem to always switch to horizontal mode so that the blank lines before and after are interpreted as paragraph breaks. Commenting them out should help. – Caramdir Jul 12 '11 at 15:57
As far as I know an TikZ overlay still adds itself as whatsit object at its original location. I had similar issues with zrefs \zlabels and Heiko Oberdiek told me that such things can't be placed into the document without disturbing the spacing. At least that's the way I understood it. However, he uses the LaTeX space hack around it, which doesn't help that much in vertical mode. – Martin Scharrer Jul 13 '11 at 7:31
No, the space hack (\@bsphack .. \@esphack) doesn't work here. It actually raises an error. The same space appears when you replace the tikzpicture with a \marginpar. Also placing tikzpicture inside a \vbox to 0pt{.. \vss} doesn't help. This seems to be a tricky issue which requires someone with deep knowledge of TeX. – Martin Scharrer Jul 13 '11 at 7:43
@Caramdir: I edited the illustration to make it more readable. Yes, vertical space is inserted. Commenting does not help -- I've also edited my example code -- vertical space is still inserted. @Martin: The thing that puzzles me is why can the tikzpicture be placed before text without disturbing the spacing, but cannot be placed in front of sections and environments. – ipavlic Jul 13 '11 at 8:51
Adding \nointerlineskip before the tikzpicture solves the problem, except at the beginning of the document (where an additional paragraph seems to be inserted somewhere). – Caramdir Jul 13 '11 at 21:53

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.

-
Thanks for clarifying the problem. Is there any possibility to somehow allow these overlay tikzpicture between paragraphs as well? In such cases (but not all cases) the position of overlay pictures is not relevant because they draw on the page directly. So could this picture code instead be saved and reinserted at the end of the page using some at-shipout or similar hook? – Martin Scharrer Jul 14 '11 at 10:47

You can put the overlaying tikzpicture environments in a box of width zero:

\makebox[0pt][c]{%
\begin{tikzpicture}[overlay]
...
\end{tikzpicture}%
}


I have also found that "accidental" empty lines, created by code-structuring line breaks, can cause unwanted space added. Make sure to comment out all line endings in sensitive code areas.

-