6

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:

enter image description here

However, if I uncomment the tikzpicture I get this:

enter image description here

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

me--
  • 377
  • 2
  • 8
  • 1
    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. –  Jun 14 '19 at 04:13
  • 3
    Possible duplicate: https://tex.stackexchange.com/q/68720/14500 – Paul Gaborit Jun 14 '19 at 05: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 06:32
  • @Ulrike Hence the second part of my question: how can I prevent it? – me-- Jun 14 '19 at 06: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 06:38
  • The tikzpicture is not unique in this behavior. A \marginpar has the same effect even though may expect it shouldn't. Try \marginpar{x}% \section*{Title}% to see what I mean. There it is arguably even worse since it does matter where you issue the \marginpar. –  Jun 14 '19 at 06:45
  • Yeah, I'm already using the suggested workaround (thanks!). I just thought maybe there was a more elegant means of controlling this. But this being LaTeX, I guess not :) – me-- Jun 14 '19 at 07:33
  • You can also use \rlap or \llap to avoid leaving vmode. – John Kormylo Jun 14 '19 at 15:32

1 Answers1

2

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}

induced leavevmode

JJM Driessen
  • 1,353
  • 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 09:07