Page Layout ⇒ Fancyhdr Questions
Fancyhdr Questions
I am using the fancyhdr package to set my headers and footers in an article I am writing and I am stuck regarding the following two issues:
i) I want the left header of each page to be the running title of the document (not the section). I know this can easily be done with something like
\newcommand{\runningtitle}{Title}
\lhead{\runningtitle}
but thought that there should be a way of feeding it directly to fancyhdr without doing that - tried \lhead{\title} but nothing happened. So, is there a way to directly pass the title of the article to fancyhdr without writing any other code - could not find anything in the documentation?
ii) On some pages, there is text from two sections - is there a way to include both their nameas in the header - soemthing like Introduction and Motivation?
Thanks,
George
Learn LaTeX easily with newest books:
The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis
The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more
LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis
Fancyhdr Questions
for the first issue, you can use the \@title command (obviously in the preamble and inside \makeatletter...\makeatother), as the following example suggests:
Code: Select all
\documentclass{book}
\usepackage{fancyhdr}
\usepackage{lipsum}% jus to generate some text
\pagestyle{fancy}
\makeatletter
\fancyhf{}
\lhead{\@title}
\rhead{\@author}
\makeatother
\title{The title of this book}
\author{Some author}
\begin{document}
\lipsum[1-15]
\end{document}
Fancyhdr Questions
I think this won't work because \maketitle clears out \@title. An easy solution is to define user commands for the title etc.:gmedina wrote:for the first issue, you can use the \@title command
Code: Select all
\newcommand*\doctitle{abc}
\newcommand*\docauthor{def}
\title{\doctitle}
\author{\docauthor}
\lhead{\doctitle}
\rhead{\docauthor}
\hypersetup{pdftitle=\doctitle, pdfauthor=\docauthor}
Fancyhdr Questions
You are absolutely right, it will not work if it preceeds \maketitle and it will print out the title as plain text if I repeat the \title command after \maketitle. What do I need to change so that \maketitle does not clear the \@title macro (just thought I will give it a try as I am trying to learn a few things beyond the basics)?phi wrote:I think this won't work because \maketitle clears out \@title. An easy solution is to define user commands for the title etc.:gmedina wrote:for the first issue, you can use the \@title command
Thanks,
George
Re: Fancyhdr Questions
Thanks,
George
Fancyhdr Questions
You have to redefine \maketitle. The actual definition depends on your document class. For the article class, the definition is as follows:gkl wrote:What do I need to change so that \maketitle does not clear the \@title macro (just thought I will give it a try as I am trying to learn a few things beyond the basics)?
Code: Select all
\if@titlepage
\newcommand\maketitle{\begin{titlepage}%
\let\footnotesize\small
\let\footnoterule\relax
\let \footnote \thanks
\null\vfil
\vskip 60\p@
\begin{center}%
{\LARGE \@title \par}%
\vskip 3em%
{\large
\lineskip .75em%
\begin{tabular}[t]{c}%
\@author
\end{tabular}\par}%
\vskip 1.5em%
{\large \@date \par}% % Set date in \large size.
\end{center}\par
\@thanks
\vfil\null
\end{titlepage}%
\setcounter{footnote}{0}%
\global\let\thanks\relax
\global\let\maketitle\relax
\global\let\@thanks\@empty
\global\let\@author\@empty
\global\let\@date\@empty
\global\let\@title\@empty
\global\let\title\relax
\global\let\author\relax
\global\let\date\relax
\global\let\and\relax
}
\else
\newcommand\maketitle{\par
\begingroup
\renewcommand\thefootnote{\@fnsymbol\c@footnote}%
\def\@makefnmark{\rlap{\@textsuperscript{\normalfont\@thefnmark}}}%
\long\def\@makefntext##1{\parindent 1em\noindent
\hb@xt@1.8em{%
\hss\@textsuperscript{\normalfont\@thefnmark}}##1}%
\if@twocolumn
\ifnum \col@number=\@ne
\@maketitle
\else
\twocolumn[\@maketitle]%
\fi
\else
\newpage
\global\@topnum\z@ % Prevents figures from going at top of page.
\@maketitle
\fi
\thispagestyle{plain}\@thanks
\endgroup
\setcounter{footnote}{0}%
\global\let\thanks\relax
\global\let\maketitle\relax
\global\let\@maketitle\relax
\global\let\@thanks\@empty
\global\let\@author\@empty
\global\let\@date\@empty
\global\let\@title\@empty
\global\let\title\relax
\global\let\author\relax
\global\let\date\relax
\global\let\and\relax
}
\def\@maketitle{%
\newpage
\null
\vskip 2em%
\begin{center}%
\let \footnote \thanks
{\LARGE \@title \par}%
\vskip 1.5em%
{\large
\lineskip .5em%
\begin{tabular}[t]{c}%
\@author
\end{tabular}\par}%
\vskip 1em%
{\large \@date}%
\end{center}%
\par
\vskip 1.5em}
\fi
Code: Select all
\if@titlepage
\renewcommand\maketitle{\begin{titlepage}%
\let\footnotesize\small
\let\footnoterule\relax
\let \footnote \thanks
\null\vfil
\vskip 60\p@
\begin{center}%
{\LARGE \@title \par}%
\vskip 3em%
{\large
\lineskip .75em%
\begin{tabular}[t]{c}%
\@author
\end{tabular}\par}%
\vskip 1.5em%
{\large \@date \par}% % Set date in \large size.
\end{center}\par
\@thanks
\vfil\null
\end{titlepage}%
\setcounter{footnote}{0}%
\global\let\thanks\relax
\global\let\maketitle\relax
\global\let\title\relax
\global\let\author\relax
\global\let\date\relax
\global\let\and\relax
}
\else
\renewcommand\maketitle{\par
\begingroup
\renewcommand\thefootnote{\@fnsymbol\c@footnote}%
\def\@makefnmark{\rlap{\@textsuperscript{\normalfont\@thefnmark}}}%
\long\def\@makefntext##1{\parindent 1em\noindent
\hb@xt@1.8em{%
\hss\@textsuperscript{\normalfont\@thefnmark}}##1}%
\if@twocolumn
\ifnum \col@number=\@ne
\@maketitle
\else
\twocolumn[\@maketitle]%
\fi
\else
\newpage
\global\@topnum\z@ % Prevents figures from going at top of page.
\@maketitle
\fi
\thispagestyle{plain}\@thanks
\endgroup
\setcounter{footnote}{0}%
\global\let\thanks\relax
\global\let\maketitle\relax
\global\let\@maketitle\relax
\global\let\title\relax
\global\let\author\relax
\global\let\date\relax
\global\let\and\relax
}
\fi
- localghost
- Site Moderator
- Posts: 9202
- Joined: Fri Feb 02, 2007 12:06 pm
Fancyhdr Questions
Code: Select all
\documentclass[11pt,a4paper,english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}
\usepackage{babel}
\usepackage{lmodern}
\usepackage{fancyhdr}
\usepackage{blindtext}
\parindent0em
\parskip\medskipamount
\title{Title of the Document}
\author{Author name}
\makeatletter
\let\Title\@title
\let\Author\@author
\makeatother
\fancyhf{}
\cfoot{\thepage}
\lhead{\Title}
\rhead{\Author}
\pagestyle{fancy}
\begin{document}
\maketitle
\tableofcontents
\blinddocument
\end{document}
For the second issue I'm not aware of an automatism which could do this.
Best regards
Thorsten¹
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
Fancyhdr Questions
You don't have to create a new document class, simply put the changed definition in your document if you want to use them.gkl wrote:Ok, made the change in article.cls and is working now. The one thing I do not know is how do I update the changes so that I can keep the document class to article - at the moment I had to rename it to myclass.cls and use that name in the \documentclass directive?