You are on page 1of 6

Programming Programming

with LATEX with LATEX


From
newcommand 1 Characteristics of TEXand friends
From
newcommand
Programming in LATEX
to class files to class files

Paul A. Paul A.
Thompson,
Ph.D.
2 \newcommand Thompson,
Ph.D.

Characteristics Characteristics
of TEXand 3 Example 1 of TEXand • Program: systematic method to solve problem
friends friends • Numerical languages (C, FORTRAN, SAS, R)
\newcommand \newcommand
• Solves problems with numbers or words
Example 1 4 Example 2 Example 1
• Standard method for solution: Expertise
Example 2 Example 2 • Algorithms written in code
Programming Programming
in LATEX
5 Programming in LATEX in LATEX
• Formatting languages like LATEX
Packages in Packages in • Objective: Formatting tasks
LATEX LATEX
6 Packages in LATEX • Systematic approach to formatting task
Package and Package and • Structure vs content
Class Class
Definitions Definitions • Systematic presentation
Documentation 7 Package and Class Definitions Documentation
in LATEX in LATEX

8 Documentation in LATEX

Programming Programming
with LATEX with LATEX
From
newcommand
From
newcommand
Architecture of TEX and friends
to class files to class files

Paul A. Paul A.
Thompson,
Ph.D.
Thompson,
Ph.D.
• The TEX Language
• Separation of structure and content
Characteristics Characteristics
of TEXand of TEXand
• Text viewed as boxes
friends friends • Horizontal mode: Boxes are laid out
\newcommand \newcommand • Vertical mode: rows added successively
Example 1 Example 1 • Horizontal mode easy to work with
Example 2 Example 2 • Vertical mode harder (\parbox, pspicture)
Programming Programming • List-processing or macro language
in LATEX in LATEX
• LATEX:
Packages in Packages in
LATEX LATEX • Componential approach
Package and Package and • Macros are higher-level
Class Class
Definitions Definitions • Actual TEX code: protected/isolated
Documentation Documentation • ConTEXt:
in LATEX in LATEX
• Alternate macro system
• Mainly used in desktop publishing

Programming Programming
with LATEX with LATEX
From
newcommand
Macro-Processing Languages From
newcommand
Creating LATEX Commands
to class files to class files

Paul A. Paul A.
Thompson, • Elements for input Thompson,
Ph.D. Ph.D.
• Primitive
Create or redefine commands
Characteristics • Text Characteristics
of TEXand of TEXand
friends • Comment friends • Create new command
\newcommand • Macro definition \newcommand • \newcommand{cmd}[narg]{command definition}
Example 1 Special command Example 1 • cmd: Contains only letters, single word, back-slash
Example 2
No output produced Example 2 • Must be unique
Programming
Macros contain macros Programming
• narg: 1 ≤ narg ≤ 9
in LATEX • Macro call in LATEX • Optional
Packages in Expansion of macro Packages in • In command definition section, use #1, #2, etc.
LATEX LATEX
Additional macros also expanded • \renewcommand: Redefine existing command
Package and Package and
Class Expansion ends at primitive or text Class • Just \newcommand syntax
Definitions Definitions
• LATEX • Replaces existing commands
Documentation Documentation
in LATEX • Huge collection of macros in LATEX
• Seldom see TEX primitives
• \tracingonline and others

Programming Programming
with LATEX with LATEX
From
newcommand
Uses for \newcommand From
newcommand
Define variable for name
to class files to class files

Paul A. Paul A. This example shows simple macros variables.


Thompson, Thompson,
Ph.D. Ph.D.
\newcommand{\clientfull}{John Smith, Esq}
Characteristics
Macro variables and macro functions are not distinguished in Characteristics \newcommand{\clientfirst}{John}
of TEXand
friends
LATEX. Both are defined by \newcommmand. of TEXand
friends
\newcommand{\clientformal}{Mr. Smith}
\newcommand{\sittest}{good}
\newcommand • Variable \newcommand
\newcommand{\spx}{\hspace*{5pt} \\}
Example 1 • Define name and value Example 1 \newcommand{\restring}{The Johnson Case}
Example 2 • Resolving the macro displays the contents Example 2 \newcommand{\duedate}{next Friday}
Programming
• Program Programming To: \clientfull \\
in LATEX in LATEX From: Paul A. Thompson, Ph.D. \\
Packages in • Slightly enhanced over variable Packages in Re: \restring \spx
LATEX • Multiple arguments LATEX
Dear \clientformal, \spx
Package and • Increasing the arguments over 9 is not feasible Package and
In looking over the materials that you
Class Class
Definitions
• Variable vs Program: Complexity Definitions have sent, \clientfirst, I find that
Documentation Documentation your situation is \sittest. I will
in LATEX in LATEX prepare the assignment for \duedate. \spx
Sincerly yours, \spx
Paul A. Thompson
Programming Programming
with LATEX with LATEX
From
newcommand
Results From
newcommand
Changing the \spx Command
to class files to class files

