2

I an currently formatting a page with the tikz package. I created a command to insert a vertical line and text above it. Sadly the formatting seems to be different every time it is called (there are spaces where no spaces are declared).

As you can see here: enter image description here

What's going on there?

My code:

% !TeX program = lualatex
\documentclass[a4paper,12pt]{article}

%font encoding & formatting
\usepackage[onehalfspacing]{setspace}
\usepackage{fontspec}
\setmainfont{Arial}
\usepackage[document]{ragged2e}

%language
\usepackage[ngerman]{babel}
\usepackage{lipsum}

%Styles
\usepackage{tikz}
\usepackage{tikzpagenodes}
\usepackage{atbegshi}
\usepackage{fancyhdr}
\usepackage{xcolor}

\definecolor{bluey}{RGB}{12,3,150}



\newcommand{\sep}[1]{
    \vspace{0.5cm}
    {\color{blue} \large \textbf{#1}}\\
    \begin{tikzpicture}
        \draw [blue, fill=blue] (-2,0.5) circle [radius=0.17];
        \draw [blue, line width = 1.5pt] (-1.75,0.5) -- (2.5,0.5);
        \draw [lightgray, line width = 1.5pt] (2.5,0.5) -- (11,0.5);
    \end{tikzpicture}

}

%header format
\newcommand\HeaderTwo{%
    \begin{tikzpicture}[remember picture,overlay]
    \fill[bluey]
    (current page.north west) -- (current page.north east) --
    ([yshift=2cm]current page.north east|-current page text area.north east) --
    ([yshift=3cm]current page.north west|-current page text area.north west) -- cycle;
    \end{tikzpicture}%
}

\begin{document}

\HeaderTwo
\sep{My very very long foo Text}
\vspace{0.5cm}
\lipsum[1]

\sep{Another very very long foo text}
\vspace{0.5cm}
\lipsum[2]

\clearpage
\HeaderTwo


\sep{Another very very long foo text}
\vspace{0.5cm}
\lipsum[3]

\end{document}
  • 2
    You are using \HeaderTwo at the beginning of the page. This makes the \vspace visible as latex thinks you have started the page already. You should probably use it as header and won't have any issues anymore. – nox Jul 20 '18 at 11:31
  • @nox I'm afraid I can't follow your explanation. How do I define it as a header? You cannot see it on my picture, but on page two I did the same and that was formatted properly. Also the linewidth is not always the same, it seems to be smaller some times (more or less randomly) – Rüdiger Jul 20 '18 at 11:58
  • 1
    Fastest fix: put two empty lines between your Headertwo definition and your sep. The line width is unaffected, that is probably an aliasing problem with your screen. Zoom in (or print) to see constant line width. – Huang_d Jul 20 '18 at 12:12
  • 1
    Note, this is not a fix, but just shifts everything down, I'll post an answer in a sec. – nox Jul 20 '18 at 12:23
  • See my edited answer, I fixed the first page position offset by adjusting \headheight for a 12pt doc and added a \par I forgot in \HeaderTwo. – nox Jul 21 '18 at 12:44
3

A very handy package to debug this, if you have no idea what's going on, is showframe. Using this with your code, you will see that your code just shifts down the first section title of the second page, rather than fixing the issue. The problem is that \vspace{0.5cm} of your \sep command. It will produce vertical space of the specified amount, but only if it's not at the beginning of a page. This is probably the desired behavior for titles and headings of sections etc., because that way the heading is positioned at the top of the page rather than shifted down. However, you put something at the beginning of the page (your tikz picture of \HeaderTwo). Although it is not intended to be put on the body of the page, latex doesn't know that. So latex sees this picture and afterwards, \vspace produces its space.

Here is how the second page of your code actually looks like:

yourresult

To avoid that, I suggested to use a header instead (that's why we have it after all). In the following I made use of the fancyhdr package which you already had in your preamble. The header is not part of the body of the page, so latex properly recognizes when \vspace should produce it's space.

I added a \par in your \sep macro, that way you don't have to prepend an empty line. Also, I loaded the showframe package and deleted the unnecessary \vspaces you should avoid in your main document.

Finally I commented out the Arial font, as I don't have it installed on my system. My result with

% !TeX program = lualatex
\documentclass[a4paper,12pt]{article}

%font encoding & formatting
\usepackage[onehalfspacing]{setspace}
\usepackage{fontspec}
%\setmainfont{Arial}
\usepackage[document]{ragged2e}

%language
\usepackage[ngerman]{babel}
\usepackage{lipsum}

%Styles
\usepackage{tikz}
\usepackage{tikzpagenodes}
\usepackage{atbegshi}
\usepackage{fancyhdr}
\usepackage{xcolor}

\definecolor{bluey}{RGB}{12,3,150}

\newcommand{\sep}[1]{
    \par\vspace{1cm}%
    {\color{blue} \large \textbf{#1}}\\%
    \begin{tikzpicture}
    \draw [blue, fill=blue] (-2,0.5) circle [radius=0.17];
    \draw [blue, line width = 1.5pt] (-1.75,0.5) -- (2.5,0.5);
    \draw [lightgray, line width = 1.5pt] (2.5,0.5) -- (11,0.5);
    \end{tikzpicture}%
}

%header format
\lhead{\HeaderTwo} % defines your header as header in pagestyle fancy
\pagestyle{fancy} % loads it
\setlength{\headheight}{14.5pt} % corrects header height for 12pt document

\newcommand\HeaderTwo{%
    \begin{tikzpicture}[remember picture,overlay]%
    \fill[bluey]%
    (current page.north west) -- (current page.north east) --%
    ([yshift=2cm]current page.north east|-current page text area.north east) --%
    ([yshift=3cm]current page.north west|-current page text area.north west) -- cycle;%
    \end{tikzpicture}%
    \par%
}

\usepackage{showframe}


\begin{document}
\sep{My very very long foo Text}
\lipsum[1]

\sep{Another very very long foo text}
\lipsum[2]
\clearpage

\sep{Another very very long foo text}
\lipsum[3]
\end{document}

looks like the following:

result1 result2

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.