Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

How do I insert the title in the footer? And decide if it is set or not?

%preteste.sty
\usepackage{fancyhdr}
\usepackage{lipsum}

\renewcommand{\title}[1]{\gdef\@title{#1}\gdef\mytitle{#1}}
\newcommand{\mytitle}{%
   \PackageErrorNoLine{pkgname}
     {No \protect\title\space given}
     {You have to provide a title}%
   \gdef\mytitle{You have to provide a title}%
   \mytitle
}
\fancyfoot{}
\fancyfoot[C]{\mytitle}

See the error if no define \title and no use \maketitle

%titulo.tex
\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{preteste}

% \title{Title}
% \author{R\'egis S. Santos}
% \date{\the\year}

\begin{document}
\pagestyle{fancy}
% \maketitle

\lipsum
\end{document}

fix my code, please.

share|improve this question
2  
add comment

2 Answers

up vote 7 down vote accepted

After \maketitle the title is forgotten. By the way, \title wouldn't print the title anyway: it's a command for declaring what the title is.

\newcommand{\mytitle}{Title}
\title{\mytitle}
...
\fancyfoot[C]{\mytitle}

If you need it in a package, then redefine \title:

\renewcommand{\title}[1]{\gdef\@title{#1}\gdef\mytitle{#1}}
...
\fancyfoot[C]{\mytitle}

This duplication will allow you to say normally

\title{Title}
\author{A. U. Thor}
\maketitle

in the document.

If you want to raise an error if the author doesn't provide a \title declaration, add to your package

\newcommand{\mytitle}{%
   \PackageError{preteste}% use the package name
     {No \protect\title\space given\@gobble}
     {You have to provide a title}%
   \gdef\mytitle{You have to provide a title}%
   \mytitle}

After raising the error, it will redefine itself to print an informative message in the footer. The \title declaration in the document will clear this definition, so there's nothing to worry about.

The titling package can be used, but maybe it's too much for this simple application.

share|improve this answer
    
I've used this method, but I put the \fancyfoot[C]{\title} a file.sty, then it will not work. Do you have any other suggestions? –  Regis da Silva Oct 9 '11 at 23:28
1  
Read egreg's answer more carefully: you need to put fancyfoot[C]{\mytitle} not \title. –  Alan Munn Oct 10 '11 at 0:50
    
Also make sure that you load your file.sty after you have loaded the fancyhdr package, and after you have defined \mytitle –  cmhughes Oct 10 '11 at 1:34
    
Thanks egreg, but how to verify \ifdefined\@title or similar, if the title is not set. –  Regis da Silva Oct 10 '11 at 22:29
    
@RegisdaSilva See edited message –  egreg Oct 10 '11 at 23:15
show 5 more comments

I think an easy solution involves the following instructions in the document's preamble after the \title, \author, and \date commands have been set up:

\makeatletter
\let\doctitle\@title
\makeatother

You can then insert the title string in the footer as follows:

\fancyfoot[C]{\doctitle}

Finally, if you want to put all of this stuff in a package, be sure to invoke this package only after the \title has been set up and the fancyhdr package has been loaded. Happy TeXing!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.