Wednesday, February 20, 2013

Defining Custom Commands in LaTeX


\( \LaTeX \) is a very powerful typesetting system. Its power is greatly augmented by the fact that it allows you to define your own commands. In general, a custom command will be placed in your document's preamble (meaning before \begin{document}) and will follow the form:

\newcommand{\command_name}[number_of_arguments]{command_definition}

If your command has no arguments, then you exclude the [number_of_arguments] section. In the {command_definition} section you refer to passed arguments by using  #n where n is the number of the argument you are passing. To pass down multiple arguments, you simply tack on new curly braces for each one. Here are some examples:

Example 1: Automate left and right parenthesis

 

Definition

 

\newcommand{\p}[1]{\left( #1 \right)}

In Use


\p{ 5.0 \times 10^3 } + \p{ 2.1 \times 10^{18} }

Output

 

\( \left( 5.0 \times 10^3 \right) + \left( 2.1 \times 10^{18} \right)\)



Example 2: Define Kilohms

 

Definition


\newcommand{\ko}{k \Omega}

In Use


R = 50 \ko

Output

 

\( R = 50 k \Omega \)


Example 3: Pythagorean's Hypotenuse

 

Definition


\newcommand{\hyp}[2]{ \sqrt{ #1^2 + #2^2 } }

In Use


\hyp{500}{12}

Output

 

\( \sqrt{ 500^2 + 12^2 }\)

No comments:

Post a Comment