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.

6
  • Note that the base document classes define \title and \author that redefines the values of \@title and \@author, used when issuing \maketitle. Perhaps you can elaborate more on what you mean by "access the title and author".
    – Werner
    Jun 27, 2012 at 23:28
  • I mean I want to write in my .cls file, \fancyfoot[C]{AUTHOR—TITLE}, for example. I tried using \@title and @author there but when I run pdflatex, the footer is simply .
    – mk12
    Jun 27, 2012 at 23:31
  • This is essentially a duplicate of Use the values of \title \author and \date in a custom title page
    – Alan Munn
    Jun 27, 2012 at 23:39
  • @Mk12: In order for this to not be considered a duplicate by others, you need to include your comment about modifying the footer to your liking.
    – Werner
    Jun 27, 2012 at 23:47
  • I said I want to insert them in the footer from a custom class in the original post, didn't I?
    – mk12
    Jun 28, 2012 at 2:02

1 Answer 1

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}
3
  • hmm, I tried that but they still aren't showing up…
    – mk12
    Jun 28, 2012 at 2:00
  • Oh, it's because I have \maketitle… Should I use the titling package then if I want to kept maketitle?
    – mk12
    Jun 28, 2012 at 2:21
  • @Mk12: I've updated my solution to include your \maketitle usage requirement.
    – Werner
    Jun 28, 2012 at 5:50

Not the answer you're looking for? Browse other questions tagged .