Extending LATEX with Packages

Simon Law

23 March 2004

Using Packages

Extend LATEX

New commands, environments, behaviour

Standard
\usepackage{fullpage}

Options
\usepackage[margin=0.5in]{geometry}

Finding Packages

Many packages are standard

Newer packages are found on CTAN
http://www.ctan.org/

Google Is Your Friend
http://www.google.ca

Installing Packages

Get your system administrator to do it

Install locally
$ export TEXINPUTS=~/texmf/:
% setenv TEXINPUTS ~/texmf/:

Install in the same directory

Refresher

% This is a comment.
\documentclass{article}
\begin{document}
\section{Refresher}
This is a document typeset in \LaTeX{}where \emph{I} will justify my raison
d'\^{e}tre.
\end{document}

New Commands

Save a command
\let\textbforig\textbf

Create a new command
\newcommand{red}[1]{\textcolor{red}{#1}}

Replace a command
\renewcommand{red}[1]{\colorbox{red}{#1}}

Create a command if one doesn't exist
\providecommand{blue}[1]{%
\textcolor{blue}{#1}}

New Environments

Create a new environment
\newenvironment{figure}
{\begin{figureorig}[!htbp]}
{\end{figureorig}}

Replace an existing environment
\renewenvironment{figure}[1][!htbp]
{\begin{figureorig}[#1]}
{\end{figureorig}}

Environments are actually commands!
\figure and \endfigure

Lengths

TEX thinks about lengths separately
\newlength{boxwidth}

Determining length
\setlength{boxwidth}{5em}
\settowidth{boxwidth}{abc}
\settoheight{boxwidth}{abc}

Mathematics
\addtolength{boxwidth}{-1ex}

Using Lengths

Use them anywhere lengths are required
\rule{0.3ex}{\boxwidth}

Default lengths
\rule{0.3ex}{0.7\textwidth}
\rule{0.3ex}{0.7\columnwidth}
\rule{0.8\textheight}{0.3ex}

Boxes

Basic building block for TEX

Single-line box (LR mode)
\makebox[3cm]{Hello}
\mbox{Goodbye}

Framed box
\framebox[3cm]{Hello}
\fbox{Goodbye}

Paragraph box
\parbox{\columnwidth}{The\\TEXbook}

Math Mode

Some things must be in Math mode
$\beta$ is β

Doesn't work inside Math mode
\[ $\beta$ = m/n \]

But this does
\[ \ensuremath{\beta} = m/n \]

For text too!
You must measure \ensuremath{\beta}.

ifthen Package

Useful wrapper around conditionals
\ifthenelse{test}{then}{else}

test can include

Boolean: \AND \OR \NOT

Infix numerical relations: 1<2 2=2 2>1

String test: \equal{Simon}{Simon}

Length: \lengthtest{1<2}

Misc: \isodd{1} \isundefined{\mycommand}

Classes

Used to determine document type

Standard classes are:

article

book

letter

report

slides

Global commands should be in packages

Packages

Apply to most classes

Loaded by \usepackage{name}

Saved as name.sty

Headers

Used to identify package
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{simon}%
[2004/03/23 Simon's Package]
\RequirePackage{calc}
\RequirePackage{ifthen}[1994/06/01]

@

Commands are actually macros

Global namespace

Need reserved symbol: @
\let\emph@rig\emph

In normal documents:
\makeatletter
It \emph@rig{is} mine!
\makeatother

Reporting Errors

Three different error levels:
\PackageError{simon}
{Can't frob the foo.}
{foo is probably set incorrectly.}
\PackageWarning{simon}{Lookout for bar!}
\PackageInfo{simon}{I am happy.}

Throw errors when things are wrong.
\newcommand{\address}[1]{%
\renewcommand{\@address}{#1}}
\newcommand{\@address}{%
\ClassError{uw-wkrpt}{%
No \noexpand\address given}{}}

Example

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{homework}[2004/03/23 Simon's Homework]
\newcommand{\ud}{\ensuremath\,\mathrm{d}}
\newcommand{\uD}{\ensuremath\mathrm{D}}
\newcommand{\deriv}[3][]{%
\ensuremath\frac{\!\ud^{#1}#2}{\!\ud#3^{#1}}}
\newcommand{\fr}[2]{\ensuremath{\mbox{}^{#1}\!\!/\!_{#2}}}
\newcommand{\note}[1]{\rule{0pt}{2em}\textrm{#1}}
\newcommand{\course}[1]{\newcommand{\@course}{#1}}
\newcommand{\assignment}[1]{\newcommand{\@assignment}{#1}}
\newcommand{\class}[1]{\newcommand{\@class}{#1}}
\author{Simon Law}
\class{ComESutra}
\renewcommand{\maketitle}{%
\noindent%
\begin{tabular*}{\textwidth}{%
l@{\extracolsep{\fill}}c@{\extracolsep{\fill}}r}%
\textbf{\@course}
& \textbf{Assignment \#\@assignment}
& \textbf{\@date}\\%
\@class & & \@author%
\end{tabular*}%
}

Resources

The Not So Short Introduction to LATEX 2ε
www.ctan.org/tex-archive/info/ lshort/english/lshort.pdf

LATEX 2ε for class and package writers
http://www.ctan.org/tex-archive/macros/latex/doc/clsguide.pdf

UK List of TeX Frequently Asked Questions
http://faq.tug.org

Source code:
/usr/share/texmf/tex/latex/base/latex.ltx

Slides
http://www.law.yi.org/~sfllaw/talks/

2004-03-23T10:16:21