About
Shop
LaTeX
Software
Books
Gallery
News
Contact
Blog
Settings
Account
Latest news 2024-10-15: New blog post: Tales for Our Times Book Launch.


8.2 Redefining Commands

Commands can be redefined using the command:

\renewcommand{<cmd>}[<n-args>][<default>]{<text>}

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:

Never redefine a command whose existing function is unknown to you or just because you want to use a particular command name, regardless of its previous function. By way of illustration: as a production editor, I have to combine articles by different authors into a single book. Each author supplies the LaTeX code for their own article. Every so often, I get code that redefines a command for the convenience of the author. Later on another author tries to use the same command, on the assumption that the command behaves according to its original definition. This tends to involve the accent commands as they are short and that saves the author typing. It usually goes along these lines: author A redefines \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:
Fran\c{c}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 ( bullet
symbol ), \labelitemii is an en dash ( en dash ), \labelitemiii is an asterisk ( asterisk ) and \labelitemiv is a centred dot ( centered
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:

List uses dagger symbol instead of a bullet point for
the marker

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:

List uses a pointing finger instead of a bullet point
for the marker

In the above example, it would actually be easier to use the dinglist environment defined in the pifont package:

\begin{dinglist}{43}

\item Animal

\item Mineral

\item Vegetable

\end{dinglist}

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:

\familydefault

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:

\renewcommand{\familydefault}{\sfdefault}

\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}
Similarly, if you want the default font to be monospaced (typewriter) then you'd need to do:

\renewcommand{\familydefault}{\ttdefault}

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:

\renewcommand*{\ttdefault}{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.


Table 8.1: Predefined Names (Book and report style classes (such as scrreprt and scrbook), article-style classes (such as scrartcl), remainder book, report and article-style classes)
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:

\renewcommand*{\contentsname}{Table of Contents}

[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.

\addto{<command>}{<code>}

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:

\addto{\captionsenglish}{%
  \renewcommand{\contentsname}{Table of Contents}%
}

Notes:

Take care if you want to patch an existing command. For example, suppose you want to append something to the action of a command. You might be tempted to do
\renewcommand*{\foo}{\foo Something else}
This will cause an infinite loop where \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 Commands

If 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.

You can download or view the solution.


This book is also available as A4 PDF or 12.8cm x 9.6cm PDF or paperback (ISBN 978-1-909440-00-5).

© 2012 Dickimaw Books. "Dickimaw", "Dickimaw Books" and the Dickimaw parrot logo are trademarks. The Dickimaw parrot was painted by Magdalene Pritchett.

Terms of Use Privacy Policy Cookies Site Map FAQs