Page Layoutfancyhdr problem

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
Post Reply
delia
Posts: 5
Joined: Tue Apr 13, 2010 11:53 pm

fancyhdr problem

Post by delia »

Hi,
to compile my thesis, I had to move my settings for the fancy header into a separate file (command.tex). So I copied all the corresponding tex in this file like this:

Code: Select all

\newcommand{\makefancyheader}[1]{
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}}
\fancyhf{} % delete current header and footer
\fancyhead[LE,RO]{\bfseries\thepage}
\fancyhead[LO]{\bfseries\rightmark}
\fancyhead[RE]{\bfseries\leftmark}
\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0pt}
\addtolength{\headheight}{0.5pt} % space for the rule
\fancypagestyle{plain}{\fancyhead{} % get rid of headers on plain pages
\renewcommand{\headrulewidth}{0pt} % and the line
}
\setlength{\headheight}{15pt}
}
Then in my main file, I do

Code: Select all

\include{command} 
\makefancyheader
before including the chapters.
The compilation stops at the first chapter with this error:

Code: Select all

(D:/chapter01.tex
Chapter 1.
Runaway argument?
{
! Paragraph ended before \markboth was complete.
<to be read again> 
                   \par 
l.1 \chapter{Introduction}
                          
? h
I suspect you've forgotten a `}', causing me to apply this
control sequence to too much text. How can we recover?
My plan is to forget the whole thing and hope for the best.
What do I miss? i am not an expert user, so I appreciate any suggestion!

thank you

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

fancyhdr problem

Post by gmedina »

Hi,

we'll need some more information (and perhaps some actual code). I did a simple test with this sample document:

Code: Select all

\documentclass{book}
\usepackage{fancyhdr}

\newcommand{\makefancyheader}[1]{
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}}
\fancyhf{} % delete current header and footer
\fancyhead[LE,RO]{\bfseries\thepage}
\fancyhead[LO]{\bfseries\rightmark}
\fancyhead[RE]{\bfseries\leftmark}
\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0pt}
\addtolength{\headheight}{0.5pt} % space for the rule
\fancypagestyle{plain}{\fancyhead{} % get rid of headers on plain pages
\renewcommand{\headrulewidth}{0pt} % and the line
}
\setlength{\headheight}{15pt}
}

\begin{document}
\makefancyheader{Test chapter}
\include{e}
\end{document}
which uses the code snippet you posted and the following subsidiary file test.tex:

Code: Select all

\chapter{Test chapter}
Test
and the document compiled without problems. Take your document and go commenting out parts of it until your document compiles, this will give you a hint as to where the culprit is to be found.

Once the problematic part has been detected, perhaps you can correct the problem yourself, if not, please post a complete and minimal version of your document allowing us to reproduce the problem mentioned.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
delia
Posts: 5
Joined: Tue Apr 13, 2010 11:53 pm

fancyhdr problem

Post by delia »