Paul A. Paul A.
Thompson, Thompson,
Ph.D. To: John Smith, Esq Ph.D.

Characteristics From: Paul A. Thompson, Ph.D. Characteristics


of TEXand
Re: The Johnson Case of TEXand A space command can now be manipulated to get various
friends friends
effects
\newcommand \newcommand

Example 1 Dear Mr. Smith, Example 1


Effect 1:
Example 2 Example 2
\newcommand{\spx}{\\ \hspace*{5pt} \\ \vspace*{10pt}}
Programming In looking over the materials that you have sent, John, I find Programming Effect 2:
in LATEX in LATEX

Packages in
that your situation is good. I will prepare the assignment for Packages in
\newcommand{\spx}{\\ \hspace*{5pt} \\ \vspace*{20pt}}
LATEX next Friday. LATEX
Effect 3:
Package and Package and
Class Class \newcommand{\spx}{\\ \hspace*{5pt} \\ **Snort** \vspace*{5pt}}
Definitions Sincerly yours, Definitions

Documentation Documentation
in LATEX in LATEX
Paul A. Thompson

Programming Programming
with LATEX with LATEX
From
newcommand
Results: Effect 1 From
newcommand
Results: Effect 2
to class files to class files
To: John Smith, Esq
Paul A. To: John Smith, Esq Paul A.
Thompson, Thompson, From: Paul A. Thompson, Ph.D.
Ph.D. From: Paul A. Thompson, Ph.D. Ph.D.
Re: The Johnson Case
Characteristics
Re: The Johnson Case Characteristics
of TEXand of TEXand
friends friends

\newcommand \newcommand
Dear Mr. Smith, Dear Mr. Smith,
Example 1 Example 1

Example 2 Example 2

Programming Programming
in LATEX In looking over the materials that you have sent, John, I find in LATEX

Packages in that your situation is good. I will prepare the assignment for Packages in In looking over the materials that you have sent, John, I find
LATEX LATEX
next Friday. that your situation is good. I will prepare the assignment for
Package and Package and
Class Class next Friday.
Definitions Definitions

Documentation
in LATEX
Sincerly yours, Documentation
in LATEX

Sincerly yours,
Paul A. Thompson

Paul A. Thompson

Programming Programming
with LATEX with LATEX
From
newcommand
Results: Effect 3 From
newcommand
Command for entire letter
to class files to class files

Paul A. Paul A. \newcommand{\laddit}{I thoroughly enjoyed our meeting,


Thompson, To: John Smith, Esq Thompson,
and complement your fine equine impressions.}
Ph.D. Ph.D.
From: Paul A. Thompson, Ph.D. \newcommand{\myname}{Paul A. Thompson, Ph.D.}
Characteristics
of TEXand
Re: The Johnson Case Characteristics
of TEXand
\newcommand{\formlet}[7]{
friends friends \begin{tabular}{ll}
\newcommand \newcommand To: & #1 \\
**Snort** Dear Mr. Smith, From: & \myname \\
Example 1 Example 1
Re: & #4 \\
Example 2 Example 2
\end{tabular}
Programming **Snort** In looking over the materials that you have sent, Programming
in LATEX in LATEX
#7 Dear #2, #7
Packages in
John, I find that your situation is good. I will prepare the Packages in
In looking over the materials that you
LATEX
assignment for next Friday. LATEX have sent, #3, I find that your situation is
Package and Package and #5. I will prepare the assignment for #6.
Class Class \laddit
Definitions Definitions
**Snort** Sincerly yours, #7 Sincerly yours, #7
Documentation Documentation
in LATEX in LATEX
\myname
}
**Snort** Paul A. Thompson \formlet{John Smith, Esq}{John}{Mr. Smith}{good}
{the Johnson Case}{next Friday}{\\ \hspace*{5pt} \\}

Programming Programming
with LATEX with LATEX
From
newcommand
Comments about Macro \formlet From
newcommand
Text as typeset -
to class files to class files Here is a complex text, with 2 errors

