Creating Your Own LaTeX Macros

2009 June 30
by Chris Engelsma

\LaTeX is an excellent document markup language which is used, predominantly in the academic industry, to produce crisp reports and simple professional presentations or slides.  Why not just use Microsoft Office?  Well, there are a lot of reasons why you’d want to choose between these two environments.  For example, one might want to use Office because it is WYSIWYG (or, What You See Is What You Get), which is a programming term used for a system where the content displayed during editing appears similar, if not identical, to the final product.  This is very handy for the people who enjoy watching their report grow.  They can recognize if something doesn’t look correct immediately.  However, this can be a hindrance when you know what you want but can’t seem to find that damn button that does it.

The \LaTeX environment is rather text-based, and you don’t see the actual product until you compile it.  One benefit of \LaTeX is that it handles a lot of things for you, and makes it look good, and keeps organization easy when it comes to references, citations and equations.  The problem here comes in when you don’t know the correct syntax, which is comparable to the previous problem.  So, \LaTeX isn’t for everyone, but then again neither is Office.

For those of you who use \LaTeX, you can make your lives a lot simpler if you create a basic macro file that handles all those cumbersome commands.  So, to show an image you go from typing:

\begin{figure}
\includegraphics[width=200px]{earth.png}
\caption{This is planet Earth.}
\label{fig:earth}
\end{figure}

\begin{figure}
\includegraphics[width=200px]{earth.png}
\caption{This is planet Earth.}
\label{fig:earth}
\end{figure}

to

\fig{200px}{earth.png}{This is planet Earth.}{fig:earth}

which effectively reduced the number of lines from 5 to 1.

To create your own macro, create a tex file that will be easy for you to remember, such as macro.tex.

Within your blank .tex file, create your first definition by using the \def command.  For this one, we will use the figure example above.  Type:

\def\fig#1#2#3#4 {
\begin{figure}
\includegraphics[width=#1]{#2}
\caption{#3}
\label{#4}
\end{figure}
}

Following the \def declaration you must put the name of your function, followed with however many parameters you want to have the freedom to choose (4, in this case).  Notice how they are entered as #1#2#3#4.  Within the curly braces of the function, place the command you want, with each variable replaced with a variable number.

To use the macro you’ve just made, in the preamble of your report put:

\input{macro.tex}

Assuming your macro is in the same directory as your file.  Then in the main file all you need to do is call your \fig command with all the parameters to produce the desired output

Now you can dump all the redundant cumbersome functions you want in your macro.tex file and let that be the last time you’ll ever have to type all those out!

No comments yet

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS