next up previous
Next: References Up: textalk3 Previous: TEX ing, Previewing and

Subsections

Further LATEX


Defining your own commands

In LATEX it is easy to build up your own commands. Simply put

\newcommand{\mycmd}{definition}
where definition is any LATEX input. Suppose you want to write intoxication, typeset as
\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.

Figure: Using $ \backslash$newcommand in text
\begin{figure}
\centering \begin{tabular}{p{3in}cp{3in}}
We shall consider the...
...t of }\verb*!\intox{}!\texttt{
in modern society.}
\end{tabular} \end{figure}

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 $ a$ and $ b$ as $ \langle a,b \rangle$ which is typeset as

$\langle a,b \rangle$
If you use \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.

Figure: Using $ \backslash$newcommand in math mode
\begin{figure}
\centering \begin{tabular}{p{3in}cp{3in}}
The inner product \en...
...line \$}\verb*!\!\texttt{ab=a\_1b\_1 +
a\_2b\_2\$.}
\end{tabular} \end{figure}

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 $ (a,b)$, 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 $ \langle x_i,y_j \rangle$.

If your command name \mycmd in

\newcommand{\mycmd}{definition}
has already been defined then you will get an error when you process the file. For example
\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 `\l ' while $\ell$ gives `$ \ell$'. However some commands may be low-level ones which would be very bad to redefine, for example \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.

Summary: Using $ \backslash$newcommand


LATEX document classes

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,

\documentclass[options]{class}
I used \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.


Importing Graphics

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.

Figure 13: A ``portable'' picture in LATEX
\begin{figure}
\centering\par\unitlength = 1mm
\begin{picture}(50,70)(0,0)
\put(...
...615793167,57.6132115816913)
\drawline(25,55)(0,55)
\end{picture}\par\end{figure}

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

Figure 14: Rob at the Grand Canyon
\begin{figure}
\centering %\psdraft
\epsfig {file=rob-gc2.eps} \end{figure}

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.eps
and then to put this into your LATEX file you must load the package epsfig and use the LATEX code:
\epsfig{file=rob.eps}

next up previous
Next: References Up: textalk3 Previous: TEX ing, Previewing and
Robert Judd
2000-12-10