# Programming in LaTeX

Latex is turing complete, but it’s syntax can make it hard to really program. What’s especially confusing, is that oftentimes keywords are not followed by a space, but rather directly by a string. For example if you have the boolean variable foo, you can set it to true using the command footrue, which is a very weird syntax compared to foo = true what we’re used to… However, if you really want to, you can write if’s and loops and do all kinds of stuff… Following is a short tutorial on how to do so.

## Booleans and If / Else Statements in Latex

One of the most fundamental parts of any programming language is arguably the boolean data type and it’s logic counterpart the if/else statement. Booleans are part of the latex core, as well as of packages such as the ifthen package.

### Booleans and If / Else Without a Package (Option 1)

Booleans can be used in Latex without the use of any packages. However, the syntax is quite cumbersome.

Declaration:

\newif\iffoo %Declaration

Assignment:

\footrue %Sets foo to true
\foofalse %Sets foo to false

Check:

\iffoo
...
\else % else is optional, you can leave it out
...
\fi

Usage example:

\documentclass{article}

\begin{document}
\newif\iffoo %Declaration, defaults to false

\iffoo
text 1
\fi

\footrue % Set foo to true...

\iffoo
text 2
\else
text 3
\fi
\end{document}

Output:

### Booleans with the Ifthen Package (Option 2)

When someone is not comfortable with the syntax of the latex core for booleans, he or she might consider to use the ifthen package.

Requirements:

\usepackage{ifthen}

Declaration & Assignment:

\newboolean{boolvar} %Declaration, defaults to false
\setboolean{boolvar}{false} %Assignment

Check:

\boolean{boolvar}

Usage Example:

\documentclass{article}
\usepackage{ifthen}
\begin{document}
\newboolean{boolvar} % Declaration, defaults to false
\ifthenelse{\boolean{boolvar}}{text1}{text2} % If/else with boolean
\setboolean{boolvar}{true} % Assignment
\ifthenelse{\boolean{boolvar}}{text3}{text4} % If/else with boolean
\end{document}

Output:

## Integers and Loops in Latex

While integers and loops are an integral part of any programming language, it only occurs to few people to use those concepts in latex outside of the enumeration or itemize environments. Nonetheless it can be done, and here is how.

### Integers / Counters

Right next after booleans, or even before them, one usually learns about the data type integers. Latex again has a very peculiar syntax for handling them.

Declaration:

\newcounter{mycounter} %initialized with 0

Assignment:

\setcounter{mycounter}{2} %sets counter to 2

Increment:

\stepcounter{mycounter} % increases mycounter by 1

Get value for programming (e.g. in ifnum condition, see below):

\value{mycounter}

Get value for text output in document:

\themycounter

Adding a number to a counter:

\addtocounter{mycounter}{4}

Adding a counter to a counter:

\addtocounter{mycounter}{\value{myothercounter}}

To compare two counters in an if-clause, the \ifnum-keyword is used:

\ifnum\value{mycounter}>3
Cool beans! % this is only printed if mycounter is larger than 3
\fi

Note: There are also counters in TeX (as opposed to LaTeX), so you might encounter code like \newcount\mycounter elsewhere. The difference between \newcount (TeX) and \newcounter (LaTeX) is minuscule, so make sure you don’t get confused since e.g. calling \value{mycounter} on a counter initialized by \newcount\mycounter will throw an error!

Finally, here’s a complete usage example:

\documentclass{article}
\begin{document}
\newcounter{mycounter} %declare counter, initialized to 0
\themycounter %prints 0
\setcounter{mycounter}{4} %mycounter is now 4
\themycounter %prints 4
\themycounter %prints 8
\themycounter %prints 6
\themycounter %prints 12
\stepcounter{mycounter} % increments mycounter by one
\themycounter %prints 13

\ifnum\value{mycounter}>11
Well yes, \themycounter\ is larger than 11...
\else
...thus this is neverprinted
\fi

\roman{mycounter} %prints mycounter in roman numbers
\end{document}

With it’s glorious output:

Next, we’ll cover how loops can be implemented in LaTeX.

### For Loops Without a Package

Since we now have the basics in place to create an integer, compare integers using the operators =, < and > , all we are missing to write a loop is a keyword to do so. The syntax for this is:

\loop \ifsomething
...
\repeat

So a full example would be:

\documentclass{article}

\begin{document}
\newcounter{mycounter}
\setcounter{mycounter}{2}

\loop
\ifnum \value{mycounter} < 5
hello \themycounter,
\stepcounter{mycounter}
\repeat

\end{document}

Output:

### For Loops With the forloop package

Requirement:

\usepackage{forloop}

Usage example:

\documentclass{article}
\usepackage{forloop}
\begin{document}
\newcounter{ct}
\forloop{ct}{1}{\value{ct} < 5}{\arabic{ct} }
\end{document}

Output:

### For Loops With the pgffor Package

If you prefer a more Python-like syntax, the pgffor package might just be for you. I took the examples from this Stackoverflow answer.

Example Snippet 1:

\foreach \n in {0,...,22}{do something}

Full Example 2:

\documentclass{article}
\usepackage{pgffor}
\begin{document}
\foreach \n in {apples,burgers,cake}{Let's eat \n.\par}
\end{document}

Output:

### While Do in Latex with the Ifthen package

Requirement:

\usepackage{ifthen}

Usage Example:

\documentclass{article}
\usepackage{ifthen}
\usepackage{pifont} % for the ding
\begin{document}
\newcounter{ctra} % declare counter, is set to 0
\setcounter{ctra}{3} % set counter to 3
\whiledo {\value{ctra} < 7}
{
\thectra {\large \ding{111}} % the + counter name prints a counter
\stepcounter {ctra} % increase the counter by one
}
\end{document}

Output:

## Floating Point Numbers (Lenghts in Latex…)

If you read the previous paragraphs, you should get the gist of it by now, so I’ll just provide an example for this one:

\documentclass[varwidth]{standalone}
\begin{document}
\newlength{\mylen} %Declaration
\setlength{\mylen}{3.14pt} %Assignment
The number $\pi$ is approximately \the\mylen\ (think away the pt...).

To be (2 digits) more precise, it's rather \the\mylen.

\settowidth{\mylen}{Text} % measures the width of "Text"
"Text" is \the\mylen\ wide... Crazy stuff.
\end{document}

Which yields the following output:

## Comparison to a Normal Programming Language

Let’s compare the crazy LaTeX syntax to a normal language like Java. The following table can also be used for reference.

LaTeXJavaMeaning & Notes
\newif\iffooboolean foo;Declare a boolean. Defaults to false in LaTeX and null in Java
\footruefoo = true;Set foo to true
\foofalsefoo = false;Set foo to false
\iffoo
%do this
\else
%do that
\fi
if (foo) {
//do this
} else {
//do that
}
If-else statement
\newcounter{n}int n;Initialize integer. Defaults to 0 in Latex and null in Java.
\setcounter{n}{2}n = 2;Set n to two
\stepcounter{n}++n;Increases n by one
\value{n}n;Get value of n for calculations
\then %this is actually a \the immediately followed by n...log.info(Integer.toString(n));Get value of n for print
\ifnum\value{n}>12
%do this
\fi
if (n > 12) {
//do this
}
Check if n is larger than 12.
\ifnum\value{n}=12
%do this
\fi
if (n == 12) {
//do this
}
Check if n is equal to 12.
\newcounter{mycounter}
\loop \ifnum\value{mycounter}<5
\themycounter
\stepcounter{mycounter}
\repeat
for (int i = 0; i < 5; i++) {
log.info(Integer.toString(i));
}
For Loop. Prints 0 1 2 3 4
Alternative:
\usepackage{pgffor}
\foreach \n in {0,...,4}{do something}
\newlength{\mylen}float mylen;Create a floating point number. Latex initializes this to 0.0, Java to null.
\setlength{\mylen}{3.14pt}mylen = 3.14;Sets length to 3.14pt in latex and to 3.14 in Java. Since Latex is for typesetting the length must have a unit.
\the\mylenlog.info(Float.toString(mylen));Get mylen for print
\mylenmylen;Get mylen for calculation
\ifnum\mylen<1
%do this
\fi
if (mylen < 1) {
//do this
}
Compare mylen to another number