Icon for parent category 'Writing'

Semantics & Custom Macros

Before I start introducing command after command, I want to talk about something fundamentally important: semantics. Semantics means separating your markup and layout. In other words: using meaningful commands that say something about what is inside. Meaningful commands, together with counters, form the vast majority of working with LaTeX.

For instance, say you want to emphasize certain words or phrases by making them italic. Then you could use the \textit{phrase} command on everything, and you’re done. But that command carries no meaning. It merely states some visual change, but doesn’t mark the text as anything.

And what if, just before printing the document, you decide that you want to place emphasis instead using bold text? You’d have to go in and change every single instance where you used this command. And still, the command would be meaningless, just to get the layout right.

The solution is to work semantically. Instead, use the \emph{phrase} command. That command states: this text is emphasized. Now you and the software know this piece of text is slightly more important than the other words in the sentence.

If you markup the document like that, everything has the correct meaning assigned. And then you can easily define your own styling rules later about how to show emphasized text.

Custom Macros

To be able to do this, you need to know how to create your own commands, and how to alter existing ones. At first, I planned on including this chapter as the very last one. But I decided that it’s just too useful to not know about when first learning LaTeX. For the reason stated above: get into the good habit of writing semantic markup, of seperating layout and content.

New Commands

The syntax for creating a new command is

1\newcommand{\name}[numArg][default]{definition}

Here, numArg is the number of arguments it accepts, with a maximum of 9.

Remark

Like with deep nesting: if your macro needs that many arguments … ya gotta reconsider what you’re trying to do 😛

Only the first one can be optional. If it isn’t present—which is possible, because it’s optional—the default value will be used. Within the definition, you can use #<number> to refer to the arguments.

 1% Defining our own counter
 2\newcounter{myromancounter}
 3\setcounter{myromancounter}{1}
 4
 5% Defining our own command, \romansection, which takes 1 argument, and automatically increments our counter
 6\newcommand{\romansection}[1]
 7    {\section*{\Roman{myromancounter}. #1} 
 8    \addtocounter{myromancounter}{1} 
 9}
10
11\romansection{This is the first section}
12\romansection{This is the second section}
Code left > output right
Code left > output right

Of course, new commands must carry a name that’s not already in use. If you want to overwrite an existing command instead, use

1\renewcommand{\name}[numArg][default]{definition}
1% We renew the existing section counter to one with Roman numerals
2\renewcommand{\thesection}{\Roman{section}}
3
4\section{This is the first section}
5\section{This is the second section}
Code left > output right
Code left > output right

Note that, when renewing a command, you can’t use the existing one in the definition - it creates a loop, which LaTeX doesn’t know how to handle.

Remark

Typically, commands are created or altered in the preamble, before anything is processed/printed. But you can do it any time you want.

New Environments

The syntax for creating a new environment is:

1\newenvironment{name}[numArg]{before}{after}

Before defines the commands to be executed right before the environment starts. After the commands to be executed right after the environment has wrapped up.

 1\newenvironment{noIndent}
 2    {\noindent}
 3    {\}
 4
 5A paragraph!
 6
 7\begin{noIndent}
 8    A paragraph!
 9\end{noIndent}
10
11A paragraph!
Code left > output right
Code left > output right

It’s possible to also declare new commands within such a new environment declaration, which will only be available within that environment. Nothing changes, except for the fact that you need to access arguments with ##<number>.

 1\newenvironment{noIndent}
 2    {\noindent 
 3        \newcommand{\boldText}[1]
 4            {\textbf{##1}}
 5    }
 6    {}
 7
 8A paragraph!
 9
10\begin{noIndent}
11    A \boldText{cool} paragraph!
12\end{noIndent}
Code left > output right
Code left > output right
Continue with this course
Support me and this website!

Want to support me?

Buy one of my projects. You get something nice, I get something nice.

Donate through a popular platform using the link below.

Simply giving feedback or spreading the word is also worth a lot.