Ok, I did the check and if I comment out the \markboth{#1}{} and \markright{\thesection\ #1} or I substitute #1 with {} that works.
Here it is my code, in a shortened form.
mainfile.tex:

Code: Select all

\documentclass[12pt,a4paper,twoside,openright]{report}
\usepackage[ngerman, english]{babel} 
\usepackage{fancyhdr}
\usepackage[a4paper, twoside,top=40mm, bottom = 35mm]{geometry}
\include{command}
\begin{document}
\include{abstract}
\makefancyheader
\include{chapter01}
\end{document}
the file abstract (where I do not want fancy header) looks like this:
abstract.tex

Code: Select all

\begin{abstract}
test
\end{abstract}
the file chapter01.tex is:

Code: Select all

\chapter{This is the title of the Chapter}
This is the text of the chapter
\section{this is the title of the section}
text first page
\newpage
text second page 
the command.tex is like this:

Code: Select all

\newcommand{\makefancyheader}[1]{
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection\ {#1}}}
\fancyhf{} % delete current header and footer
\fancyhead[LE,RO]{\bfseries\thepage}
\fancyhead[LO]{\bfseries\rightmark}
\fancyhead[RE]{\bfseries\leftmark}
\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0pt}
\addtolength{\headheight}{0.5pt} % space for the rule
\fancypagestyle{plain}{\fancyhead{} % get rid of headers on plain pages
\renewcommand{\headrulewidth}{0pt} % and the line
}
\setlength{\headheight}{15pt}
}
Like this the code compile but the output misses all the chapter 1 text.
If I add newlines around the \makefancyheader, like this:

Code: Select all

\begin{document}
\include{abstract}

\makefancyheader

\include{chapter01}
\end{document}
I get the error which I posted before:

Code: Select all

! Paragraph ended before \markboth was complete.
So I assume the error comes from reading correctly the arguments of the \makefancyheader definition?
Thanks again for the help
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

fancyhdr problem

Post by gmedina »

Hi,

you must declare an argument for the \makefancyheader command even if it is empty; so use \makefancyheader{} (with a matching pair of braces) instead of just \makefancyheader
1,1,2,3,5,8,13,21,34,55,89,144,233,...
delia
Posts: 5
Joined: Tue Apr 13, 2010 11:53 pm

fancyhdr problem

Post by delia »

Oh thank you so much! Now it runs, but I still have a problem: the title of the chapter does not appear in the pdf , while if I insert the text of command.tex directly in the mainfile.tex it appears on even pages on the right as requested. What is the trick?
The code which produces the correct output is:

Code: Select all

\documentclass[12pt,a4paper,twoside,openright]{report}
\usepackage[ngerman, english]{babel} 
\usepackage{fancyhdr}
\usepackage[a4paper, twoside,top=40mm, bottom = 35mm]{geometry}
\include{command}

\begin{document}
\include{abstract}

%\makefancyheader{}
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection\ {#1}}}
\fancyhf{} % delete current header and footer
\fancyhead[LE,RO]{\bfseries\thepage}
\fancyhead[LO]{\bfseries\rightmark}
\fancyhead[RE]{\bfseries\leftmark}
\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0pt}
\addtolength{\headheight}{0.5pt} % space for the rule
\fancypagestyle{plain}{\fancyhead{} % get rid of headers on plain pages
\renewcommand{\headrulewidth}{0pt} % and the line
}
\setlength{\headheight}{15pt}
\include{chapter01}
\end{document}
Thank you again for all the help!
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

fancyhdr problem

Post by gmedina »

Hi,

the problem comes from the way in which you are defining the \makefancyheader command:

Code: Select all

\newcommand{\makefancyheader}[1]{
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection\ {#1}}}
\fancyhf{} % delete current header and footer
\fancyhead[LE,RO]{\bfseries\thepage}
\fancyhead[LO]{\bfseries\rightmark}
\fancyhead[RE]{\bfseries\leftmark}
\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0pt}
\addtolength{\headheight}{0.5pt} % space for the rule
\fancypagestyle{plain}{\fancyhead{} % get rid of headers on plain pages
\renewcommand{\headrulewidth}{0pt} % and the line
}
\setlength{\headheight}{15pt}
}
Note that you define the command with one argument and then you are using this argument for both \markboth and \markright, so invoking simply \makefancyheader{} will cause both marks to be empty. Perhaps what you are looking for is something like this:

Code: Select all

\newcommand{\makefancyheader}{%
  \pagestyle{fancy}
  \renewcommand{\chaptermark}[1]{\markboth{##1}{}}
  \renewcommand{\sectionmark}[1]{\markright{\thesection\ {##1}}}
  \fancyhf{} % delete current header and footer
  \fancyhead[LE,RO]{\bfseries\thepage}
  \fancyhead[LO]{\bfseries\rightmark}
  \fancyhead[RE]{\bfseries\leftmark}
  \renewcommand{\headrulewidth}{0.5pt}
  \renewcommand{\footrulewidth}{0pt}
  \addtolength{\headheight}{0.5pt} % space for the rule
  \fancypagestyle{plain}{\fancyhead{} % get rid of headers on plain pages
    \renewcommand{\headrulewidth}{0pt} % and the line
  }
  \setlength{\headheight}{15pt}
}
the command has no arguments now, so you can simply use

Code: Select all

\makefancyheader 
(with no braces) in your document and the marks will now be picked up automatically.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
delia
Posts: 5
Joined: Tue Apr 13, 2010 11:53 pm

Re: fancyhdr problem

Post by delia »

Awesome! I am deeply thankful.
Just for my knowledge, what is the difference in # and ##?
thank you again!
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

fancyhdr problem

Post by gmedina »

The number of "#" symbols can be used to nest new commands inside each other. Let's suppose that you have defined \commandone with one mandatory argument and now you want to define \commandtwo with one mandatory argument (the number of arguments is not relevant in this example); assume further that inside the definition for \commandtwo you will use the command \commandone previously defined. In the definition of the \commandtwo command, #1 will stand for the mandatory argument of \commandtwo, so how can you refer to the mandatory argument of \commandone? Obviously you cannot use #1 (since, as I already mentioned, #1 represents the mandatory argument of \commandtwo); the TeX solution is simply to double the "#" symbol; with this convention, when you are defining \commandtwo, #1 will represent its own mandatory argument, whereas ##1 will represent the mandatory argument for \commandone.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
delia
Posts: 5
Joined: Tue Apr 13, 2010 11:53 pm

Re: fancyhdr problem

Post by delia »

Fantastic, thank you!
Post Reply