# How can I programmatically obtain the title of a LaTeX document?

I have a series of documents that are programmatically rendered by `pdflatex` from within a GNU Make `makefile`.

I'd like to be able to write a Make target that "polls" each document for its `\title` so that I may amalgamate each one in a "document list".

Can I do this? If so, how?

-
I'm not quite sure how you want to get the title from the document to `make`: it might be handy to give more detail as there will have to be some programming at the LaTeX end in any case. (LaTeX standard classes store the title in `\@title` but it's not kept after `\maketitle`.) –  Joseph Wright Oct 30 '12 at 11:43
@JosephWright: I think that's probably the answer. There isn't really any more detail as this is merely a concept at this stage. –  Lightness Races in Orbit Oct 30 '12 at 11:46
Depends a bit on how you define "title". Is the title whatever is contained in `\title{...}`? Or is the title whatever gets put in the PDF metadata for the title? When polling, do you just want to get the title or is it okay to compile the whole document? If the former, what if there are macros in the title - should they be expanded (if they can)? –  Loop Space Oct 30 '12 at 11:51
@AndrewStacey: Whatever's in `\title`. I didn't even know I could configure the PDF metadata distinctly - I'll look into that. Compiling the whole document first would be acceptable but clearly non-optimal. Macros should probably be expanded, yes. I can see now that there are more factors to this than I first realised. –  Lightness Races in Orbit Oct 30 '12 at 11:56
Yup! The thing to do is to look at the documents you actually have and figure out some parameters that will work for those. If the title is quite simple, you could just pipe the documents through a perl script. If it has some expandable macros, you could run tex up to the `\title` declaration whereupon it expands its argument (as much as possible) and merrily quits (actually, to do it properly it should be the `\maketitle` that forces the exit). That would save on compiling the rest of the document. –  Loop Space Oct 30 '12 at 11:58

The standard classes store the title given using `\title` in a global macro called `\@title`. However, this information is not retained after `\maketitle` (for historical reasons). Thus if you want the information to persist, the easiest way is to redefine `\title` to save the data in a macro of your choosing

``````\makeatletter
\xdef\my@title{No \string\title\space given!}
\renewcommand{\title}[1]{%
\gdef\@title{#1}%
\gdef\my@title{#1}%
}
\makeatother
``````

while of course retaining the standard set up for `\maketitle`. You can then use `\my@title` to recover the information, for example by writing it to the log file.

-
Note I've set up `\my@title` to produce something even if no title was actually given: this may or may not be what is needed. –  Joseph Wright Oct 30 '12 at 11:51
If the aim is simply to write the title to a log file (or some other file), why not just adjust the `\title` macro to write its argument to a file? No need to remember it then. –  Loop Space Oct 30 '12 at 11:52
@AndrewStacey Would also make a reasonable answer: the question is rather broad and the best approach will depend on the detail. –  Joseph Wright Oct 30 '12 at 12:08
Hence my comments on the main question. –  Loop Space Oct 30 '12 at 12:11