# Why does source code in my header of report appear?

In my header is source code where nothing is supposed to be. Main document:

\documentclass[a4paper,12pt]{report}
\usepackage{lipsum}
\usepackage{fancyhdr}
\usepackage{layout}
\usepackage{titlesec}
\titlespacing{\chapter}{0pt}{-50pt}{40pt}
\setlength{\parindent}{0pt}
\begin{document}
\chapter{FirstChapter}
\lipsum[2] %Everything fine here.
\restoregeometry
\fancyhf{}
\chapter*{LastChapter}
\thispagestyle{fancy}
\lipsum[1]
\end{document}


And when I include LastChapter (and several others, which work fine) only the LastChapter makes problems with the header (btw: the footer is empty, that's OK). The last line of the PreLastChapter is \clearpage. Does that matter?

Has this something ot to with the fact that I changed the geometry several times before?

Note: I would like to not throw something out of this source code, it works fine except that one little error. (I updated all of my packages yesterday, I use TeXnicCenter and MiKTeX).

• If I run your example I get an error ! Package xkeyval Error: inputenc' undefined in families blx@opt@pre'. If that occurs in your real document you need to fix that before looking at the output, if not please fix the example to be error free and demonstrate the problem with a header. – David Carlisle Jan 16 '16 at 20:01
• It appears for more than several compilations already but it didn't make some problems so far so I ignored it unless I don't know where it comes from neither to fix it in which file (document/line). So it's about this error? I also ignored it because my TeXnicCenter tells me \begin{document} is an illegal parameter / an undefined controol sequence. – Nepumuk Jan 16 '16 at 20:04
• if you get a tex error it really isn't worth even looking at the resulting pdf. When recovering from errors tex tries to get into a state that it can carry on to check more of the document but it does not try to make sensible typeset output. If I scroll past the error then as its a one page document, I just get a chapter head and no page head, what did you expect? – David Carlisle Jan 16 '16 at 20:06
• unless you make an example that demonstrates the problem, no-one will be able to help. You have neither shown your bad output nor produced code that puts "source code in the header" (what do you mean by that?) – David Carlisle Jan 16 '16 at 20:14
• I'm already searching in TheChapterBefore.tex, but I can't find it -.- – Nepumuk Jan 16 '16 at 20:48

For me it seems here are two missunderstandings. The first one is a missing knowledge about the resulting typing area, the second one is a missunderstanding of command \titlespacing.

Let us start with the typing area. I added option showframe to package geometry to show the resulting typing area, the area for the header, footer and margins.

The command \titlespacing is defined as (see documentation of package titlesec, chapter 3.2 Spacing):

%\titlespacing*{command}{left}{before-sep}{after-sep}[right-sep]
\titlespacing{\chapter}{0pt}{-20pt}{40pt} % <=================== my suggestion
\titlespacing{\chapter}{0pt}{0pt}{40pt} %   <=================== no negative distance


Your choice (-50pt) results in the fact that your chapter heading is moved upside into the reserved space for the header. The reason, that you see this effect not on the first page is that there is no rule in the header. On the second page the chapter header is written above the header rule.

With my choice (-20pt) has only the effect that your chapter heading is moved a little bit up, but is not moved into the header (and the header rule for page 2).

With no negative distance (0pt) I think one get the best result, but that is off cource my personal opinion ...

Please have now a look to my MWE

\documentclass[a4paper,12pt]{report}

\usepackage{lipsum}
\usepackage{fancyhdr}
\usepackage[%
showframe        % <===================== to visualize the typing area
]{geometry}

\usepackage{titlesec}
%\titleformat{command}[shape]{format}{label}{sep}{before-code}[after-code]
%\titlespacing*{command}{left}{before-sep}{after-sep}[right-sep]
\titlespacing{\chapter}{0pt}{-20pt}{40pt} % <====================== -50pt
\setlength{\parindent}{0pt}

\begin{document}

\chapter{FirstChapter}
\lipsum[1-8] %Everything fine here.
\restoregeometry
\fancyhf{}

\chapter*{LastChapter}
\thispagestyle{fancy}
\lipsum[1]

\end{document}


with the result:

and the third page:

That should be the result you want, I think.

With you choice of -50pt you get (I show only the top part of the pages) for page 1:

and for the third page (see the marked bottom of the header line with the red arrow):

Hope this helps you to see your missunderstandings ...

Change the line \titlespacing{\chapter}{0pt}{-50pt}{40pt} as \titlespacing{\chapter}{0pt}{-30pt}{40pt} and run the code you will get the desired result.