5

Possible Duplicate:
Use the values of \title, \author and \date on a custom title page

How can I access the title and author in my custom class based on article, so that I can insert them in the footer that I automatically set up?

I tried \@title and \@author but nothing shows up.

mk12
  • 1,847
  • 2
  • 15
  • 17

1 Answers1

6

\@title and \@author are technically only defined once you use \title and \author:

\def\title#1{\gdef\@title{#1}}
\def\@title{\@latex@error{No \noexpand\title given}\@ehc}
\def\author#1{\gdef\@author{#1}}
\def\@author{\@latex@warning@no@line{No \noexpand\author given}}

That is, if you reference \@title and/or \@author without calling \title and \author first, it should produce a warning. To avoid this situation, based on the fact that you're writing a special class, you could use a different approach:

\let\@title\@empty
\let\@author\@empty
\fancyfoot[C]{\ifx\@author\@empty\else\@author~--~\fi\ifx\@title\@empty\else\@title\fi}

The above sets the footer to be AUTHOR - TITLE if both are defined. It will only be TITLE if no author is defined. It can be expanded to properly work if an author is given but no title.

Here's a minimal example using lipsum and fancyhdr:

enter image description here

\documentclass{article}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{fancyhdr}% http://ctan.org/pkg/fancyhdr
\makeatletter
\fancyfoot{}
\fancyfoot[C]{\ifx\@author\@empty\else\@author~--~\fi\ifx\@title\@empty\else\@title\fi}
\let\@title\@empty
\let\@author\@empty
\makeatother

\title{My title}
\author{Me}
\pagestyle{fancy}%
\begin{document}
\lipsum[1-3]
\end{document}​

If you're actually using \maketitle as well, you need a little more work done. The MWE below adds \@@title and \@@author instead of \@title and \@author:

\documentclass{article}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{fancyhdr}% http://ctan.org/pkg/fancyhdr
\makeatletter
\def\title#1{\gdef\@title{#1}\gdef\@@title{#1}}
\def\author#1{\gdef\@author{#1}\gdef\@@author{#1}}
\let\@@title\@empty
\let\@@author\@empty
\fancyfoot{}
\fancyfoot[C]{\ifx\@@author\@empty\else\@@author~--~\fi\ifx\@@title\@empty\else\@@title\fi}

\title{My title}
\author{Me}
\begin{document}
\maketitle
\thispagestyle{fancy}%
\lipsum[1-3]
\end{document}
Werner
  • 603,163