LaTeX: create and use variable names

Why we talk about variables in LaTeX?

I use LaTeX to write legal documents.

Legal documents are full of names and data that can be entered several times.

I need a system to enter the names and the recurrent data only once in the act, both to avoid wasting time rewriting the same things and to reduce the risk of typing errors.

The solution is the newcommand command.

What’s the newcommand command?

Technically the newcommand command is used to create new commands.

It therefore serves to extend the default command set.

But a command that prints a text string or number is exactly what it takes to create an alphanumeric variable.

Then this command can be used to create alphanumeric variables within the documents

Let’s create an alphanumeric variable with newcommand

The structure of the syntax to create the variables is very simple: \newcommand{\vbname}{whatever you want}

Obviously, the previous command must be inserted in the preamble.

You can see three steps:

  1. the first step: the backslash and the name of the command to create other commands, i.e. \newcommand
  2. the second step: the creation of a variable that, being a command, needs its backslash, i.e. \vbname (which means variablename)
  3. the third step: assigning content to the variable. Here you can insert text or numbers or whatever you want in the variable content that will be printed in place of the variable name.

Then you can write \vbname in the source code and get whatever you want in the printed document.

A few examples

First example: repeating a name

I need to repeat a long name in a document. The name is, unfortunately, a little complex: supercalifragilistichespiralidoso.

If I had to write the name letter by letter every time I could easily make mistakes or forget a few letters.

The solution is really simple: in the preamble I can create a variabile, named for example sp, and assign it the word supercalifragilistichespiralidoso:

\newcommand{\sp}{supercalifragilistichespiralidoso}

In the source document I can simply insert the command \sp and, after the compilation, I will get a printed document with supercalifragilistichespiralidoso.

In this way I will be able to write faster and have the guarantee that the name will be repeated without variations.

In legal documents this is a great advantage!

Another example: using the same fragment in many documents

If I wrote a very interesting piece of act I’d like to use it for various acts, to save time. But there’s a problem: in that piece I inserted various names of people and places.

If I use the piece with its proper names I would get an act with the proper names of another act. If I change all the names, one by one, I would waste too much time.

The solution is to use variables.

See the following example:

The languages of Europe are members of the same family. Their separate existence is a myth. For science, music, sport, etc, Europe uses the same vocabulary.

Now I substitute the boldanames with different words.

The languages of \cont\ are members of the same family. Their separate existence is a myth. For \first, \second, \third, etc, \cont\ uses the same vocabulary.

At this point you must insert, in the preamble, the content of every variable: \newcommand{\cont}{Europe} \newcommand{\first}{science} \newcommand{\second}{music} \newcommand{\third}{sport} After the LaTeX compilation you will get the same fragment of text with the names in clear. If you need to use the same phrase but with other names you must only modificate che preamble with other assignments such as:

\newcommand{\cont}{America} \newcommand{\first}{technology} \newcommand{\second}{literature} \newcommand{\third}{fitness} And now, after compiling, you will get:

The languages of America are members of the same family. Their separate existence is a myth. For technology, literature, fitness, etc, America uses the same vocabulary.

There is no risk of forgetting a name from the previous version and you don’t waste time changing all the names of the next fragment.

The backslash after the variable

You probably noticed the backslash character after cont: \cont\.
You have to add it if you need a blank space after the word (Europe are or America are).

It you don’t add it you would get two pasted words (Europeare or Americaare) because the command is not normally programmed to insert a space after the generated name.

To solve this problem you have to separate the end of the command with a backslash.

Then you will have two backslashes: at the beginning and at the end of the command.

But if you have a punctuation character immediately following the command itself you don’t need the final backslash.

Thak You for Your attention

Originally published at Franco Pasut’s Tumblr Blog.

--

--

--

I am a lawyer and occasionally write about technology.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

When You Know You Can ’t Meet the Release Date

Joe asks:

The Art of Deploying a Service Mesh!

BTZC Staking Update

How to deploy your Streamlit App with Heroku ?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Franco Pasut

Franco Pasut

I am a lawyer and occasionally write about technology.

More from Medium

Static and dynamic libraries in C

How to Use Root C++ Interpreter Shell to Write C++ Code

Regex secrets revealed

C++ — Command Line Arguments