We looked at this briefly in § 2.3 above,
where we saw how the title, author, and date had to be
specified before the
\maketitle command could be used. In
Figure 7.1 above we saw a simple example of a
redesigned title page. In this section we'll look at how it
works, and how to extend it.
There’s a good reason for gathering the information before
you use it: many title pages are complex, with a lot more
information on them than a simple title and author and date;
and in books and reports, there are many more pages that
follow the title page before you get to the body of the
document. All of those pages contain information
about information, which is what metadata
is.
E.1.1 Gathering the data
But we can start with the three items we already know.
They can all be handled the same way: a) define a default that the writer can’t change
accidentally; b) create a command the writer can use
explicitly; and c) build the metadata into a title page
To do this, LATEX makes use of the fact that an
‘at’ sign (@) is not a
letter: it’s actually classified as
‘other’. But as with everything else
in LATEX, that can be redefined, so we can use the
@ in a command for
internal use to store metadata, but if
authors accidentally try to use it in the document, it won't
let them. This also means we can give them default values
that will be used if an author forgets
to use the normal commands (remember we already saw in § 2.3 above how
today’s date will be used if you don't specify a date at
all). We can also add provision for a subtitle in the same
way.
\makeatletter
\def\@title{Untitled document}
\def\@subtitle{\relax}
\def\@author{A N Other}
\def\@date{\today}
\makeatotherIn fact LATEX already defines
\@title, \@author, and
\@date, so you could just add
\@subtitle, but it’s useful to re-do it to
see how the default values work.
For \@subtitle we use the
\relax command, which is just a
placeholder, because there really isn't an error if a
document has no subtitle.
E.1.2 Defining the user interface
Now we’re ready to make the commands the author will
actually use in the document. Again, they all work the same
way, by taking one argument each and storing it in the
@-commands we just created. The
\title, \author, and
\date commands are of course already
defined in LATEX, so we have to use
\renewcommand for them, and just
\newcommand for the
\subtitle because it’s new.
\renewcommand{\title}[1]{\gdef\@title{#1}}
\newcommand{\subtitle}[1]{\gdef\@subtitle{#1}}
\renewcommand{\author}[1]{\gdef\@author{#1}}
\renewcommand{\date}[1]{\gdef\@date{#1}}Each command can now accept one argument, and its value
(#1) will be stored in the
@-commands we just created.
Put all these new definitions in between the
\makeatletter…\makeatother
commands.
E.1.3 Implementing the design
Finally we can now redesign our title page. I don't have
a design in mind, so I'm just going to range everything on
the left. To do this, you rewrite the
\maketitle command:
\renewcommand{\maketitle}{%
\begin{titlepage}
\sffamily
\raggedright
\null\vspace{2cm}
\Huge\bfseries\@title\par
\if\@subtitle\relax\else
\vspace{1cm}
\huge\bfseries\@subtitle\par
\fi
\vspace{4cm}
\LARGE\fontseries{m}\selectfont\@author\par
\vfill
\normalsize\@date\par
\clearpage
\end{titlepage}
}This also contains @ signs, so it too has
to go between
\makeatletter…\makeatother
commands. Within the titlepage environment
you can see we
are doing this:
Set the sans-serif font, ragged right alignment, and
2cm space (the \null is required, or
you can use \vspace* instead,
otherwise LATEX will strip away unprotected white-space
at the top of a page).
Use the Huge font size and bold face,
and issue the \@title,which contains
whatever the author has typed in
\title, followed by a
\par (remember font sizes and
baselines only get implemented when a paragraph is
terminated).
Now something new:
\if…\else…\fi
which is a ‘conditional’:
something that lets LATEX decide if a condition is
true or false, and take whatever action is given.
Here, if the \@subtitle is [still]
\relax (that is,
\subtitle was never used), then do
nothing; \else (otherwise) leave 1cm
space and set the subtitle in the huge font
size (in bold).
The \@author gets the same
treatment, except there is no way to revert out of bold
except by telling LATEX to use the m
(medium) font;
Finally, fill to the end of the page and output the
date, then clear the page ready for the next one.
E.1.4 Using it in practice
Now we can put this all into a test file and start a new
document as in Figure E.1 below.
Figure E.1: Testing a new layout with added metadata
\documentclass{book}
\usepackage{fontspec}
\makeatletter
\def\@title{Untitled document}
\def\@subtitle{\relax}
\def\@author{A N Other}
\def\@date{\today}
\renewcommand{\title}[1]{\gdef\@title{#1}}
\newcommand{\subtitle}[1]{\gdef\@subtitle{#1}}
\renewcommand{\author}[1]{\gdef\@author{#1}}
\renewcommand{\date}[1]{\gdef\@date{#1}}
\renewcommand{\maketitle}{%
\begin{titlepage}
\sffamily
\raggedright
\null\vspace{2cm}
\Huge\bfseries\@title\par
\if\@subtitle\relax\else
\vspace{1cm}
\huge\bfseries\@subtitle\par
\fi
\vspace{4cm}
\LARGE\fontseries{m}\selectfont\@author\par
\vfill
\normalsize\@date\par
\clearpage
\end{titlepage}
}
\makeatother
\begin{document}
\title{The Habit}
\subtitle{or They’re Back Again}
\author{J R Hartley}
\date{1973}
\maketitle
\chapter{An unexpected parley}
\end{document}Process this with LuaLATEX and you'll get
a title page…and something else. This is the
book class, and I added the title of
the first chapter, so LATEX has correctly spotted that in
books, printing is usually double-sided, and chapters always
start on a right-hand page, so it has output an empty page 2
before starting the first chapter.
E.1.5 Packaging it up
Last of all, you would normally put all that redesign
into a style file or package, even a class file if you were
going to make a big production out of it. When you do this,
you need to add a few lines identifying what the file is,
and remove any \makeatletter and
\makeatother commands, because
@ signs are legal in
commands inside styles and packages.
Let’s call it novel.sty. Cut out
all the code you wrote, delete
\makeatletter and
\makeatother, and add these three lines at
the top:
the first command
(\NeedsTeXFormat) specifies LATEX2ε
from 2020 (meaning 2020-or-later). To match it to your
own installation (which is all you have tested it on so
far), you can use the date you see in the console log
when you run LATEX, where it says LaTeX2e
<2020-02-02> patch level 2 (or
whatever date it on yours)
the second command
(\ProvidesPackage) gives the name of
your new package, and in square brackets you provide the
date and the version number and a line of
explanation.
You should also add a comment line saying who wrote it,
and add \endinput at the very end of the
file.
\NeedsTeXFormat{LaTeX2e}[2020/02/02]
\ProvidesPackage{novel}[2023/08/01 v0.01
Package code for novel layout]
% Written by ...........................
\def\@title{Untitled document}
\def\@subtitle{\relax}
\def\@author{A N Other}
\def\@date{\today}
\renewcommand{\title}[1]{\gdef\@title{#1}}
\newcommand{\subtitle}[1]{\gdef\@subtitle{#1}}
\renewcommand{\author}[1]{\gdef\@author{#1}}
\renewcommand{\date}[1]{\gdef\@date{#1}}
\renewcommand{\maketitle}{%
\begin{titlepage}
\sffamily
\raggedright
\null\vspace{2cm}
\Huge\bfseries\@title\par
\if\@subtitle\relax\else
\vspace{1cm}
\huge\bfseries\@subtitle\par
\fi
\vspace{4cm}
\LARGE\fontseries{m}\selectfont\@author\par
\vfill
\normalsize\@date\par
\clearpage
\end{titlepage}
}
\endinputFigure E.2: The typeset title page
E.1.6 Putting it to work
Now you can give this as a template to an author to fill
in the title, subtitle, author, and date, and start
typesetting.
\documentclass{book}
\usepackage{fontspec,novel}
\begin{document}
\title{The Habit}
\subtitle{or They're Back Again}
\author{J R Hartley}
\date{1973}
\maketitle
\chapter{An unexpected parley}
\end{document}They don't need to know anything about how it works:
they just use the commands as normal, and out comes the new
style title page.
E.1.7 Extending the metadata
One immediate benefit of storing the data for re-use is
that if you implement running headers and footers, you can
use any of these @-commands in them, which
avoids errors in retyping or having to specify the metadata
in more then one place.
In practice, there is usually a lot more that which goes
into the titling of a book or a report or a journal article.
Some or all of the following items may be needed, usually
spread over several pages called
‘preliminaries’ or
‘prelims’ for short:
title, subtitle, author or editor, and date, as we
know;
if the work was translated, the original title and
subtitle may be needed, and the name of the
translator;
there may be many authors or editors, especially on
a report or article, and maybe other contributors;
each author may need their qualifications, email
address, and institutional affiliation;
in journals, there may be date submitted,
date received, date last edited, and others;
there will almost always be some legal blurb and a
copyright notice with a separate date and owner;
the name and logo of the publisher, with their
address;
if the document is part of a series, the name of the
series and related series, and the names of the
editorial board;
the edition and the publication history;
an abstract (in reports, white papers, articles etc,
but not books)
acknowledgements and dedication;
warnings or admonishments;
declarations of interest or independence.
Most of these can be implemented in exactly the way we
have used, as @-commands that can be used or
not, as the case demands.
Longer passages of text such as an abstract can be
typeset into a savebox (see for example
the \Sbox feature of
the fancybox package), and then output
on the relevant page. Using this approach means authors
don't have to bother knowing the right order of things:
they just specify what needs specifying and your code takes
care of the arrangement.
Some of the pages may need to go in a fixed order:
universities and publishers in particular can be very
picky about having everything in the right sequence.
A common book sequence for prelims might be:
cover page (title page)
blank, or maybe legal;
half title;
publisher identity, board and series;
abstract;
continuation or blank;
dedication;
warning or blank;
admonishments;
blank;
table of contents;
list of figures;
list of tables;
list of acronyms or taxa;
blank;
introduction;
preface;
foreword;
first chapter.
The intro, preface, and foreword all begin on right-hand
pages in western Latin designs, but they may have many
continuation pages among them, depending on how long they
are. Even though we are in a digital age now, the heavy hand
of the western world’s history of printing practices still
affects the layout of formal documents, so the list above is
aimed at double-sided printing, hence the blank pages,
because important stuff always starts
on a right-hand (odd) page (left-hand for left-to-right
language documents).
In theory you could automate the entire sequence in a
single \maketitle command, thereby
ensuring that all documents in this series come out with the
information easily findable because it’s in the same place
in each document, and that the authors don't have to worry
about getting it ordered.