Page LayoutFancyhdr Questions

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
Post Reply
gkl
Posts: 28
Joined: Sat Jan 17, 2009 12:21 am

Fancyhdr Questions

Post by gkl »

Dear All,

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

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Fancyhdr Questions

Post by gmedina »

Hi,

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}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

Fancyhdr Questions

Post by phi »

gmedina wrote:for the first issue, you can use the \@title command
I think this won't work because \maketitle clears out \@title. An easy solution is to define user commands for the title etc.:

Code: Select all

\newcommand*\doctitle{abc}
\newcommand*\docauthor{def}
\title{\doctitle}
\author{\docauthor}
\lhead{\doctitle}
\rhead{\docauthor}
\hypersetup{pdftitle=\doctitle, pdfauthor=\docauthor}
These commands you can use everywhere you want.
gkl
Posts: 28
Joined: Sat Jan 17, 2009 12:21 am

Fancyhdr Questions

Post by gkl »

phi wrote:
gmedina wrote:for the first issue, you can use the \@title command
I think this won't work because \maketitle clears out \@title. An easy solution is to define user commands for the title etc.:
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)?

Thanks,
George
gkl
Posts: 28
Joined: Sat Jan 17, 2009 12:21 am

Re: Fancyhdr Questions

Post by gkl »

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?

Thanks,
George
phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

Fancyhdr Questions

Post by phi »

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)?
You have to redefine \maketitle. The actual definition depends on your document class. For the article class, the definition is as follows:

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
You can redefine this command so that \@title etc. don't get deleted:

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
User avatar
localghost
Site Moderator
Posts: 9201
Joined: Fri Feb 02, 2007 12:06 pm

Fancyhdr Questions

Post by localghost »

Saving the contents of \@title and \@author into other commands will do the trick.

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}
This solution does not depend on the used (standard) document class.

For the second issue I'm not aware of an automatism which could do this.


Best regards
Thorsten¹
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes[/size]

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1
phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

Fancyhdr Questions

Post by phi »

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?
You don't have to create a new document class, simply put the changed definition in your document if you want to use them.
Post Reply