# Math Macros: The best feature of Lyx you’re not using

$\newcommand{\lyxlock}{}$
LyX is a brilliant tool for writing in the mathematical sciences. It provides a WYSIWYG style editor on top of LaTeX with just the right amount of abstraction for getting-stuff-done. With the proper setup, typing math into LyX is easier, faster and less error prone then using LaTeX directly, and it has virtually no down sides.
Part of the setup is the use of use of LyX math macros. Math macros allow you to define new commands that you can use within LyX math mode. When writing pure LaTeX, most people include a set of standard macros in the header of the LaTeX documents. For example, a common one is a shortcut for argmin:
\newcommand{\argmin}{\operatornamewithlimits{argmin}}

LyX math macros allow you to do the same thing within LyX.
To create the equivalent macro in LyX, create a new math macro
This will insert the following into your document:
This won’t appear when you render out your document, so you can put it anywhere you like. To create the macro, just fill out the 3 parts. The first part is what you will type when using the macro, the second part is the LaTeX that will be output, and the third part controls what it looks like on screen within LyX. For most macros the TeX and LyX sections should be the same, for the argmin example we want the TeX code to produce a “mathop”, which LyX can’t natively display. So we put a \TeXtrm{argmin} in the LyX part. The filled in macro is thus:
Note that for this to work you have to have amsmath package imported. To set that up, go Document -> Settings..., then select the radio buttons under the math options section:
To use the macro, just type \argmin within a LyX math box, then press space. It should insert the macro seemlessly. Within LyX the subscripts will look like:
but when you render it out to a pdf it will be typeset correctly as:

## A few useful macros

I have the following macros setup in a lyx-math-macros.lyx file:
These macros illustrate the parameters feature as well. To add parameters to a macro, use the macro bar that appears at the bottom of the screen when editing the macro.
I have a lyx template setup that I use for new documents. It includes a LyX sub-document include statement which gives access to the macros.

## How it’s implemented

If you convert your LyX document to a pdfLaTeX document, you’ll see the following command is generated in the preamble
\global\long\def\argmin{\operatornamewithlimits{argmin}}

This uses lower level LaTeX commands then \newcommand, but it has a similar effect . At the call site you’ll see something like:
{argmin}_{x\in \mathbb{R}}

Other then the extra {} brackets, this is as simple as you could want.

## 8 thoughts on “Math Macros: The best feature of Lyx you’re not using”

1. Damian says:

How can I make a macro with something like this?

\begin{logicproof}{1}a\&a\end{logicproof}

2. Julián Calderón Almendros says:

Is this characteristic aboslutly the same as \newcommand in LaTeX?

3. Great post, I remember discovering this feature a while after using LyX and feeling so disappointed for not knowing about it earlier!

4. Jim says:

It’s worth noting that sometimes you just want plain old \operatorname{} rather than \operatornamewithlimits{}. If it’s a (multi-letter) function or variable you want the first, which is why e.g. \sin is defined that way. If it’s something like a limit e.g. arg\,max, then you should use \operatornamewithlimits{}. Put another way, if it’s something you’ll ever want to square or invert, you should definitely use \operatorname{} otherwise the 2 or -1 would go right above the identifier!

5. James says:

Thanks! Very well explained. I’ll probably try to make some matrix macros for my linear algebra class this semester.

6. Jotaf says:

Wow, great feature! The LyX folks sure could make it a bit more visible (as a math toolbar button?). It should save a lot of clicking when using exotic notation. Finding that Ctrl+B works well in math mode for bold vector notation was also one of those “D’oh, why didn’t I know of this” moments. Thanks for sharing!