Friday, August 20, 2010

newcommand in LaTeX

New commands

To add your own commands, use the
\newcommand{name}[num]{definition}
\newcommand{\wbal}{The Wikibook about \LaTeX}
This is ‘‘\wbal'' \ldots{} ‘‘\wbal''
=> This is “The Wikibook about LaTeX” … “The Wikibook about LaTeX”
The next example illustrates how to define a new command that takes one argument. The #1 tag gets replaced by the argument you specify. If you wanted to use more than one argument, use #2 and so on, these arguments are added in an extra set of brackets.

\newcommand{\wbalsup}[1] {This is the Wikibook about LaTeX supported by #1}
\newcommand{\wbalTwo}[2] {This is the Wikibook about LaTeX supported by #1 #2}
% in the document body:
\begin{itemize}
\item \wbalsup{Wikimedia}
\item \wbalsup{lots of users!}
\item \wbalTwo{John}{Doe}
\end{itemize}
  • This is the Wikibook about LaTeX supported by Wikimedia
  • This is the Wikibook about LaTeX supported by lots of users!
  • This is the Wikibook about LaTeX supported by John Doe

But there a lot of caveats :-

  1. LaTeX ignores the blank following a command - So the space has to be specifically inserted with the \space.

    \newcommand{\water}{H$_2$O}
    This would allow one to write, e.g.,



      The formula for water is \water. 
    or


      \water\ is the formula for water.
    Note, in the second case, the trailing \ followed by a blank is required to ensure a blank space after the H2O;

  2.  Math mode doesn't work inside math environment -

    \newcommand{\hypotenuse}{$a^{2}+b^{2}$}
    Note that this will produce the desired formula in text (paragraph) mode because of the $...$ in the definition. In math mode, however, the first $ in the definition will cause LaTeX to leave math mode, causing problems.
    In LaTeX 2.09 a standard trick for getting around this is to put the math-mode expression in an \mbox, viz.,



    \newcommand{\hypotenuse}{\mbox{$a^{2}+b^{2}$}}
    In LaTeX2e the \ensuremath command has been provided to alleviate this problem. The argument of the \ensuremath command is always processed in math mode, regardless of the current mode. Using this mechanism the above could be written as




    \newcommand{\hypotenuse}{\ensuremath{a^{2}+y^{2}}}
     
source : http://en.wikibooks.org/wiki/LaTeX/Customizing_LaTeX

http://www.uz.ac.zw/science/maths/latex/ltx-18.html

No comments:

OK GOOGLE on Samsung Galaxy S7 doesn’t work

To make Ok Google detection work on Galaxy S7 (Galaxy series phones) we need to perform a couple of steps. 1. As long as Samsung S vo...