1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
<html>
<head><title>Help On LaTeX \newcommand and \renewcommand</title></head>
<body bgcolor="#ffffff" text="#000000" link="#000099" vlink="#cc0000" alink="#cc0000">
<h1><font size="-2">Hypertext Help with LaTeX</font><br><font color="#cc0000">\newcommand \renewcommand</font></h1>
<p>
<tt>\newcommand{cmd}[args][opt]{def}</tt>
<br>
<tt>\renewcommand{cmd}[args][opt]{def}</tt>
<br>
<tt>\providecommand{cmd}[args][opt]{def}</tt> --
<a href="versions.html">LaTeX2e</a>
<p>
These commands define (or redefine) a command.
<ul>
<li><TT>cmd</TT> The name of the new or redefined command.
A <tt>\</tt> followed by a string of lower and/or uppercase
letters or a <tt>\</tt> followed by a single nonletter.
For <tt>\newcommand</tt> the name must
not be already defined and must not begin with <tt>\end</tt>; for
<tt>\renewcommand</tt> it must already be defined. The
<tt>\providecommand</tt> command is identical to the <tt>\newcommand</tt>
command if a command with this name does not exist; if it does
already exist, the <tt>\providecommand</tt> does nothing and the
old definition remains in effect.
<p>
<li><TT>args</TT> An integer from 1 to 9 denoting the number of arguments of
the command being defined. The default is for the command to
have no arguments.
<p>
<li><tt>opt</tt> (<strong><a href="versions.html">LaTeX2e only</a></strong>)
If present, then the first of the number of arguments specified by
<tt>args</tt> is optional with a default value of <tt>opt</tt>;
if absent, then all of the arguments are required.
<p>
<li><TT>def</TT> The text to be substituted for every occurrence of cmd; a
parameter of the form #n in cmd is replaced by the text of the
nth argument when this substitution takes place.
</ul>
<h2>Examples</h2>
<tt>\newcommand{\water}{H$_2$O}</tt>
<p>
This would allow one to write, e.g.,
<pre>
The formula for water is \water.
</pre>
or
<pre>
\water\ is the formula for water.
</pre>
Note, in the second case, the trailing <tt>\</tt> followed by a blank
is required to ensure a blank space after the H<sub>2</sub>O;
LaTeX ignores the blank following a command, so the space has
to be specifically inserted with the
<a href="ltx-431.html"><tt>\<space></tt></a>.
<p>
As a second example consider
<p>
<tt>\newcommand{\hypotenuse}{$a^{2}+b^{2}$}</tt>
<p>
Note that this will produce the desired formula
in text (paragraph) mode because of the <tt>$...$</tt> in the definition.
In math mode, however, the first <tt>$</tt>
in the definition will cause LaTeX to leave math mode, causing problems.
<p>
In <strong>LaTeX 2.09</strong> a standard trick for getting around this is
to put the math-mode expression in an
<a href="ltx-265.html"><tt>\mbox</tt></a>, viz.,
<pre>
<tt>\newcommand{\hypotenuse}{\mbox{$a^{2}+b^{2}$}}</tt>
</pre>
<p>
In <strong>LaTeX2e</strong> the
<a href="ensuremath.html"><tt>\ensuremath</tt></a> command has been
provided to alleviate this problem. The argument of the <tt>\ensuremath</tt>
command is always processed in math mode, regardless of the current mode.
Using this mechanism the above could be written as
<pre>
<tt>\newcommand{\hypotenuse}{\ensuremath{a^{2}+y^{2}}}</tt>
</pre>
<hr>
See also <a href="ltx-17.html">Commands</a><br>
Go to <a href="ltx-2.html">LaTeX Table of Contents</a><br>
<hr>
<address>
Revised: Sheldon Green, 23 Jun 1995.
</address>
</body>
</html>
|