# How to use the content of \title{} as a reference?

I want to be able to reference to my document title by name. How can I achieve that?

\documentclass{scrartcl}
\usepackage{hyperref}

\begin{document}

\title{Example}\label{title}
\author{me}
\maketitle

The title is: "\nameref{title}" (Here it should read "Example")

\end{document}

-

If you look up \title in the latex source, you'll see it's defined like this:

\def\title#1{\gdef\@title{#1}}


So the title is stored in a regular macro named \@title. The only problem with using that to recover the title is the @ sign, normally not allowed in control sequences.

To get around this, alias a regular macro to \@title:

\documentclass{scrartcl}
\title{Example}
\author{me}

\makeatletter
\let\inserttitle\@title
\makeatother

\begin{document}
\maketitle

The title is: \inserttitle'' (Here it should read Example'')

\end{document}


\label does something a little bit different. It saves the value of the currently active counter (requiring an aux file and a second pass), and you want to save the expansion text of a macro. Macros are well suited for the latter, and it would be hard to overload \label and \nameref to do the former.

You'll also see that I reorganized your code. I think it's best to put macros that do not produce output like \title and \author in the preamble rather than within the document. There shouldn't be anything between \begin{document} and \maketitle because you will likely get extra spaces from blank lines. Also, having those “metadata” declarations in the preamble, and especially right after \documentclass{} makes it easier to identify the file to the user editing it.

-
\documentclass{scrartcl}

\title{Example}
\author{me}
\makeatletter\let\Title\@title\makeatother
\begin{document}

\maketitle
The title is: "\Title"

\end{document}

-
I love it. Not only did we post the same solution at the same time, we made the same reorganizations! – Matthew Leingang Apr 14 '11 at 9:48
Thanks guys... I'll go with Matthews answer because of the detailed explanation :) – meep.meep Apr 14 '11 at 10:10

You can use the authoraftertitle package which makes the values of \author{...}, \title{...} and \date{...} available via the commands \MyAuthor, \MyTitle and \MyDate (although I think it's kind of overkill to have a package only for this single purpose...):

\documentclass{scrartcl}
\usepackage{authoraftertitle}
\usepackage{hyperref}

\begin{document}

\title{Example}
\author{me}
\maketitle

The title is: "\MyTitle" (Here it does read "Example")

\end{document}

-
Interesting. I kind of agree that this is overkill. On the other hand, this solution comes in under the “hack” threshold (which I define to be use of @). – Matthew Leingang Apr 14 '11 at 17:04
@Matthew: This "package" is 32 lines long (including comments) and is fairly similar to your 'hack'. On the basis of any code that might be reused should go in a separate file, it seems quite reasonable to put this in a package. So I think the "overkill" is overkill! – Loop Space Apr 15 '11 at 7:11