Crash Course in LaTeX

Workin' It: Making Your Own Commands

Some of the real power in LaTeX comes from being able to create your own commands. The required syntax, placed in the header, is simple:
   \newcommand{\commandname}{Stuff you want the command to do}

Note that the full syntax supports passing arguments to the user-defined command, complete with optional arguments and default values. Look it up to get the full story.


This is a little easier to understand if I give you a real-world example.

I wanted to insert some space between questions on my quizes. I did not want the same space between each question, since some questions require more room for work than other questions. Suppose the source for part of my quiz looks like this:

   \begin{enumerate}
      \item What is your biggest concern/worry/fear about this course?
      \item What do you want to be when you grow up?
      \item What are you studying here at college?
      \item Tell me something about yourself.
   \end{enumerate}

I could add \vspace commands at the end of each question. Then it would look like this:

   \begin{enumerate}
      \item What is your biggest concern/worry/fear about this course? \vspace{3cm}
      \item What do you want to be when you grow up? \vspace{2cm}
      \item What are you studying here at college? \vspace{2cm}
      \item Tell me something about yourself. \vspace{5cm}
   \end{enumerate}

This works, but life could be better.

The solution: create new commands for this purpose. As you can see in my sample quiz, I chose to create some new commands (here modified to the spacing used above):

   \newcommand{\smallspace}{\vspace{2cm}}
   \newcommand{\mediumspace}{\vspace{3cm}}
   \newcommand{\largespace}{\vspace{5cm}}

Now the source for the quiz can be typed as follows:

   \begin{enumerate}
      \item What is your biggest concern/worry/fear about this course? \mediumspace
      \item What do you want to be when you grow up? \smallspace
      \item What are you studying here at college? \smallspace
      \item Tell me something about yourself. \largespace
   \end{enumerate}

New Commands with Parameters

The required syntax is
   \newcommand{\commandname}[numberofparameters]{Stuff to do}
Here is an example that I use. When typing notes I may want to tag a term as being defined for the first time. I like these to be bold and appear in the index. The source without a custom command would look like
   A \texfbf{classic}\index{classic} is a book which people praise but never read.
But add this to the header
   \newcommand{\defn}[1]{\textbf{#1}\index{#1}}
and the source can be entered
   A \defn{classic} is a book which people praise but never read.
Note that #1 refers to the first parameter in braces after the new command is used. If there are more parameters (up to nine) they are #2, #3, etc.
You don't ever have to make your own commands, but you'll be missing out on the main way LaTeX kicks the @$$ of any word precessing program. Give it a try. Start small, then work your way into more as you get comfortable.

Once you have a bunch of commands you like to use all of the time, you can store them in a style file and call that instead of having to copy and paste into each new document. See my page on style files.