The \usepackage
command takes a comma-separated list of packages, so you can
load several in one go: \usepackage{color,graphicx}
for example. If you are
passing options to a package, they will apply to each of the packages in the list.
It’s also easier to comment out packages if they are loaded
separately. So we will stick to loading each package on a separate line.
babel
packageWe showed the babel
package in the main lesson as a way to choose
different hyphenation patterns. It does a lot more than that, depending on the
language(s) being used. For example, in German, it provides some shorthands for
creating ‘soft’ hyphens, and also a way to quickly type umlauts without needing
a German keyboard. Note also how the section heading Table of Contents
normally generated by \tableofcontents
is changed to
the German Inhaltsverzeichnis.
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel} % Notice that the option name is 'ngerman'
\begin{document}
\tableofcontents
\section{"Uber "Apfel und Birnen}
\subsection{Äpfel}
Äpfel sind rot.
\subsection{Birnen}
Birnen sind gelb.
\end{document}
Other language settings make design changes: for example, in traditional
French typography, there is a space before some punctuation signs, like :
,
and this is added automatically if you load babel
with the option french
.
Sometimes, you want an option to be available to all of the packages you’ve
loaded. That is done by giving it on the \documentclass
line: every package
can ‘see’ this list. So to pass the language of a document to all packages,
we might use:
\documentclass[ngerman]{article} % Notice that the option name is 'ngerman'
\usepackage[T1]{fontenc}
\usepackage{babel}
\begin{document}
\tableofcontents
\section{"Uber "Apfel und Birnen}
\subsection{Äpfel}
Äpfel sind rot.
\subsection{Birnen}
Birnen sind gelb.
\end{document}
\newcommand
allows commands with up to nine arguments, the first of which may be optional.
If we take the example from the main lesson, we could make the color optional, defaulting to blue.
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{xcolor}
\newcommand\kw[2][blue]{\textcolor{#1}{\itshape #2}}
\begin{document}
Something about \kw{apples} and \kw[red]{oranges}.
\end{document}
Optional arguments are delimited with []
and if omitted, the default
value specified in the definition is used.
\NewDocumentCommand
From the October 2020 LaTeX release, an extended definition system is available.
In older LaTeX releases this was available via the xparse
package which we use
here for compatibility.
We can repeat the above example but using \NewDocumentCommand
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{xparse} % Only needed for older LaTeX releases
\usepackage{xcolor}
\NewDocumentCommand\kw{O{blue} m}{\textcolor{#1}{\itshape #2}}
\begin{document}
Something about \kw{apples} and \kw[red]{oranges}.
\end{document}
Just as with \newcommand
, \NewDocumentCommand
takes the command
being defined (\kw
here) and the definition body, using #1
to #9
for the arguments, however the difference is in how the arguments are
specified.
Unlike \newcommand
where just the number of arguments is given,
optionally supplying a default for the first, with
\NewDocumentCommand
each argument is specified by a letter so a two
argument command would be specified by {mm}
rather than [2]
. This
is slightly more verbose but allows many more forms of commands to be
defined. Here we just give this simple example where the first
argument is optional, defaulting to blue (O{blue}
) and the second
argument is mandatory (m
).