Access Keys:
Skip to content (Access Key - 0)
 
More about this article
Created: 11/07/2008 10:33
Modified: 02/25/2009 10:22

How can I define new commands in LaTeX?

If you find yourself needing to use a particular bit of text (with or without LaTeX commands) in your document multiple times, it may be convenient for you to define it as a command, using \newcommand. For example, if you were tired of typing the commands for a degree symbol, you might do

   \newcommand{\dg}{$^\circ$ } 

and then in your text, you could say

   At 10 in the morning, it was 68\dg outside

and LaTeX would automatically substitute "$^\circ " to make a degree symbol where you had typed \dg.

Next, you can create commands which include arguments, by including a field for the number of arguments in your \newcommand line, and putting the arguments themselves in as #1, #2, and so on. For example, if you were creating a description list and you wanted each of them to have the item itself to be in bold and the rest to be in italics, like so

   \begin{description}
   \item[\bf Elephant] {\em A big grey animal with a trunk}
   \item[\bf Zebra] {\em A stripey looking horse}
   \end{description}

then you could use a \newcommand for that:

   \newcommand{\animal}[2]{\item[\bf #1] {\em #2}}

   \begin{description}
   \animal{Elephant}{A big grey animal with a trunk}
   \animal{Zebra}{A stripey looking horse}
   \end{description}
Your Rating: Results: PatheticBadOKGoodOutstanding! 1 rates

Adaptavist Theme Builder Powered by Atlassian Confluence