1

I am creating a document with a header image created in TikZ. I have encountered an unusual behaviour that is frustrating my attempts to control the positioning.

If the header is left out (for instance by commenting out \header in the code below, the first section begins hard up against the top of the page (in this example). However, with the header image included, called with the macro \header, the first section begins lower. The amount it drops down is greater than the separation between the sections.

What is this spacing? Where does it come from? Is there a defined length controlling it?

Ideally I would like to compensate for it in the definition of \header in an associated class file and control precisely where the text will begin below the header.

\documentclass{article}

\usepackage[top=0mm, bottom=30mm, left=20mm, right=20mm, nohead, nofoot]{geometry}
\usepackage{tikz}

\newcommand{\header}{
\begin{tikzpicture}[remember picture,overlay]
\node [rectangle, fill=black!40, anchor=north, minimum width=\paperwidth, minimum height=4cm] (box) at (current page.north){};
\end{tikzpicture}}

\begin{document}

\header

\section{The First}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ipsum ipsum, porta eget rutrum et, scelerisque at tortor. Donec adipiscing est sed odio congue, quis pulvinar leo semper. Sed euismod ut augue a imperdiet. Ut et enim quam. Nam id hendrerit mi, sed pretium tortor. 
\section{The Second}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ipsum ipsum, porta eget rutrum et, scelerisque at tortor. Donec adipiscing est sed odio congue, quis pulvinar leo semper. Sed euismod ut augue a imperdiet. Ut et enim quam. Nam id hendrerit mi, sed pretium tortor. 

\end{document}

Many thanks

3
  • With \header your first \section command is not on vertical mode. – Gonzalo Medina Jul 31 '13 at 0:23
  • Thankyou. So what? Is that the problem? How do I fix it? – Jonathan Jul 31 '13 at 17:07
  • Yes, this is the problem. Please see my answer below for a possible solution. – Gonzalo Medina Jul 31 '13 at 23:40
1

The problem is that by using directly \header, the first \section command is not on vertical mode anymore and, in particular, the default spacing before a section (3.5ex plus 1ex minus .2ex); this effect can be observed by simply adding an explicit \leavevmode before \section:

\documentclass{article}
\usepackage[top=0mm, bottom=30mm, left=20mm, right=20mm, nohead, nofoot]{geometry}
\usepackage{tikz}
\usepackage{atbegshi}

\begin{document}

\leavevmode\section{The First}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ipsum ipsum, porta eget rutrum et, scelerisque at tortor. Donec adipiscing est sed odio congue, quis pulvinar leo semper. Sed euismod ut augue a imperdiet. Ut et enim quam. Nam id hendrerit mi, sed pretium tortor. 
\section{The Second}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ipsum ipsum, porta eget rutrum et, scelerisque at tortor. Donec adipiscing est sed odio congue, quis pulvinar leo semper. Sed euismod ut augue a imperdiet. Ut et enim quam. Nam id hendrerit mi, sed pretium tortor. 

\end{document}

One way o prevent this effect is by using atbegshi and adding the header material before the page is shipped out:

\documentclass{article}
\usepackage[top=0mm, bottom=30mm, left=20mm, right=20mm, nohead, nofoot]{geometry}
\usepackage{tikz}
\usepackage{atbegshi}

\newcommand{\header}{%
\begin{tikzpicture}[remember picture,overlay]
\node[
  rectangle,
  fill=black!40,
  anchor=north,
  minimum width=\paperwidth,
  minimum height=4cm
] (box) at (current page.north){};
\end{tikzpicture}}

\begin{document}
\AtBeginShipoutFirst{\header}

\section{The First}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ipsum ipsum, porta eget rutrum et, scelerisque at tortor. Donec adipiscing est sed odio congue, quis pulvinar leo semper. Sed euismod ut augue a imperdiet. Ut et enim quam. Nam id hendrerit mi, sed pretium tortor. 
\section{The Second}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ipsum ipsum, porta eget rutrum et, scelerisque at tortor. Donec adipiscing est sed odio congue, quis pulvinar leo semper. Sed euismod ut augue a imperdiet. Ut et enim quam. Nam id hendrerit mi, sed pretium tortor. 

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.