Paul A. Paul A.
Thompson, Thompson,    0   −    
0 0 −1 0 0 0 0 −1
Ph.D. Ph.D. Qe = y − XC CC ξ I−X X X X y − XC CC ξ
  − 
Characteristics • Macro uses 7 arguments Characteristics = y
0
I−X X X
0
X
0
y
of TEXand of TEXand
friends • Can’t get too much more complex - 9 in total friends
 
\newcommand • Define macros; pass in by “locally-global” scope rules \newcommand since X = X X0 X − X0 X and X0 = X0 X X0 X − X0 . Furthermore

Example 1 • Scope: Rules which govern availability of macros/variables Example 1  0   − −1  


0 0
• Global: Everywhere Qt − Qe = Cβ̂ − ξ C X X C Cβ̂ − ξ
Example 2 Example 2
• Locally Global: At the defining environment and in all   − 0   − −1   − 
Programming Programming 0 0 0 0 0
= C X X X y−ξ C X X C C X X X y−ξ
in LATEX sub-environments in LATEX
  − 0   − −1   − 
Packages in • Local: In the defining environment only Packages in = y X
0
X X
0 0
C −ξ
0
C X X
0
C
0
C X X
0 0
X y−ξ
LATEX LATEX
• In LAT  0  − 0   −1
Package and EX, macros are locally global Package and = y − XC
0

CC

0 −1
ξ X X X
0
C
0
C X X
0
−
C
0

C X X
0
−
X
0
Class • TEXprimitive \gdef defines global macro Class
   
Definitions Definitions
0 0 −1
× y − XC CC ξ
Documentation
in LATEX
Macros \laddit and \myname are locally global Documentation
in LATEX
since C = AX, for some matrix A, and

  −    
0 0 0 0 −1 0 −1
C X X X XC CC = AXC (CC) =I
Programming Programming
with LATEX with LATEX
From
newcommand
Complex Text-a From
newcommand
Complex Text-b
to class files to class files
Timm, 1975, p. 179:
Paul A. Paul A.
Thompson, Proving independence is more involved since the quadratic forms are not Thompson,
Ph.D. written in terms of the same normally distributed random variables. In Ph.D.
order to establish independence, observe that \left\{\mathbf{y}’\mathbf{X}\left[\left(\mathbf{X}’\mathbf{X}\right)^{-}\right]’
\mathbf{C}’ - \mathbf{\xi}’\right\}
Characteristics \begin{eqnarray*} Characteristics \left[\mathbf{C}\left(\mathbf{X}’\mathbf{X}\right)^{-}\mathbf{C}’\right]^{-1}
of TEXand Q_e & = & of TEXand \left[\mathbf{C}\left(\mathbf{X}’\mathbf{X}\right)^{-}\mathbf{X}’\mathbf{y}
friends \left[\mathbf{y}-\mathbf{X}\mathbf{C}’\left(\mathbf{C}\mathbf{C}’\right)^{-1} friends -
\mathbf{\xi}\right]’ \mathbf{\xi}\right] \\
\newcommand \newcommand
\left[\mathbf{I}- \mathbf{X}\left(\mathbf{X}’\mathbf{X}\right)^{-}\mathbf{X}’\right] & = &
Example 1 \left[\mathbf{y} - Example 1 \left[\mathbf{y} - \mathbf{X}\mathbf{C}’\left(\mathbf{C}\mathbf{C}’\right)^{-1}
\mathbf{X}\mathbf{C}’\left(\mathbf{C}\mathbf{C}’\right)^{-1} \mathbf{\xi}\right]’
Example 2 \mathbf{\xi}\right] \\ & = & Example 2 \mathbf{X}\left[\left(\mathbf{X}’\mathbf{X}\right)^{-}\right]’\mathbf{C}’
\mathbf{y}’\left[\mathbf{I} - \mathbf{X}\left(\mathbf{X}’\mathbf{X} \left[\mathbf{C}\left(\mathbf{X}’\mathbf{X}\right)^{-}\mathbf{C}’\right]^{-1}
Programming \right)^{-}\mathbf{X}’\right]\mathbf{y} Programming \mathbf{C}\left(\mathbf{X}’\mathbf{X}\right)^{-}\mathbf{X}’ \\
in LATEX \end{eqnarray*} in LATEX & & \hspace*{3in} \times
\left[\mathbf{y} - \mathbf{X}\mathbf{C}’\left(\mathbf{C}\mathbf{C}’\right)^{-1}
Packages in since Packages in \mathbf{\xi}\right]
LATEX $\mathbf{X} =\mathbf{X}\left(\mathbf{X}’\mathbf{X}\right)^{-}\mathbf{X}’\mathbf{X}$ and LATEX \end{eqnarray*}
$\mathbf{X}’ =\mathbf{X}’\mathbf{X}\left(\mathbf{X}’\mathbf{X}\right)^{-}\mathbf{X}’$. since $\mathbf{C} = \mathbf{A}\mathbf{X}$, for some matrix $\mathbf{A}$, and
Package and Furthermore Package and \begin{equation*}
Class \begin{eqnarray*} Class \left[\mathbf{C}\left(\mathbf{X}’\mathbf{X}\right)^{-}\mathbf{X}’\right]
Definitions Q_t - Q_e & = & Definitions \left[\mathbf{X}\mathbf{C}’\left(\mathbf{C}\mathbf{C}’\right)^{-1}\right] =
Documentation \left(\mathbf{C}\hat{\mathbf{\beta}} - \mathbf{\xi}\right)’ Documentation \mathbf{A}\mathbf{X}\mathbf{C}’\left(\mathbf{C}\mathbf{C}’\right)^{-1} = \mathbf{I}
in LATEX \left[\mathbf{C}\left(\mathbf{X}’\mathbf{X}\right)^{-}\mathbf{C}’\right]^{-1} in LATEX \end{equation*}
\left(\mathbf{C}\hat{\mathbf{\beta}} - \mathbf{\xi}\right) \\ & = &
\left[\mathbf{C}\left(\mathbf{X}’\mathbf{X}\right)^{-}\mathbf{X}’\mathbf{y} -
\mathbf{\xi}\right]’
\left[\mathbf{C}\left(\mathbf{X}’\mathbf{X}\right)^{-}\mathbf{C}’\right]^{-1}
\left[\mathbf{C}\left(\mathbf{X}’\mathbf{X}\right)^{-}\mathbf{X}’\mathbf{y} -
\mathbf{\xi}\right] \\ & = &

