\newcommand with arguments in LaTeX
If you’re using LaTeX to write a document (article, thesis, grocery list, etc.), you can define your own custom commands to save you time and make your code look cleaner. At the beginning of your document include the line
\newcommand{\newtag}{whatever_you_want}
Now, if you type “\newtag” in your LaTeX code, it will be automatically substituted with “whatever_you_want” when the LaTeX is compiled! This is great, but you can do even more. Changing the syntax slightly, you can pass arguments to these macros so that they become little functions. For instance if you want a quicker way to make things bold, include
\newcommand{\bt}[1]{\textbf{#1}}
Now, if you type “\bt{x}”, you’ll get “\textbf{x}” which will be interpreted as x. In this syntax, the number in []‘s is the number of arguments the macro should expect, and it is referenced as #1. You can also do this with multiple arguments like this
\newcommand{\vfrac}[2]{\ensuremath{\frac{#1}{#2}}}
When you call this you’d type \vfrac{a}{b} to make a fraction a/b. There will be a set of {} for each argument. In this command I also used the \ensuremath{} command. This command allows you to put ‘math’ commands into regular text or in equations, and is a big help when writing papers!
I hope this information helps you. Thanks to these guys for posting the info I finally found when I needed to find out.
Recent Comments