(click anywhere to close)

# [LaTeX] Semantics & Custom Macros

category: Writing | course: LaTeX | difficulty:

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

For instance, say you want to emphasize certain words or phrases by making it italic. Then you could use the \textit{phrase} command on everything, and you’re done. But that command carries no meaning, and what if, just before printing the document, you decide that you want to place emphasis using underlined typesetting? 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, and define your own styling rules for it. Firstly, this shows that these words are emphasized, and secondly, you can easily change the styling rules and everything will be fine.

## 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.

## New Commands

The syntax for creating a new command is

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

Here, numArg is the number of arguments it accepts, with a maximum of 9. Only the first one of those can be optional, and if it is not 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.

% Defining our own counter
\newcounter{myromancounter}
\setcounter{myromancounter}{1}

% Defining our own command, \romansection, which takes 1 argument, and automatically increments our counter

\romansection{This is the first section}
\romansection{This is the second section}


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

\renewcommand{\name}[numArg][default]{definition}

% We renew the existing section counter to one with Roman numerals
\renewcommand{\thesection}{\Roman{section}}

\section{This is the first section}
\section{This is the second section}


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.

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:

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

The before defines the commands to be executed right before the environment starts, and after the commands to be executed right after the environment has wrapped up.

\newenvironment{noIndent}{\noindent}{\\}

A paragraph!

\begin{noIndent}
A paragraph!
\end{noIndent}

A paragraph!


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>.

\newenvironment{noIndent}{\noindent \newcommand{\boldText}[1]{\textbf{##1}}}{}

A paragraph!

\begin{noIndent}
A \boldText{cool} paragraph!
\end{noIndent}

CONTINUE WITH THIS COURSE
Do you like my tutorials?
To keep this site running, donate some motivational food!
Crisps
(€2.00)
Chocolate Milk
(€3.50)
Pizza
(€5.00)