Programming Programming
with LATEX with LATEX
From
newcommand
Text as typeset From
newcommand
Observations
to class files Proving independence is more involved since the quadratic forms are not written in terms of the same to class files
normally distributed random variables. In order to establish independence, observe that
Paul A. Paul A.
Thompson, Thompson,
Ph.D.    0   −     Ph.D.
0 −1 0 −1
Qe = y − XC
0
CC ξ I−X X X
0
X
0
y − XC
0
CC ξ Notes:
Characteristics   −  Characteristics
of TEXand = y
0
I−X X X
0
X
0
y of TEXand • Timm (1975) is a great text, but very dense
friends friends • Many repetitive forms
\newcommand − − \newcommand
since X = X X0 X X0 X and X0 = X0 X X0 X X0 . Furthermore • Quadratic form CX −1 C ‘
Example 1
 0   − −1  
Example 1
• Projection X (X ‘X )−1 X ‘
0 0
Example 2 Qt − Qe = Cβ̂ − ξ C X X C Cβ̂ − ξ Example 2 • Linear model X β
  − 0   − −1   − 
Programming
= C X X
0 0
X y−ξ C X X
0
C
0
C X X
0 0
X y−ξ
Programming • Residual computation y − X β
in LATEX in LATEX

Packages in   − 0   − −1   −  Packages in


• Much repetitive coding
0 0 0 0 0 0 0 0
LATEX = y X X X C −ξ C X X C C X X X y−ξ LATEX
• \mathbf{C}
  −1 0  − 0   − −1  −
Package and 0 0 0 0 0 0 0 0 Package and • Inverse and regular inverse markings
Class = y − XC CC ξ X X X C C X X C C X X X Class
Definitions     Definitions
× y − XC
0
CC
0 −1
ξ
• Density of code masks regularity
Documentation Documentation
in LATEX in LATEX • Difficult to spot errors
since C = AX, for some matrix A, and
  −      
0 0 0 0 −1 0 0 −1
C X X X XC CC = AXC CC =I

Programming Programming
with LATEX with LATEX
From
newcommand
Approach 1 From
newcommand
Approach 1: Good idea?
to class files to class files

