Difference between revisions of "LaTeX:Commands"

 
(How to Build Your Own Commands)
Line 53: Line 53:
 
\newcounter{prob_num}
 
\newcounter{prob_num}
 
\setcounter{prob_num}{1}
 
\setcounter{prob_num}{1}
\newcommand{\prob}[5]{\bigskip \bigskip\arabic{prob_num}.\stepcounter{prob_num} #1 \par\nopagebreak[4]\medskip A.\ #2\hfill B.\ #3\hfill
+
\newcommand{\prob}[5]{\bigskip \bigskip\arabic{prob_num}.\stepcounter{prob_num} #1
 +
\par\nopagebreak[4]\medskip A.\ #2\hfill B.\ #3\hfill
 
C.\ #4\hfill D.\ #5\hfill E.\ NOTA}
 
C.\ #4\hfill D.\ #5\hfill E.\ NOTA}
  
Line 62: Line 63:
 
\prob{What is $\sqrt{100}$?}{81}{10}{9}{1}
 
\prob{What is $\sqrt{100}$?}{81}{10}{9}{1}
  
\prob{Evaluate $\displaystyle\sum_{n=1}^\infty \frac{1}{n^2}$.} {$\displaystyle\frac{1}{e}$} {$\displaystyle\frac{2}{\pi}$} {$\displaystyle\frac{\pi^3}{8}$} {$\displaystyle\frac{\pi^2}{6}$}
+
\prob{Evaluate $\displaystyle\sum_{n=1}^\infty \frac{1}{n^2}$.}
 +
{$\displaystyle\frac{1}{e}$} {$\displaystyle\frac{2}{\pi}$}
 +
{$\displaystyle\frac{\pi^3}{8}$} {$\displaystyle\frac{\pi^2}{6}$}
  
 
\end{document}
 
\end{document}
Line 85: Line 88:
  
 
Once you build a body of custom commands that you will be using in many LaTeX documents, you should learn about [[LaTeX:Packages|creating your own package]] so you don't have to copy all your custom commands from document to document.
 
Once you build a body of custom commands that you will be using in many LaTeX documents, you should learn about [[LaTeX:Packages|creating your own package]] so you don't have to copy all your custom commands from document to document.
 
  
 
==See Also==
 
==See Also==
 
*[[LaTeX:Packages | Next: Packages]]
 
*[[LaTeX:Packages | Next: Packages]]
 
*[[LaTeX:Symbols | Previous: Symbols]]
 
*[[LaTeX:Symbols | Previous: Symbols]]

Revision as of 22:31, 10 July 2007

Template:LaTeX

This page introduces various useful commands for rendering math in LaTeX, as well as instructions for building your own commands.

Math Commands

Here are some commonly used math commands in LaTeX.

Exponents and Subscripts

Fractions

Radicals

Sums, Products, Limits and Logarithms

Mods

Combinations

Trigonometric Functions

Calculus

Other Functions

Matrices

Text Styles in Math Mode

How to Build Your Own Commands

The command \newcommand is used to create your own commands. We'll start with an example:

\documentclass[11pt]{article}
\usepackage{amsmath}

\pdfpagewidth 8.5in
\pdfpageheight 11in
\newcommand{\reci}[1]{\frac{1}{#1}}
\newcommand{\hypot}[2]{\sqrt{#1^2+#2^2}}
\newcommand{\cbrt}[1]{\sqrt[3]{#1}}

\begin{document}

The reciprocal of 2 is $\reci{2}$.

The hypotenuse has length $\hypot{3}{4}$.

I'm sick of writing `$\backslash$sqrt[3]{2}' all the time, just to get $\cbrt{2}$.

\end{document}

The \newcommand declarations are in the preamble. Each is of the form

\newcommand{name of new command}[number of arguments]{definition}

The name of the new command, which must begin with a \, is the name you'll use in the document to use the command. The number of arguments is how many inputs will be sent to the command. The definition is just normal LaTeX code, with #1, #2, #3, etc., placed where you want the inputs to go when the new command is called.

New commands can be used for all sorts of purposes, not just for making math commands you'll use a lot easier to call. For example, try this:

\documentclass[11pt]{article}
\usepackage{amsmath}

\pdfpagewidth 8.5in
\pdfpageheight 11in
\newcounter{prob_num}
\setcounter{prob_num}{1}
\newcommand{\prob}[5]{\bigskip \bigskip\arabic{prob_num}.\stepcounter{prob_num} #1
\par\nopagebreak[4]\medskip A.\ #2\hfill B.\ #3\hfill
C.\ #4\hfill D.\ #5\hfill E.\ NOTA}

\begin{document}

\prob{What is $2+2$?}{4}{5}{6}{7}

\prob{What is $\sqrt{100}$?}{81}{10}{9}{1}

\prob{Evaluate $\displaystyle\sum_{n=1}^\infty \frac{1}{n^2}$.}
{$\displaystyle\frac{1}{e}$} {$\displaystyle\frac{2}{\pi}$}
{$\displaystyle\frac{\pi^3}{8}$} {$\displaystyle\frac{\pi^2}{6}$}

\end{document}

In the example above, we create a new command called \prob. Each time we call \prob, we supply 5 arguments, one for the question and one for each of the multiple choices.

In the preamble and the definition of \prob, you'll see a few new LaTeX commands:

\newcounter{prob_num} creates a counter variable called prob_num

\setcounter{prob_num}{1} setsprob_num to equal 1.

In the definition of \prob, the \bigskip and \medskip commands create vertical space.

\arabic{prob_num} prints out the current value of the counter prob_num as an arabic numeral.

\stepcounter{prob_num} increments the counter prob_num by 1.

\nopagebreak[4] tells LaTeX not to break the page between the problem and the choices unless it really, really, really has to.

The \hfill commands put roughly equal space between the choices.

Once you build a body of custom commands that you will be using in many LaTeX documents, you should learn about creating your own package so you don't have to copy all your custom commands from document to document.

See Also