# Why is an overlay tikzpicture introducing extraneous vertical spacing before a section?

Here is my MWE:

\documentclass[10pt]{article}

\usepackage{showframe}
\usepackage{geometry}
\usepackage{tikz}
\usepackage{titlesec}

\titlespacing*{\section}{0pt}{0pt}{0pt}

\begin{document}%
\definecolor{sidebar}{RGB}{220,62,136}%
% \begin{tikzpicture}[overlay,remember picture]%
%     \fill[sidebar] (current page.north west) rectangle ([xshift=2cm]current page.south west);%
% \end{tikzpicture}%
\section*{Title}%
\end{document}


This renders as expected:

However, if I uncomment the tikzpicture I get this:

Why does the section get pushed down and how can I prevent it?

• Good question, +1. As a workaround you could put the overlay picture after the section, obviously it would not change the position of the picture. I also feel that this question is very closely related, and I leave it to you and non-gold-badge cursed users to decide whether yours is a duplicate thereof. – user121799 Jun 14 '19 at 4:13
• Possible duplicate: tex.stackexchange.com/q/68720/14500 – Paul Gaborit Jun 14 '19 at 5:21
• A overlay tikzpicture is a box, a small box but nevertheless a box, so it can change spacing. – Ulrike Fischer Jun 14 '19 at 6:32
• @Ulrike Hence the second part of my question: how can I prevent it? – me-- Jun 14 '19 at 6:35
• As marmot wrote: put the tikzpicture in a place where such a box doesn't harm, e.g. in a paragraph with test and not somewhere where it builds a paragraph on its own. – Ulrike Fischer Jun 14 '19 at 6:38

As mentioned in comments and here, a tikzpicture creates a small box and calls a \leavevmode. The box itself does not take up any space with the overlay option active, but the mode change does. However, other content such as text induce the same mode change. As such, to get rid of the problem, place the picture where content is already to be added. The example below illustrates this.

Coloured nodes are added in each picture to illustrate the placement of the box and to show how it does not influence or interfere with the text.

\documentclass[10pt]{article}

\usepackage{showframe}
\usepackage{geometry}
\usepackage{tikz}
\usepackage{titlesec}

\titlespacing*{\section}{0pt}{0pt}{0pt}
\definecolor{sidebar}{RGB}{220,62,136}%

\begin{document}%
\section*{Title} % section without text, without tikz
\section*{Title} % section with text, without tikz
text
\section*{Title} % section without tikz, with text
\begin{tikzpicture}[overlay, remember picture]%
%\node[fill=green,inner sep=2cm] (n2) {};
\node[fill=red] {};
\fill[sidebar] (current page.north west) rectangle ([xshift=2cm]current page.south west);%
\end{tikzpicture}%
\section*{Title} % section with tikz, with text (= same)
\begin{tikzpicture}[overlay, remember picture]%
\node[fill=blue] {};
\fill[sidebar] (current page.north west) rectangle ([xshift=2cm]current page.south west);%
\end{tikzpicture}%
text
\section*{Title} % final section
\end{document}


• Note that in the current state of TikZ, when using baseline and overlay at the same time, it is possible that a tikzpicture adds to the height or depth of the enclosing box. – frougon Jun 14 '19 at 9:07