In LATEX it is easy to build up your own commands. Simply put
where definition is any LATEX input. Suppose you want to write intoxication, typeset as\newcommand{\mycmd}{definition}
\emph{intoxication}
throughout your document. This is a pain to type, and you could simply put
the command
\newcommand{\intox}{\emph{intoxication}}
between \documentclass{...} and \begin{document} (in the
preamble) and then use it with \intox{} as shown in
Figure 11.
Note the pair of braces after the command. These are necessary because
without them the space after the command is swallowed up as a ``command
terminator'' and then the next word is run into it. Thus, if you type
\intox in ..., then you get ``intoxicationin ...''
This is actually a silly example; most decent text editors will have some sort of ``abbrev'' function which will allow you to define abbreviations as you are typing, and then fill them in for you. This will make your document much more readable. A more useful example is the following.
In mathematics some people write the inner-product of the vectors
and
as
which is typeset as
$\langle a,b \rangle$
\newcommand as above, then you should write
\newcommand{\ab}{\ensuremath{\langle a,b \rangle}}
This will allow you to use the command in regular text or in math mode, as
shown in Figure 12.
You do not need to write \ab{} when in math mode because any spacing
in the input is ignored anyway.
The advantage of using \newcommand for this is that if you later
decide that the correct way of writing inner-product is
, then you
can simply change the definition of the command to reflect this.
You may also include arguments in the definition of a new command. Suppose you want to take the inner product of different vectors. In this case you need two arguments, one for each vector. The inner product becomes:
\newcommand{\ab}[2]
{\ensuremath{\langle #1,#2 \rangle}}
Where the `2' means require two arguments, which are then placed in
locations #1 and #2 respectively. The arguments may be
almost any valid LATEX input. For example, with this definition,
\ab{x_i}{y_j} produces
.
If your command name \mycmd in
has already been defined then you will get an error when you process the file. For example\newcommand{\mycmd}{definition}
\newcommand{\l}{\ensuremath{\ell}}
gives
! LaTeX Error: Command \l already defined.
If you are sure that you have no need for the previous definition of
\l, then you can get around this with
\renewcommand{\l}{\ensuremath{\ell}}
In this case it is probably okay, since \l gives `$\ell$ gives `\cr is a
carriage-return in TEX and is used a lot behind the scenes.
You may also define your own environments, and redefine existing ones, but that is a bit beyond the scope of this article. Many people have written their own sets of commands for specific purposes and made them publicly available. For example the package fancybox produces different boxes. To use these simply put
\usepackage{fancybox}
in the preamble, and then use the commands this provides. If you get an
error such as
! LaTeX Error: File `fancybox.sty' not found.
then either that package is not installed, or else TEX can't find it.
You can download packages from CTAN, the Comprehensive TEX Archive
Network, at
ftp://ctan.tug.org/pub/tex-archive/.
Once you have the file fancybox.sty you can put it in the current
directory and TEX will find it there.
\newcommand{\mycmd}{definition}
\newcommand{\mycmd}{\ensuremath{definition}}
\newcommand{\mycmd}[n]{definition}
#1, #2,...,#n. \mycmd in text mode, and you need a space
immediately following, then you must use it with \mycmd{},
{\mycmd} or \mycmd\ \mycmd has already been defined, then you may redefine it with
\renewcommand{\mycmd}{...}
\usepackage{packagename} in the
preamble.
Some packages change the nature of the whole document. They are no longer a simple package of commands, but a new document class. The document class is set once, in the first command of your file,
I used\documentclass[options]{class}
\documentclass[12pt]{article} in the example on
pages
-
. The standard
document classes are
| article | For short articles |
| report | For longer reports |
| book | For books |
| slides | For overhead projector-type slides |
| letter | For letters |
Most styles accept various options--the 12pt option above says use 12 point type. The default is 10 point, and the other type size option is 11 point. The article, report and book classes are very similar, while slides and letter are more specialized.
Writing a document class is more complicated than a simple package because
one must think of the structure of the entire document. It is fairly
easy to modify the behaviour of a standard class with the
\renewcommand and \renewenvironment commands, although you
should beware of unexpected side-effects.
If this is not enough then the easiest way to define a new document class
is to base it on an existing one, and import that first, then make your
changes. For example the letter on page
uses a custom class,
based on the standard letter class shown on the next 3 pages.
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{myletter}
\RequirePackage{color,calc,ifthen}
\newboolean{@uk}\setboolean{@uk}{false}
\DeclareOption{uk}{\setboolean{@uk}{true}}
\DeclareOption*{%
\PassOptionsToClass{\CurrentOption}{letter}}
\ProcessOptions
\LoadClass{letter}
\newcommand{\@email}{rjudd@math.missouri.edu}
\newcommand{\email}[1]{\renewcommand{\@email}{#1}}
\newcommand{\@telephone}{Telephone: (573) 882--6221}
\renewcommand{\telephone}[1]
{\renewcommand{\@telephone}{#1}}
\setlength{\topmargin}{0.5in-\headheight-\headsep}
\setlength{\textheight}{8in}
\setlength{\oddsidemargin}{0pt}
\setlength{\evensidemargin}{\oddsidemargin}
\setlength{\marginparwidth}{0.5in}
\setlength{\textwidth}{6.5in}
\renewcommand{\ps@firstpage}
{\setlength{\headheight}{20pt}%
\renewcommand{\@oddhead}{%
\parbox{\textwidth}{\textsf{Robert Judd} \hfill
\texttt{\@email}\\
{\color[named]{RedViolet}%
\rule[.6\baselineskip]{\textwidth}{1pt}%
\vspace{-.9\baselineskip}%
}\\
\textsf{Dept. of Math., Columbia, MO 65211}
\hfill
\textsf{\@telephone}}}%
\renewcommand{\@oddfoot}{}%
\renewcommand{\@evenhead}{}%
\renewcommand{\@evenfoot}{}}
\newcommand*{\uktoday}
{\number\day\space\ifcase\month\or
January\or February\or March\or April\or
May\or June\or July\or August\or
September\or October\or November\or December\fi
\space\number\year}
\ifthenelse{\boolean{@uk}}
{\renewcommand{\today}{\uktoday}}{}
\providecommand{\@oddhead}{}
\providecommand{\@oddfoot}{}
\providecommand{\@evenhead}{}
\providecommand{\@evenfoot}{}
\renewenvironment{letter}[1]
{\newpage
\if@twoside \ifodd\c@page
\else\thispagestyle{empty}\null\newpage\fi
\fi
\c@page\@ne
\interlinepenalty=200%
\@processto{\leavevmode\ignorespaces #1}}
{\stopletter\vfil\@@par\pagebreak\@@par
\if@filesw
\begingroup
\let\\=\relax
\let\protect\@unexpandable@protect
\immediate\write\@auxout{%
\string\@mlabel{\returnaddress}
{\toname\\\toaddress}}%
\endgroup
\fi}
\pagestyle{empty}
The most popular classes, other than the standard ones, are those of the American Mathematical Society (AMS): amsart, amsrpt and amsbook They correspond to the first three standard classes, but have slightly different formatting and additional support for mathematics.
TEX has no native support for graphics and images. This is not
surprising when you consider that TEX was written almost 20 years ago,
when most graphic formats available now hadn't been conceived. Various
drawing formats and packages have been written for TEX and LATEX , and
we already saw some examples on pages
-
.
These packages allow you to produce portable graphics in TEX --they
are written in TEX and produce the same output anywhere.
Figure 13 was produced by LATEX 's picture environment,
extended by the packages epic and eepic.
One can still import graphics; TEX was written with support for
producing \special commands. These commands produce no TEX
output--they only tell TEX the dimensions and alignment of the
object--however they can be read by the DVI driver and used to insert
arbitrary graphics. The most common (and best supported) format
is encapsulated postscript (EPS).
The photograph looks pretty ugly in the previewer, but it prints correctly. Given (say) a GIF file, one can generate the appropriate EPS with:
linux$ giftopnm rob.gif > rob.pnm linux$ pnmtops -noturn rob.pnm > rob.epsand then to put this into your LATEX file you must load the package epsfig and use the LATEX code:
\epsfig{file=rob.eps}