Paul A. Paul A.
Thompson, Thompson,
Ph.D.
Reduce typing by replacing \mathbf{X} with \zbX: Ph.D.
Approach 1 has advantages and disadvantages
Characteristics \newcommand{\zbX}{\mathbf{X}} Characteristics
of TEXand \newcommand{\zby}{\mathbf{y}} of TEXand • Advantages:
friends \newcommand{\zbC}{\mathbf{C}} friends
\newcommand{\zbA}{\mathbf{A}} • Simplification
\newcommand \newcommand{\zbxi}{\mathbf{\xi}} \newcommand
\newcommand{\zbbeta}{\mathbf{\beta}} • Much less typing
Example 1 Example 1
\newcommand{\zbI}{\mathbf{I}} • Lines are shorter and easier to read
Example 2 Example 2
\begin{eqnarray*}
Q_e & = &
• Disadvanges:
Programming Programming
in LATEX \left[\zby - \zbX\zbC’\left(\zbC\zbC’\right)^{-1}\zbxi\right]’
\left[\zbI - \zbX’\left(\zbX’\zbX\right)^{-}\zbX’\right]
in LATEX • Complexity of the structure remains
Packages in \left[\zby - \zbX\zbC’\left(\zbC\zbC’\right)^{-1}\zbxi\right] \\ Packages in • Need as many newly defined symbols as real symbols
LATEX & = & LATEX
\zby’\left[\zbI - \zbX\left(\zbX’\zbX\right)^{-}\zbX’\right]\zby • Sometimes editors (xemacs using ess) get confused by
Package and \end{eqnarray*} Package and
Class Class redefinitions
Definitions since Definitions • Redefinition of \chapterz is a bad idea
$\zbX =\zbX\left(\zbX’\zbX\right)^{-}\zbX’\zbX$ and
Documentation $\zbX’ =\zbX’\zbX\left(\zbX’\zbX\right)^{-}\zbX’$. Documentation
in LATEX Furthermore in LATEX Usually not much gain by this approach

Programming Programming
with LATEX with LATEX
From
newcommand
Approach 2 From
newcommand
Decision-making in LATEX
to class files to class files

