User defined commands in LaTeX

A very convenient tool in LaTeX is the ability to create macros or user defined commands. This turns out to be very productive and can save a lot of time, especially when you are using certain expressions frequently. This can also save a lot of headache if halfway through your document, you decide that you want to change notation. Also, when you have very long expressions that you are repeatedly using, it makes no sense to type it over and over again.

The main commands used for this purpose are {\backslash \texttt{newcommand}} and {\backslash \texttt{def}}.

The user defined commands must be defined in the preamble, i.e., the place after the {\backslash \texttt{documentclass}} but before { \backslash \texttt{begin} \{ \texttt{document}\} }.

As an example, say that you are using an expression like {\mu_{0}^{(k)}} to denote a quantity. You can define a command like

{\backslash \texttt{newcommand}\{ \backslash \texttt{mk} \} \{ \backslash \texttt{mu}\_ \{ 0 \} \hat{} \{\texttt{(k)}\} \}}

Whenever you want to use the expression, simply type out {\backslash \texttt{mk}}. Note that you can use this only in the math mode and LaTeX will give you an error if you use it in the normal mode. However, this can be solved using the {\backslash\texttt{ensuremath}} command. Use

{\backslash \texttt{newcommand}\{ \backslash \texttt{mk} \} \{ \; \backslash \texttt{ensuremath} \{\backslash \texttt{mu} \_ \{ 0 \} \hat{} \{\texttt{(k)}\} \} \; \}}

and you can use \mk in both cases.

Note that this command can be used for text also. For example, say you want to replace “Direct sum decomposition” by a compact representation. Use

{\backslash \texttt{newcommand} \{ \backslash \texttt{dsd} \} \{ \texttt{Direct sum decomposition} \} }

and you’re done. This command turns out to be really useful when you want to specify arguments. As an example, say that you want to define a function {f_{0}(x^{d})}. You can define a user defined command of the form

{\backslash \texttt{newcommand} \{ \backslash \texttt{fx} \}\{ \texttt{f} \_ \{\texttt{0} \} \texttt{(x} \hat{}\: \{\texttt{d}\}) \}}

But in some other place, you need {f_{0}(y^{l})}. You don’t have to define a new command. Instead, you can specify arguments in the following manner:

{\backslash \texttt{newcommand} \{ \backslash \texttt{fx} \}[2]\{ \texttt{f} \_ \{\texttt{0}\}(\# \texttt{1} \hat{}\: \{\# \texttt{2}\}) \} }

Here, {\# 1}, {\# 2} represent the two arguments. When you want to use the same in an equation, simply use {\$ \backslash \texttt{fx}\{\texttt{x} \}\{ \texttt{d}\} \$} to get {f_{0}(x^{d})}. You can specify upto 9 arguments numbered {\# 1} to {\#9}.

The {\backslash \texttt{def}} command behaves similarly. But a word of caution. If you use a command name that corresponds to a default LaTeX command, {\backslash\texttt{newcommand}} will give an error whereas {\backslash \texttt{def}} will simply overwrite the default definition. The syntax (for the first example) is

{\backslash \texttt{def} \backslash \texttt{mk} \{ \backslash \texttt{mu}\_ \{ 0 \} \hat{} \{\texttt{(k)}\} \}}

Note that there are no braces right after {\backslash \texttt{def}}, unlike {\backslash\texttt{newcommand}}. To specify arguments,

{\backslash \texttt{def} \backslash \texttt{fx} \langle\# 1, \# 2\rangle\{ \texttt{f} \_ \{\texttt{0}\}(\# \texttt{1} \hat{}\: \{\# \texttt{2}\}) \} }

When you want to invoke this in your document, use { \$ \backslash \texttt{fx} \langle \texttt{x,d} \rangle \$ }.

References:

G. Gratzer, Math into LaTeX, 4th ed, Springer

Advertisements

6 thoughts on “User defined commands in LaTeX

  1. How do you save all your custom commands into a library? I’d like to be able to do this and all my hotkeys, and somehow ‘port’ it, so when I load the library into the preamble (on any machine pending I have a parameter/library file), it contains all my user-defined commands and hotkeys.

    Thanks for the post!

    Chris

    • You can save all your commands in a .sty file in your working directory. For example, I save all the custom commands, definitions, and other preamble declarations in a file called mypreamble.sty
      Just add \usepackage{mypreamble} in the preamble of your source .tex file and you’re done.

      Hope this helps!

  2. Another question. Can you set up a new commands that tabs to the arguments? In other words, say I’m wanting to speed up filling in a definite integral and defining it such:
    \newcommand{definint}[4]{\int^{#1}_{#2}{#3 -comment, here is the f(x)} and d{#4 -comment with respect to which variable}. I’m sure the syntax is way off, but hopefully you understand what I’m after.

    Does this make sense? I like the tab formats and think it would be great if I could just call up my ‘base’ command and simply tab to fill in all the arguments such as upper and lower bounds, f(x) and with respect to which variable.

    I’m new at this so thanks!

    Chris

    • Yeah, you can do that. For the definite integral example, declare a new command
      \newcommand{\defintg}[4]{\int^{#1}_{#2}{#3}d{#4}}
      in the preamble. To use this in the math mode, you can do
      $g(x)=\defintg{\infty}{0}{f(x)}{x}$.

      • Hey, thanks for the reply, however, this isn’t really what I’m looking for (I think). Maybe I’m looking at defining a shortcut or hotkey, but essentially, I would like to type the command or hotkey, and the output looks like this -> \defintg^{•}_{•}{•}d{•} and all I need to do is ‘tab’ through and define the parameters. Does that help? Anyway, thanks again!

        Chris

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s