1. Introduction

In this tutorial, we’ll describe how to define and use variables in LaTeX.

2. Why Do We Need Variables in LaTeX?

Variables are placeholders for storing values we can use throughout the document. They can hold various values and strings, for example, URLs. We need variables for the following reasons:

1. they make writing easier, faster and more consistent
2. help us avoid minor typos
3. variables make it easier to update a value without having to change each occurrence manually
4. instead of manually typing a long phrase multiple times, we can simply define a variable

So, a typical use case is defining document templates.

3. Types of LaTeX Variables

To define a variable, we always start with the def command, followed by the variable’s name (starting with \), and its value (word or string) in curly braces:

\def\name{value}

Afterward, when we want to use our variable, we refer to it by its name from its definition. LaTeX variables are case-sensitive. That means that LaTeX will consider the following variables: \myname, \myName, \Myname and \MYNAME as four different variables.

We differentiate between three types of variables:

1. simple variables
2. variables with parameter
3. variables with long strings

4. Simple Variables

A simple variable stores a single value. The value can be a number, a word or a multi-word string. For example:

\def\myname{Jonathan}

In the above command, we defined a variable \myname holding the word Jonathan. To use this variable in our document, we simply refer to it by its name anywhere we want to insert it. For instance:

Hi, my name is \myname.

will generate:

Similarly, we can define a variable with a numeric value:

\def\myexperience{5}

We can use it in the same way as \myname.

Calling the variable names will insert the defined variable combined (without a blank space) with the succeeding word. For instance:

Hi, my name isn't \myname but Mike.

will generate:

To add a blank space between the defined variable and the next value, we’ll add either a curly bracket {} or backslash \ after the variable. Using the same example:

Hi, my name isn't \myname{} but Mike.

will generate:

5. Variables with Parameters

A variable with parameters takes one or more arguments referred to by their numbers preceded by #. For instance, #1 is the first argument, #2 the second, and so on.

Let’s say we want to format the name and age of an individual. Then, we can define the following variable:

\def\mydetails#1#2{#1 (age #2)}

The above \mydetails takes two arguments: #1 and #2, where #1 is the name, and #2 is the age. Here’s an example of how we could use it in our document:

Candidates \mydetails{Jonathan}{25} and \mydetails{Sarah}{27} applied for this position.

This line of code would generate the following :

Also, there can be a single parameter. For instance:

\def\mydetails#1{#1}

The line:

I am \mydetails{25} years old.

would generate:

6. Variables with Long Strings

Variables can also hold an entire sentence. For example:

\def\mytitle{How to Define Variables in LaTeX}

Typing the variable name \mytitle would output the entire string from its definition. For example:

The article titled "\mytitle" describes how to define a variable in LaTeX

generates:

7. Conclusion

In this article, we showed how to define variables in LaTeX documents and talked about the common variable types.

Using variables makes writing more efficient and consistent. It helps avoid the manual typing of repetitive words and minor typos. Also, it helps us make document templates.