% !TEX root = ../manual.tex
% !TEX program = xelatexmk
% !TEX spellcheck = en_GB

\section{Variables \& Commands}
In \LaTeX\ you can make use of three ways to define a command:

\begin{multicols}{2}
\begin{lstlisting}
% TeX
\def\firstmacro#1#2{Macro #1}
% LaTeX2e
\newcommand\secondmacro[1]
{Macro #1}
% LaTeX3, from the `xparse` package
\DeclareDocumentCommand
\thirdmacro{ m }{Macro #1}

First: \firstmacro{1} \\
Second: \secondmacro{2} \\
Third: \thirdmacro{3}
\end{lstlisting}
\columnbreak
% TeX
\def\firstmacro#1{Macro #1}
% LaTeX2e
\newcommand\secondmacro[1]{Macro #1}
% LaTeX3, from the `xparse` package
\DeclareDocumentCommand\thirdmacro{ m }{Macro #1}

First: \firstmacro{1} \\
Second: \secondmacro{2} \\
Third: \thirdmacro{3}
\end{multicols}

I do \emph{not} recommend using |\def| for anything except storing simple text, because it's very finicky and will overwrite commands without prompt.
|\newcommand| is also very limited, so unless you have no choice: use |\DeclareDocumentCommand|.

The syntax of |\DeclareDocumentCommand| is quite straightforward:
\begin{lstlisting}
\DeclareDocumentCommand\foo{ s o O{bar} m }{%
	% s : star, like \foo vs \foo*
	\IfBooleanTF{#1}{With star}{Without star}
	% o : optional without default
	\IfNoValueTF{#2}{Without argument}{With argument: #2}
	% O{bar} : optional with default value (bar)
	#3
	% m : mandatory argument
	#4
}
% If the command is already defined and you want to replace it:
\RenewDocumentCommand\foo{ m }{Argument: #1}
\end{lstlisting}

There are a lot more options, but for those you should really look at \fnhref{http://mirrors.ctan.org/macros/latex/contrib/l3packages/xparse.pdf}{the documentation} or \fnhref{http://uk.tug.org/wp-content/uploads/2009/11/xparse.pdf}{this presentation}.
