8.2 Redefining Commands
Commands can be redefined using the command:
This has exactly the same format as \newcommand
but is
used for redefining existing commands.
Again there is a starred version to indicate that the command
is a short command.
Caveat:
\c
(the
cedilla accent command) to display a maths bold
“c” to indicate a
vector. Later, author B, uses the cedilla accent, say, in the name
François:
Author A's hack turns this into
Franccois.
Example (Redefining List Labels):
Recall the itemize environment discussed in
§4.4.1. Unordered Lists. You may have up to four nested
itemize environments, the labels for the outer environment
are specified by the command \labelitemi
, the labels for
the second level are specified by \labelitemii
, the third
by \labelitemiii
and the fourth by \labelitemiv
.
By default, \labelitemi
is a bullet point (
),
\labelitemii
is an en dash (
),
\labelitemiii
is an
asterisk (
) and
\labelitemiv
is a centred dot (
). These can be changed by redefining
\labelitemi
etc.
Recall from Table 4.2 that the command
\dag
produces a dagger symbol, we can use this symbol
instead of a bullet point:
\renewcommand
*{\labelitemi
}{\dag
}
\begin
{itemize}
\item
Animal
\item
Mineral
\item
Vegetable
\end
{itemize}
Output:
Here's another example, it uses the PostScript font ZapfDingbats via the pifont package:
\renewcommand
*{\labelitemi
}{\ding
{43}}
\begin
{itemize}
\item
Animal
\item
Mineral
\item
Vegetable
\end
{itemize}
Output:
Example (Redefining the Default Font):
Recall from §4.5.3. Changing Document Fonts that the default font family is usually the serif (Roman) family. So what happens if you need to write your entire document in, say, Helvetica? The default font family name is stored in:
This command is usually
defined to be just \rmdefault
, which in turn stores the name of the
default serif font (initially cmr, Computer Modern Roman). If
you want the default font to be sans-serif, all you
need do is add the following line to the
preamble:
\sfdefault
stores the name of the default sans-serif font (initially cmss,
Computer Modern Sans-Serif) and
the helvet package redefines \sfdefault
to phv,
which is the identifier for the Helvetica font.
So the following document will be in Helvetica:
\documentclass
{scrartcl}
\usepackage
{helvet}
\renewcommand
{\familydefault
}{\sfdefault
}
\begin
{document}
This is a sample document.
\end
{document}
Incidentally, you may have noticed in §4.5.3. Changing Document Fonts
that although I said I'd used the anttor and
libris packages to set the serif and sans-serif families for
the PDF version of this book, I
didn't mention anything about the typewriter (monospaced) font. I used
the TXTT font, but that doesn't have a corresponding package. You
just redefine \ttdefault
to txtt:
Example (Redefining Fixed Names):
You may have noticed that LaTeX automatically generates pieces of text such as “Chapter”, “Figure”, “Bibliography”. These are generated by the commands shown in Table 8.1.
Command | Default Text |
---|---|
\contentsname |
Contents |
\listfigurename |
List of Figures |
\listtablename |
List of Tables |
\bibname |
Bibliography |
\refname |
References |
\indexname |
Index |
\figurename |
Figure |
\tablename |
Table |
\partname |
Part |
\chaptername |
Chapter |
\appendixname |
Appendix |
\abstractname |
Abstract |
You can change the defaults using \renewcommand
. For
example, suppose you want the table of contents to be labelled
“Table of Contents”, instead of the default “Contents”, you
would need to do:
[Changing the words babel uses]In fact, the
babel package (see §5.8. Multi-Lingual Support: using the babel package) uses this method
to redefine the commands in Table 8.1 whenever you
switch language using
\selectlanguage
or within the contents of
the otherlanguage environment. This unfortunately has the
side-effect that means if you try to redefine these commands,
babel will automatically overwrite your definition whenever
there's a language change, which includes at the beginning of the
document environment. Instead you need to use
babel's \addto
mechanism.
This patches the definition of a command (specified in the first
argument) adding <code> to the end of the command
definition. Whenever babel switches the current language, it
uses the command \captions
<language>, which performs
all the redefinitions of commands like those listed in
Table 8.1. For example, if you are using babel
with the english option and you want to change
\contentsname
so that it does “Table of Contents” instead
of “Contents”, you need to do:
Notes:
\foo
recursively
calls itself. Instead you should use one of the commands provided by
the etoolbox package (such as \appto
, which has the same
syntax as babel's \addto
described above). For further details,
read the etoolbox documentation.
Exercise 21: Renewing CommandsIf you did Exercises 16 and 17, go back to that document and changed the figures and tables so that they are labelled “Fig” and “Tab” instead of “Figure” and “Table”. Hint: you need to redefine
\tablename
and\figurename
.
This book is also available as A4 PDF or 12.8cm x 9.6cm PDF or paperback (ISBN 978-1-909440-00-5).