4

I'm a grey headed guy taking a calculus class. I'm trying to figure out how to make a user command with two arguments to generate limit statement with right arrow. I keep having to type \lim_{x \rightarrow \infty} blah blah.

So, I would like shortcut with first param as in x above and second param where the limit is going to.

  • 5
    \newcommand\mylim[2]{\lim_{#1\rightarrow#2}} and \mylim{x}{\infty} (in math mode), or with an optional argument defaulting to "x": \newcommand\mylim[2][x]{\lim_{#1\rightarrow#2}} and \mylim{\infty} and \mylim[y]{\infty} – Gonzalo Medina Feb 8 '14 at 18:27
  • Instead of a command, you could use an editor (or a third party program) which lets you insert snippets. – Manuel Feb 8 '14 at 18:42
9

To define new macros (you might call them commands, shortcuts, etc.) in LaTeX, we use \newcommand (or \renewcommand if we want to use the name of an existing macro). So we start with

\newcommand{\mylim}{code goes here}

where \mylim is the name of the macro we want to define. In the second set of curly parentheses, we add the code we want to use as a shortcut. We use a hash # and a number to denote the first input, second input, and so on. So we'd write:

\newcommand{\mylim}{\lim_{#1 \to #2}}

Then we need to tell LaTeX that this command takes two mandatory arguments, so we add [2] between the name of the macro, and the definition:

\newcommand{\mylim}[2]{\lim_{#1 \to #2}}

More information about defining new macros can be found on this Wikibooks page.

And here's a minimal example:

\documentclass{article}

\usepackage{amsmath}

\newcommand{\mylim}[2]{\lim_{#1 \to #2}}

\begin{document}

\begin{equation*}
  \mylim{x}{\infty} blah\;blah
\end{equation*}

\end{document}

which produces

enter image description here

  • Please provide an explanation of your answer, along with code. – Chris Chudzicki Feb 8 '14 at 18:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.