Programming LaTeX


Nelson Elhage























% minimal.cls
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{minimal}[1995/10/30 Standard LaTeX minimal class]
% Also \ProvidesStyle
\renewcommand{\normalsize}{\fontsize{10pt}{12pt}\selectfont}
\setlength{\textwidth}{6.5in}
\setlength{\textheight}{8in}






















% A [slightly] more realistic example
\NeedsTexFormat{LaTeX2e}
\ProvidesClass{example}[2009/01/22 nelhage's Example LaTeX class]
\LoadClass{article}
\RequirePackage{fancyhdr}

\DeclareOption{simple}{\pagestyle{empty}}
\DeclareOption{twocolumn}{\OptionNotUsed}
\DeclareOption*{
  \PassOptionToClass{\CurrentOption}{article}
}

\ProcessOptions\relax


% \documentclass[simple]{example}














\AtBeginDocument{
  % Not allowed to typeset anything!
  \title{My Document Title}
  \author{\@author \\ Foo Corporation}
}
\AtEndDocument{
  \vfill Some footer text
}





















\ClassError{example}{Something has gone horribly wrong!}{This class
  has encountered a terrible error!  \MessageBreak Consider using the
  \protect\fewerhorriblerrors\space command. \MessageBreak The
  document title is \@title}
% LaTeX appends a . for you.

% \ClassWarning { class-name } { warning-text }
% \PackageWarning { package-name } { warning-text }
% \ClassWarningNoLine { class-name } { warning-text }
% \PackageWarningNoLine { package-name } { warning-text }
% \ClassInfo { class-name } { info-text }
% \PackageInfo { package-name } { info-text }
















\newcommand{\water}{H$_2$O}
\newcommand{\hi}[1]{Hello, #1}

\renewcommand{\hi}[1]{Salutations, #1}
\providecommand{\hi}[1]{Hello, #1}

{
  \renewcommand{\hi}[1]{Good Morning, #1}
  \hi{Nelson}
}

% Uses the old \hi
\hi{Nelson}
















\def\hi#1{Hello, #1}




























\newenvironment{proclamation}[1]
{\begin{centering}
    \large And so proclaimed #1:
\end{centering}}
{}
% Analogous \renewenvironment{}

\begin{proclamation}[Nelson]
  Some text
\end{proclamation}




















\newcommand{\hellotwo}[2]{
  {
    \newcommand{\hi}[1]{Hello, ##1}
    \hi{#1} \\
    \hi{#2}
  }
}
\hellotwo{Nelson}{Geoff}


















% Introducing \csname ... \endcsname
\newcommand{\hi}{Hello, World}
\newcommand{\althi}{Greetings!}

\newcommand{\thecommand}{hi}
\csname\thecommand\endcsname            % --> Hello, World
\csname alt\thecommand \endcsname       % --> Greetings!























% Commands the define other commands
\newcommand{\alias}[2]{
  \expandafter\def\csname#1\endcsname{\csname#2\endcsname}%
}

\newcommand{\alias}[2]{
  \def\csname#1\endcsname{\csname#2\endcsname}
}

\alias{hi}{hello}

\def\csname hi\endcsname{\csname hello\endcsname}

\def\hi{\csname hello\endcsname}

\def\hello{Hello, World}
\alias{hi}{hello}

\hi  % --> Hello, World






















\newcommand\newtable[1]{
  \expandafter\newcommand%
    \csname#1\endcsname[1]%
      {\csname table@#1@##1\endcsname}
  \expandafter\newcommand%
    \csname set#1\endcsname[2]%
    {\expandafter\def\csname table@#1@##1\endcsname{##2}}
}

\newtable{user}
\setuser{nelhage}{Nelson Elhage}
\setuser{geofft}{Geoff Thomas}
\texttt{nelhage} is \user{nelhage}.

















\newcounter{acounter}
\setcounter{acounter}{10}
My counter is \arabic{acounter}.
\addtocounter{acounter}{1}
\newcounter{othercounter}
\setcounter{othercounter}{5}
\addtocounter{acounter}{\value{othercounter}}
My counter is now \arabic{acounter}.
% See also the 'calc' package




















% TeX Conditionals
%\ifFOO <then text> [\else <else text>] \fi
\def\cmd{}
\ifx\cmd\empty
  $\backslash$cmd is empty!
\else
  $\backslash$cmd is not empty
\fi





















% ifnum
\ifnum 4 > 8 \then
 Math is broken
\fi

\ifnum \value{page} = 3 \then
Page three!
\fi

% < = > are the only operators

% ifmmode

\ifmmode \mbox{In math mode} \else Not in math mode \fi
\ensuremath{...}

% Others that are less useful




















Flags:

\newif\ifflag

\flagtrue
\flagfalse

\ifflag
 true text
\else
 false test
\fi

\iftrue
\iffalse


























LaTeX has an “ifthen” package
Supposed to define better syntax
Harder to debug when it breaks

























Category Codes

0      Escape character        \
1      Beginning of Group      {
2      End of Group            }
3      Math shift              $                                    %$
4      Alignment tab           &
5      End of line             <return>
6      Parameter               #
7      Superscript             ^
8      Subscript               _
9      Ignored character
10     Space                   SPC
11     Letter                  A-Z and a-z
12     Other Character         all others
13     Active Character        ~
14     Comment Character       %
15     Invalid character       <delete>











\catcode`\<CHAR>=NUM
Changes a character's category code



























By default, @ is "other"
LaTeX provides
\makeatletter
and
\makeatother

\makeatletter
\newcommand{\test}{The author is \@author}
\makeatother
\test
-> The author is \@₁₁author
-> The author is Nelson Elhage























\makeatletter
\def\@subscript{_}
\def\@underscore{\_}
\catcode`\_=13
\def_{\ifmmode\@subscript\else\@underscore\fi}
\makeatother


\texttt{SYS_open}
$x_j$














Any questions at this point?



Reference:

