# What exactly is a command in Latex?

In other programming languages I have come across a command is a verb; it is something for the computer to do. The things that commands act upon are called variables and these come in various types. We can have a string variable or a numeric variable or a date variable and so on, and the code usually instructs the commands to do something to the variables: @ Row(24) Col(65) Print(date), for example.

In Latex, anything that starts with a backslash is called a command, so \author{text} is called a command despite the fact it does nothing. It is really just the declaration of a variable. Not only that, it is a variable I am unable to use, directly. If I type \author Latex tells me I have made an error, and if I type \author{} Latex assumes the author has no name. So the "command" \author{text} is, in real computer programming terms neither a command nor a variable.

The "command" \title{text} is the same. If I actually want to display the title within my document I can either write it directly, by hand, or I can define a new command, \newcommand{mytitle}{title text} and then in the body of my document type \mytitle. So as far as I can determine, the command \title{text} does exactly nothing for my document. It is not a command in the, "do something useful" sense, it is not a variable in the, "store some data" sense. So what exactly is the proper definition of "command" in Latex?

-
Perhaps you'd enjoy reading the chapter on macros in TeX by Topic by Viktor Eijkhout. The purpose of TeX is quite different from other 'programming languages'.... – jon Oct 23 '13 at 5:01
I think the negative point isn't quite fair. The OP has a genuine question. – A.Ellett Oct 23 '13 at 5:01
Think of \author and \title as what is called setters in other programming languages. In Java, you would probably call them setAuthor() and setTitle(), respectively. They set internal ("private") state that is then used by other commands ("methods"), in this case it is \maketitle that uses the "private" state and actually does typeset it. – Daniel Oct 23 '13 at 7:20
for the record, knuth calls any alphabetic string beginning with a backslash a control sequence (more specifically, a control word). i don't remember the term "command" being used in this sense in the texbook; other people started using that term because it's shorter, and more easily understood (if imperfectly) by a newbie. – barbara beeton Oct 23 '13 at 7:34
Why do you say \author{text} does nothing? Of course it produces no text in the document and no messages on screen, but surely code in many programming languages will do that. One can even type commands in a command line window and have apparently nothing happen. – Dan Oct 23 '13 at 18:47

LaTeX commands do do something. For example, in the case of \author, the command does the following:

\gdef\@author{#1}


That is \author globally defines a macro \@author to store the name of the author for use later.

\@author is then called when you place \maketitle (depending on how a particular class defines \maketitle). So, \author is a command that takes 1 argument and stores that argument in another, internal, command \@author which can be called later on.

The commands \author and \title are usually set in the preamble to your document:

\documentclass{article}
\author{Martin}
\title{My document title}
\begin{document}
\maketitle

Content of my document.

\end{document}


It's a bit more difficult to get the content of \@author. For example, in the article class, once you call \maketitle, the content of the macro \@author is cleared. You could work around this by doing something along the following lines:

\documentclass{article}
\author{Martin}
\title{My document title}
\makeatletter
\let\my@author\@author
\newcommand\getauthor{\my@author}
\makeatother
\begin{document}
\maketitle

Content of my document.

The author's name is \getauthor.

\end{document}


What I've done here is save the content of \@author in my own macro \my@author. Then I define a command to retrieve the content of my macro.

If you want to access the title of your document, then you should do something similar to what I did for accessing the author:

\makeatletter
\let\my@title\@title
\newcommand\getmytitle{\my@title}
\makeatother


What's import here is that the letting be done after you've called \author and \title. Otherwise, you'll most likely get an error message. That is \@author and \@title are defined to issue errors if you try to use them and they haven't already been defined by call from \author{...} and \title{...}.

## Deeper issue of what a command is about

Some commands in LaTeX take an apparently "immediate" effect such as \textbf{...} which will set the content of the argument in a bold font, \section{...} which will start a new section whose title will be that of the passed argument. Some commands require multiple runs of LaTeX before things settle down: for example, \tableofcontents. Some commands work in tandem: \label{...} and \ref{...}. Some commands don't seem to take any effect until something else happens: such as in the case of \author{...} and \title{...} whose effects are not noticeable until you call \maketitle.

Depending on what kind of a background you're coming from, it can be a bit tricky thinking about LaTeX macros/commands. There are various complicating factors: The asynchronous nature of how LaTeX makes various formatting decisions can lead to many headaches if you're not aware of them. Some commands expand when you call them (in the mouth of LaTeX). Other commands wait until they're in the gullet. Generally, it's a very broad topic that is probably best handled on a case by case basis.

The suggestion in the comments to your question to read TeX by Topic is a very good suggestion. If you're just trying to write a document and the commands aren't doing what you're expecting them to do, there are quite a number of good resources. You might want to check out some of the titles suggested at What is the best book to start learning LaTeX?. Of course, if you really want to get into the nuts-and-bolts of it, there's The TeXbook by Donald Knuth (but if you're just getting started, it's not the first place I would recommend someone look).

If you want to see how individual commands are defined, there are several approaches. If you have access to a command line and you're working on a Linux box or Mac, then you can try to locate a given class and look at the actual code. Alternatively, you can call from the command line texdef (or latexdef--though I've not had success with latexdef on my Mac), to see the source code for a command:

 \$ texdef -t latex -c article maketitle -s


which will spit out to your terminal the definition that the article class uses for \maketitle.

You can also use texdoc source2 to look at the documentation for LaTeX kernel. Or texdoc article to get the documentation for the standard document classes.

-
A very full and informative answer from A. Ellett, so I must thank you for the effort which is appreciated, but it doesn't actually answer the question I thought I had asked. Maybe the question reads differently to other people than it does to me. – Martin Oct 23 '13 at 10:58