# How to avoid empty space caused by TikZ

When adding a background image by TikZ on a custom page, it leaves an empty space where the TikZ code is incorporated into the document. For example,

\documentclass{article}
\usepackage{lipsum}
\usepackage{tikz}
\usetikzlibrary{calc}
\title{Test Title}
\author{A name}
\begin{document}
\maketitle
\tikz[overlay,remember picture]{%
\node [rotate=45] at ($(current page.north west)+(7,-7)$) {\Huge\textcolor{gray}{TikZ}};
}
\section{First}
\lipsum[1]
\section{Second}
\subsection{This is not nice}
\lipsum[1]
\subsection{This is fine}
\lipsum[1]
\end{document}


As you can see in the below examples, the TikZ code leaves a footprint on the document (an empty space after \maketitle before the first section). This is not where the TikZ image is drawn (which is in the margin), but this is where the TikZ code is incorporated into the TeX document.

How do we treat the TikZ code to avoid its footprint on the actual document?

Figure 1: Without TikZ code (no extra space before the first section)

Figure 2: With TikZ code (an extra space added before the first section)

-
@percusse I meant after \maketitle. Compare the space before the first ssection in both cases (with and without TikZ). – All Aug 25 '12 at 11:33
Facepalm! Now I see it, – percusse Aug 25 '12 at 11:53

A tikzpicture (even with overlay) implies a call to \leavevmode (if you use \leavevmode instead of your tikzpicture into your example, you get the same footprint).

To add a tikzpicture with overlay without footprint, put it where you can quit vertical mode.

Example (at beginning of a paragraph):

\documentclass{article}
\usepackage{lipsum}
\usepackage{tikz}
\usetikzlibrary{calc}
\title{Test Title}
\author{A name}
\begin{document}
\maketitle
\section{First}
\tikz[remember picture,overlay]%
\node [rotate=45] at ($(current page.north west)+(7,-7)$)%
{\Huge\textcolor{gray}{TikZ}};%
\lipsum[1]
\section{Second}
\subsection{This is not nice}
\lipsum[1]
\subsection{This is fine}
\lipsum[1]
\end{document}

-

You couls use the atbegshi package and \AtBeginShipout or related commands auch as \AtBeginShipoutNext. Here, for example, let's take \AtBeginShipoutFirst to get it already on the first page:

\documentclass{article}
\usepackage{lipsum}
\usepackage{tikz}
\usepackage{atbegshi}
\usetikzlibrary{calc}
\title{Test Title}
\begin{document}
\AtBeginShipoutFirst{\tikz[overlay,remember picture]{
\node at ($(current page.west)+(2,0)$) {\Huge\textcolor{gray}{TEST}};
}}
\maketitle
\section{First}
\lipsum[1]
\section{Second}
\subsection{This is not nice}
\lipsum[1]
\subsection{This is fine}
\lipsum[1]
\end{document}

-
I already use this package for watermark with \AddToShipoutPicture, but I am curious to understand the reason for this empty space caused by TikZ. Then, it would be easy to use TikZ on any custom page. That was an example for the first page. – All Aug 25 '12 at 11:51

I can't find the exact question but as a rule of thumb, we might avoid putting interesting items as the first things that LaTeX sees starting paragraphs, tabular cells etc. Often it doesn't like it.

For example, the following doesn't have any effect.

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

\title{Test Title}
\author{authorize}
\begin{document}
\maketitle
\section{First}
My precious sentence which doesn't finish with a par.
\tikz[overlay,remember picture]{
\path[use as bounding box](0,0);
\node [gray,inner sep=0,rotate=45] at ([shift={(7cm,-7cm)}]current page.north west) {\Huge TikZ};
}\lipsum[1]

\section{Second}
\subsection{This is not nice}
\lipsum[1]
\subsection{This is fine}
\lipsum[1]
\end{document}


-
I checked and the effect is stronger before the first section, but it also has an effect before other sections too. Put the TikZ right before \section{Second}. It will still has an effect. – All Aug 25 '12 at 12:10
@Ali I've deleted an extra \hspace command now I don't see any space in my version. – percusse Aug 25 '12 at 12:11