|
|
|||
![]() |
Department of Engineering |
| University of Cambridge > Engineering Department > computing help > LaTeX |
LaTeX lets you write new commands (or adapt old ones), create or adapt environments, and write your own packages and even classes. This document will give detailed descriptions on how to do simple things, but won't cover all the issues relating to class creation.
Sometimes you might want to repeat a long expression. The tidy way to do this is to use \def (or \newcommand). The following defines \xdt to produce something like X.desktop
\def\xdt{$\cal X\!\!$\texttt{.desktop}}
One problem that can arise is that your new code can introduce unwanted space. The Spaces in macros section of the FAQ covers this issue.
Use \newcommand to create a command and \renewcommand to over-write an existing one. You can use only letters in the names of commands.
\newcommand{\
commandname}[number of arguments]{
command text, using #1, #2 etc to denote arguments}
\newcommand{\ve}[1]{\(#1_1 ... #1_n\)}
\ve{x}
produces as output
x1 ... xn.
A problem with this example is that it shouldn't change to math mode if
LaTeX is already in that mode. A better try would be
\newcommand{\ve}[1]{\ensuremath{#1_1 ... #1_n}}
which will only change to math mode if it's necessary.
weekday macro uses the ifthen
package. Note the use of '%' characters to suppress spaces in the final
output. weekday{3} should print out Wednesday.
\usepackage{ifthen}
\newcommand{\weekday}[1]%
{%
\ifthenelse{\equal{#1}{1}}{Monday}{}%
\ifthenelse{\equal{#1}{2}}{Tuesday}{}%
\ifthenelse{\equal{#1}{3}}{Wednesday}{}%
\ifthenelse{\equal{#1}{4}}{Thursday}{}%
\ifthenelse{\equal{#1}{5}}{Friday}{}%
\ifthenelse{\equal{#1}{6}}{Saturday}{}%
\ifthenelse{\equal{#1}{7}}{Sunday}{}%
}
\weekday{3}
\newcommand\bibname{Bibliography}
so adding
\renewcommand{\bibname}{References}
to your file should achieve what you want.
Counters (e.g. figure - a variable that stores the figure
number) have related commands
(e.g. thefigure) to control their appearance, so
they're easy to customise
\renewcommand\thefigure{\roman{figure}}
produces figure numbers in lower case roman numerals.
Longer commands can be adapted too. Remember however, that if the
command involves a @ character you have to enclose your changes in
\makeatletter ... \makeatother or put the code
into a package. Here's an example that
changes the appearance of section headings, making them into centred
small caps -
\makeatletter
\renewcommand{\section}{\@startsection{section}{1}{0mm}
{\baselineskip}%
{\baselineskip}{\normalfont\normalsize\scshape\centering}}%
\makeatother
\begin{document}
The following's adapted from the TeX Frequently Asked Questions -
Rather than overwriting an old command it's common to want to add some
code at the beginning or the end of it.
Suppose we want a version of a command that does some
small extension of its original definition: we might try:
\renewcommand{\splat}{addedcode\splat}
However, this would not work: a call to \splat would execute
addedcode, and then call the redefined \splat again; this is an infinite recursive loop.
Fortunately, the TeX primitive \let command comes to our rescue; it allows us to take a "snapshot" of the current state of a command, which we can then
use in the redefinition of the command. So:
\let\Oldsplat\splat
\renewcommand{\splat}{addedcode\Oldsplat}
effects the required patch, safely. Adding things at the end of a command
works similarly.
If \splat takes arguments, one must pass them on:
\renewcommand{\splat}[2]{addedcode\Oldsplat{#1}{#2}}
\newenvironment{\
environmentname}[number of arguments][
default value of the 1st (optional) argument]{
entry code, using #1, #2 etc to denote arguments}
{
exit code - arguments can't be used}
Simple use isn't as painful as it looks - the following provides a
variant of the itemize command, emphasising the items.
\newenvironment{emlist}{\begin{itemize} \em}{\end{itemize}}
\begin{emlist}
\item first comment
\item second comment
\end{emlist}
The end of the environment ends the scope of the emphasis.
produces
The end of the environment ends the scope of the emphasis.
\renewenvironment is used to change existing environments, but you can't change the number of arguments an environment takes.
A package needn't have any special code, but usually it starts with some special package commands
| | computing help | LaTeX | |