Paul A. Define macros for quadratic forms, projections, and other Paul A.
• LATEX command \ifthenelse provides decision-making
Thompson, Thompson,
Ph.D. structures in the text Ph.D. ability
Characteristics
\newboolean{testva} \newboolean{testvba} \newboolean{testvbb}
Characteristics • Style: ifthen
\newcommand*{\qaform}[4]{
of TEXand
\left[#1\right]’\left[#2\right]
of TEXand • Syntax:
friends friends
\ifthenelse{\boolean{testva}}{^{-1}}{}\left[#3\right]}
\newcommand*{\qbform}[7]{
\ifthenelse{if-test}{true-action}{false-action}
\newcommand \newcommand
\left[#1\right]’ #2 \left[#3\right]
Example 1 \ifthenelse{\boolean{testvba}}{^{-1}}{} #4 Example 1
\ifthenelse{\boolean{testvbb}}{\\ & & \hspace{2.5in} \times}{}\left[#5\right]} • test: Test of some sort - value equals something, length
Example 2 \newcommand*{\forma}[2]{#1\left(#2’#2\right)^{-}#1’} Example 2
\newcommand*{\formb}[3]{#1\left(#2’#2\right)^{-}#3’}
¿ some amount, boolean
Programming \newcommand*{\formc}[3]{#1’\left(#2’#2\right)^{-1}#3} Programming • true-action: Text issued for true
in LATEX \newcommand*{\formd}[3]{#1#2’\left(#3#3’\right)^{-1}} in LATEX
• false-action: Text issued for false
Packages in Packages in
LATEX
\begin{eqnarray*}
\setboolean{testva}{true}
LATEX • LATEX command \newboolean defines decision-making
Package and Q_e & = & \qaform{\zby - \zbX\formc{\zbC}{\zbC}{\zbxi}}
{\zbI - \forma{\zbX}{\zbX}}
Package and boolean
Class Class
Definitions {\zby - \zbX\formc{\zbC}{\zbC}{\zbxi}}{0} \\ Definitions • Style: ifthen
\setboolean{testv}{falsee}
Documentation & = & \qaform{\zby} Documentation • Syntax: \newboolean{name}
in LATEX {\zbI - \forma{\zbX}{\zbX}} in LATEX
{\zby}{0} • Set values: \setboolean{true}, \setboolean{true}
\end{eqnarray*} • Use in test:
since \ifthenelse{\boolean{bool-name}}{t-a}{f-a}
Programming Programming
with LATEX with LATEX
From
newcommand
Approach 2: Good idea? From
newcommand
Approach 3-a
to class files to class files
Define macros for quadratic forms, projections, and other
Paul A. Paul A.
Thompson, Thompson, structures in the text. Embed \mathbf in macro definition.
Ph.D. Ph.D.
\newboolean{testvaa} \newboolean{testvba} \newboolean{testvbb}

Characteristics Characteristics \newcommand{\qaform}[3]{\left[\mathbf{#1}\right]’


of TEXand of TEXand
friends Approach 2 advantages and disadvantages friends
\left[\mathbf{#2}\right]
\ifthenelse{\boolean{testvaa}}{^{-1}}{}
\newcommand • Advantages: \newcommand
\left[\mathbf{#3}\right]}
\newcommand{\qbform}[5]{\left[\mathbf{#1}\right]’ \mathbf{#2}
\left[\mathbf{#3}\right] \ifthenelse{\boolean{testvba}}{^{-1}}{}
Example 1 • Clarification of structure Example 1
\mathbf{#4} \ifthenelse{\boolean{testvbb}}{\\ & & \hspace{2.5in} \times}{}
Example 2 • Simplification Example 2 \left[\mathbf{#5}\right]}

Programming • Lines are shorter and easier to read Programming \newcommand{\resform}[3]{\mathbf{#1} - \mathbf{#2}#3}
in LATEX in LATEX
• Disadvanges: \newcommand{\forma}[2]{\mathbf{#1}\left(\mathbf{#2}’\mathbf{#2}\right)^{-}\mathbf{#1}’}
Packages in Packages in \newcommand{\formb}[3]{\mathbf{#1}\left(\mathbf{#2}’\mathbf{#2}\right)^{-}\mathbf{#3}’}
LATEX • Need as many newly defined symbols as real symbols LATEX \newcommand{\formc}[3]{\mathbf{#1}’\left(\mathbf{#2}’\mathbf{#2}\right)^{-1}\mathbf{#3}}
\newcommand{\formd}[3]{\mathbf{#1}\mathbf{#2}’\left(\mathbf{#3}\mathbf{#3}’\right)^{-1}}
Package and • Final result not much easier to read Package and
Class Class \begin{eqnarray*}
Definitions Definitions \setboolean{testvaa}{false}
Q_e & = & \qaform{\resform{y}{X}{\formc{C}{C}{\xi}}}
Documentation Documentation
{\resform{I}{}{\forma{X}{X}}}
in LATEX in LATEX
{\resform{y}{X}{\formc{C}{C}{\xi}}} \\
\setboolean{testvaa}{false}
& = & \qaform{y}
{\resform{I}{}{\forma{X}{X}}}
{y}
\end{eqnarray*}

Programming Programming
with LATEX with LATEX
From
newcommand
Approach 3-b From
newcommand
Approach 3: Good idea?
to class files to class files

Paul A. $\mathbf{X} = \forma{X}{X}\mathbf{X}$ and $\mathbf{X}’ =\mathbf{X}’\forma{X}{X}$. Paul A.


Thompson, Furthermore Thompson,
Ph.D. \begin{eqnarray*} Ph.D.
\setboolean{testvaa}{true}
Characteristics Q_t - Q_e & = & \qaform{C\hat{\beta} - \xi} Characteristics
of TEXand {\forma{C}{X}} of TEXand
friends {C\hat{\beta} - \xi} \\ friends Approach 3 advantages and disadvantages
\setboolean{testvaa}{true}
\newcommand & = & \qaform{\formb{C}{X}{X}y - \xi} \newcommand • Advantages:
{\forma{C}{X}}
Example 1 {\formb{C}{X}{X}y - \xi} \\ Example 1 • Clarification of structure
\setboolean{testvaa}{true}
Example 2
& = & \qaform{y’\formb{X}{X}{C} - \xi’}
Example 2 • Simplification
Programming {\forma{C}{X}} Programming • Bold-face specification moved to macro command
in LATEX {\formb{C}{X}{X}y - \xi} \\ in LATEX
\setboolean{testvba}{true} \setboolean{testvbb}{true} • Disadvanges:
Packages in & = & \qbform{\resform{y}{X}{\formc{C}{C}{\xi}}} Packages in
LATEX {\formb{X}{X}{C}} LATEX • Somewhat difficult to read
{\forma{C}{X}}
Package and {\formb{C}{X}{X}} Package and • 2 other structures should be written
Class {\resform{y}{X}{\formb{C}{C}{\xi}}} Class
Definitions \end{eqnarray*} Definitions
since $\mathbf{C} = \mathbf{A}\mathbf{X}$, for some matrix $\mathbf{A}$, and
Documentation \begin{equation*} Documentation
in LATEX \formb{C}{X}{X} in LATEX
\formd{X}{C}{C} =
\mathbf{A}\formd{X}{C}{C} = \mathbf{I}
\end{equation*}

Programming Programming
with LATEX with LATEX
From
newcommand
LATEX Programming Challenges From
newcommand
Managing Counters in LATEX
to class files to class files

Paul A. Paul A.
Thompson,
Ph.D.
Thompson,
Ph.D.
A counter is an integer variable

Characteristics Characteristics
• \newcounter{ctr}: New counter
of TEXand Approach 3 advantages and disadvantages of TEXand • Example: \newcounter{attempt}: New counter
friends friends
attempt
\newcommand • Advantages: \newcommand
• \setcounter{ctr}{val}: Set counter value
Example 1 • Clarification of structure Example 1
• \addtocounter{ctr}{val}: Increment counter
Example 2 • Simplification Example 2
• \the<ctr>: Display counter value
Programming • Bold-face specification moved to macro command Programming
in LATEX in LATEX • \alph{ctr}: Display counter value as alpha
• Disadvanges: • \whiledo{test}{do-clause}: Repeat processing
Packages in Packages in
LATEX • Somewhat difficult to read LATEX
• Counters are used in environments
Package and • 2 other structures should be written Package and
Class Class • figure, table
Definitions Definitions
• section, subsection
Documentation Documentation
in LATEX in LATEX
• enumerate
• Most math in LATEX is integer level

Programming Programming
with LATEX with LATEX
From
newcommand
Managing lengths in LATEX From
newcommand
Managing Boxes in LATEX
to class files to class files

Paul A. Paul A.
Thompson, Thompson,
Ph.D.
A length is a dimensional value Ph.D.

Characteristics Characteristics
of TEXand • Textual objects have heights, widths, and depths of TEXand A box is a container for text
friends friends
• Space have widths
\newcommand \newcommand • Boxes can store text after typesetting
• Boxes have heights, widths, depths
Example 1
• \newlength{cmd}: New length variable Example 1 • Boxes can store figures
Example 2
• \setlength{cmd}{val}: Set length value
Example 2 • Boxes can be measured for placement
Programming
in LATEX • \addtolength{cmd}{val}: Increment length
Programming
in LATEX
• Boxes can be displayed as normal text
Packages in • \settowidth{cmd}{text}: Set cmd to width of text Packages in
• \newsavebox{box}: New box
LATEX
• \lengthtest{test}: Test of length - allows different
LATEX • \sbox{box}{text}: Set counter value
Package and Package and • \usebox{box}: Use contents
Class dimensions Class
Definitions Definitions
• Many formatting problems require measuring objects and
Documentation Documentation
in LATEX checking them in LATEX
Programming Programming
with LATEX with LATEX
From
newcommand
What’s available and what’s not From
newcommand
Programming in a package or class
to class files to class files

Paul A. Paul A. We now can create a package file


Thompson, Thompson,
Ph.D. Ph.D.
• Programming here is very different than many situations
Characteristics LATEX tools: hits and misses Characteristics • You must do all the work BEFORE you see any of the
of TEXand of TEXand
friends • Tool hits: friends data
\newcommand \newcommand • Package is loaded at the start of the document
• Counters - increment, display, test
Example 1 Example 1 • Text comes after the package
• Text objects: Measuring, storing, displaying
Example 2 Example 2
• Alterations and modifications done at the beginning
• Tool misses:
Programming Programming • Code in package/class
in LATEX • Arrays and other multiple objects in LATEX
• Signal- or event-driven
Packages in • Most or all math functions Packages in
LATEX LATEX • Each event is treated separately
• Character-string functions (scan, substr, index, verify,
Package and Package and
• Memory processes are available but not convenient
Class
reverse) Class
Definitions • Sophisticated do loops Definitions
• Memory for the system: aux file
Documentation Documentation • LATEX is a multi-pass system
in LATEX in LATEX
• Pass 1: Determine event which needs memory process
• Write information to aux file
• Read aux file at start of processing

Programming Programming
with LATEX with LATEX
From
newcommand
Converting macros into a package From
newcommand
Adding the package to your
to class files to class files

Paul A. Paul A.
installation
Thompson, Thompson,
Ph.D.
We now can create a package file Ph.D.
The package must now be added to the installation. This is
installation-dependent.
Characteristics • Package name: quadform.sty Characteristics
of TEXand
friends
• Setting up the package of TEXand
friends
• Installations have local trees and project trees
\newcommand • Move all macro definitions into package \newcommand • New packages are added to the local tree
Example 1
• Add comments Example 1 • Location:
Example 2
• Add additional options Example 2 local-root/tex/latex/your-package/package.sty
• Make additional structural components available • Select Package Manager → Refresh Filename Database
Programming Programming
in LATEX • Advantages of the package in LATEX
• In MiKTEX, Start→Programs→Miktex 2.6→Settings
Packages in Packages in
LATEX
• Easily used elsewhere LATEX • Refresh FNDB
Package and
• Set defaults for booleans Package and • Test addition by creating a LATEXfile using the package
Class • Documentation Class
• Create the file mytextest.tex
Definitions Definitions
• Much more systematic treatment • Add line \usepackage{your-package}
Documentation Documentation
in LATEX • Hide messy stuff in LATEX • Open a command window
• Ensure that other packages are included • Process file: latex mytextest
• Examine output statements in window or log
• You should see your location as the file location

Programming Programming
with LATEX with LATEX
From
newcommand
The Structure of Classes/Packages From
newcommand
Identification Section
to class files to class files

Paul A. Paul A.
Thompson, Thompson,
Ph.D. Ph.D.

Characteristics
Classes and packages have a structure Characteristics These options enable the package to be recognized and have a
of TEXand of TEXand
friends • Identification: information about the package/class friends version set up correctly.
\newcommand • Initial code: Code needed by the remaining sections (ADV) \newcommand • Name of package/class
Example 1 • Declaration of options: Options show up in package calls Example 1
• \ProvidesPackage{name}[release info]
Example 2 Example 2
(ADV) • Name: Name of package
Programming Programming • Release info: (yyyy/mm/dd version info-about)
in LATEX
• Execution of options: These options are now executed in LATEX

Packages in (ADV) Packages in


• TEX Format
LATEX LATEX
• Package loading: Other packages which are needed are • \NeedsTeXFormat{name}[release info]
Package and Package and
Class brought in Class
• Ensures that correct LATEX system is used
Definitions
• Main code: The actual package/class code
Definitions • Most important for distibution of package
Documentation Documentation
in LATEX in LATEX

Programming Programming
with LATEX with LATEX
From
newcommand
Package Loading From
newcommand
Documentation in the TEX System
to class files to class files

Paul A. Paul A.
Thompson, Thompson,
Ph.D. Ph.D.
• The TEX philosophy
Characteristics Characteristics
of TEXand of TEXand • Documentation and code are in same file
friends • Name of package/class friends
• Code modifications can be easily documented
\newcommand • \RequirePackage{package}[release info] \newcommand
• Both are well-synchronized
Example 1 • Name: Name of package Example 1
• Functional package files: docstrip
Example 2 • Release info: (yyyy/mm/dd version info-about) Example 2
• Functional documentation files: doc
Programming • Package is loaded iff not already loaded Programming
in LATEX in LATEX
• Better and more functional documentation systems are
• \RequirePackageWithOptions{package}[release being developed
Packages in Packages in
LATEX info] LATEX • Documentation process
Package and • Package to be loaded with specific options Package and
Class Class
• Write documentation
Definitions Definitions • Finalize code
Documentation Documentation • Weave together with special commands
in LATEX in LATEX
Programming Programming
with LATEX with LATEX
From
newcommand
Documentation of packages From
newcommand
Conclusions
to class files to class files

Paul A. Paul A.
Thompson, Thompson,
Ph.D. Ph.D.

Characteristics Characteristics
of TEXand • Defining the quadform documentation of TEXand • Macros systematize the production of text
friends friends
• Describe each macro • Reduce errors
\newcommand \newcommand
• Describe special conditions • Cut down on repetitive typing
Example 1 Example 1
• Define the .sty file with latex file.ins • Concentrate on content, central definition of form
Example 2 Example 2
• Commands in here indicate processing to produce .sty file • Packages can help you with macros
Programming Programming
in LATEX • Multiple .sty files may be produced from one .dtx in LATEX • Create documentation
Packages in • Define documentation with latex file.dtx Packages in • Separate content (files with text) from structure (package
LATEX LATEX
• Each process reads file in different way files)
Package and Package and
Class • Two products Class • LATEX tools are not extensive
Definitions Definitions

Documentation Documentation
in LATEX in LATEX

You might also like