share
TeX - LaTeXCommand for argmin or argmax?
[+325] [9] Alejandro
[2010-11-11 17:45:09]
[ math-mode symbols math-operators ]
[ https://tex.stackexchange.com/questions/5223/command-for-argmin-or-argmax ]

How can you get the argmin or argmax in Latex? Two solutions I found are:

\underset{x}{\operatorname{argmax}} 

\DeclareMathOperator*{\argmin}{arg\,min}

Any other ideas?

[1] http://www.breakthru.altervista.org/?p=27

[2] http://researchonsearch.blogspot.com/2007/05/enter-argmax-argmin-in-latex.html

(32) How do these solutions (especially the second) feel not right? The \DeclareMathOperator is the way to go in this case. - Pieter
(1) Personally, I like the look of \text{arg}\,\max\limits_{\theta}\, - Jean-Paul
what about how to make it work in Jupyter? - Charlie Parker
[+361] [2010-11-12 04:05:03] Jan Hlavacek [ACCEPTED]

As Pieter pointed out, the correct way to define argmin and argmax operators in LaTeX is:

\usepackage{amsmath}
\DeclareMathOperator*{\argmax}{arg\,max}
\DeclareMathOperator*{\argmin}{arg\,min}

Actually, using the amsopn package would be sufficient but it's loaded by amsmath internally, which is recommended for math typesetting anyway.

The * in \DeclareMathOperator* places the underscored argument underneath the word rather than to the bottom right of it.


(57) If you use \DeclareMathOperator*{\argmin}{\arg\!\min} you get rid of the ugly space between arg and min. - rbp
(39) @rbp Why should one use \arg\min when simply argmin does the same? - egreg
(4) @egreg you're right. I arrived at this one from some experimentation with other options, and didn't look back :) - rbp
(5) In ConTeXt that is: \definemathcommand [argmin] [limop] {\mfunction{arg\,min}} - mb21
(11) Explanation: I left the thin space (\,) between the arg and min because that's what the original poster had in the question, so I assumed that they wanted the space there. Personally I would just do \DeclareMathOperator*{\argmin}{argmin}, without the space. - Jan Hlavacek
(1) How can I make it work also in a context like $\argmin_x{v(x)}$ i.e. when the code is between $$ and not in equations? - Donbeo
what about how to make it work in Jupyter? - Charlie Parker
@CharlieParker This should work with MathJaX in Jupyter, and KaTeX already has \argmax. What does not work for you in Jupyter? - mforbes
1
[+185] [2015-12-20 21:45:36] egreg

Summary

In the sequel, LaTeX is assumed.

Preliminary questions

1. Should a thin space separate “arg” from “min”?

This is a matter of personal preference. The default definition for \limsup has a thin space, on the other hand “arcsin” is usually written without a space between the two components. Consistency is, as always, the keyword: using a macro definition will ensure it and also easiness in modifying the typesetting in the whole document, if switching from one option to another is needed.

2. Should limits go below the operator in display style?

Also this is debatable and conventions used in the field the document is written for should be followed.

3. Should the operator name be typeset upright?

Definitely, like all other operator/function names such as sine and cosine.

Available tools

1. amsmath

With amsmath one can do, in the preamble,

\DeclareMathOperator*{\argmin}{arg\,min} % thin space, limits underneath in displays
\DeclareMathOperator*{\argmin}{argmin} % no space, limits underneath in displays
\DeclareMathOperator{\argmin}{arg\,min} % thin space, limits on side in displays
\DeclareMathOperator{\argmin}{argmin} % no space, limits on side in displays

Of course, only one of these should be used. In the document it's sufficient to type \argmin and the spacing around the operator will be the right one according to the common rules of math typesetting.

The seemingly different

\newcommand{\argmin}{\operatornamewithlimits{argmin}}

is not really so, because it's essentially the same as \DeclareMathOperator*{\argmin}{argmin}; moreover it uses a deprecated command, that should be \operatorname*.

2. No package

The following definitions are very similar to the ones above, in the same order

\newcommand{\argmin}{\mathop{\mathrm{arg\,min}}
\newcommand{\argmin}{\mathop{\mathrm{argmin}}
\newcommand{\argmin}{\mathop{\mathrm{arg\,min}\nolimits}
\newcommand{\argmin}{\mathop{\mathrm{argmin}\nolimits}

With \underset

\underset{x}{\mathrm{argmin}}

This also was mentioned in the question and in one answer, but it is wrong, as the visual comparison will show.

3. Differences

The “no package” version is much more rigid than the amsmath version, because this package can receive the nonamelimits option that will automatically change all operators such as \lim, \max that usually have limits underneath (in displays) to have them on the side. The same option would of course act also on newly defined operators, provided \DeclareMathOperator* has been used.

Test document

In the following test document, the proposed definitions or constructions will be compared.

\documentclass{article}
\usepackage{amsmath}

% limits underneath
\DeclareMathOperator*{\argminA}{arg\,min} % Jan Hlavacek
\DeclareMathOperator*{\argminB}{argmin}   % Jan Hlavacek
\DeclareMathOperator*{\argminC}{\arg\min}   % rbp

\newcommand{\argminD}{\arg\!\min} % AlfC

\newcommand{\argminE}{\mathop{\mathrm{argmin}}}          % ASdeL
\newcommand{\argminF}{\mathop{\mathrm{argmin}}\limits}   % ASdeL

% limits on side
\DeclareMathOperator{\argminG}{arg\,min} % Jan Hlavacek
\DeclareMathOperator{\argminH}{argmin}   % Jan Hlavacek
\newcommand{\argminI}{\mathop{\mathrm{argmin}}\nolimits} % ASdeL

\newcommand{\cs}[1]{\texttt{\symbol{`\\}#1}}

\begin{document}

\begin{align}
&\cs{argminA}  & \argminA_x f(x) &= \{x \mid f(x) = \min_{x'} f(x')\} \\
&\cs{argminB}  & \argminB_x f(x) &= \{x \mid f(x) = \min_{x'} f(x')\} \\
&\cs{argminC}  & \argminC_x f(x) &= \{x \mid f(x) = \min_{x'} f(x')\} \\
&\cs{argminD}  & \argminD_x f(x) &= \{x \mid f(x) = \min_{x'} f(x')\} \\
&\cs{argminE}  & \argminE_x f(x) &= \{x \mid f(x) = \min_{x'} f(x')\} \\
&\cs{argminF}  & \argminF_x f(x) &= \{x \mid f(x) = \min_{x'} f(x')\} \\
&\cs{underset} & \underset{x}{\mathrm{argmin}} f(x) &= \{x \mid f(x) = \min_{x'} f(x')\}\\
&\cs{argminG}  & \argminG_x f(x) &= \{x \mid f(x) = \min_{x'} f(x')\} \\
&\cs{argminH}  & \argminH_x f(x) &= \{x \mid f(x) = \min_{x'} f(x')\} \\
&\cs{argminI}  & \argminI_x f(x) &= \{x \mid f(x) = \min_{x'} f(x')\}
\end{align}

\end{document}

enter image description here

Results

As predicted, many lines typeset the same. However, the macros defined with \mathop will not obey the nonamelimits option. Also the result of \argminD is clearly wrong, as the limit is typeset below “max” and not below the whole operator name.

The construction with \underset is wrong because it will not use the correct spacing after the operator: a thin space follows all others and it is the right way to typeset. Compare \sin x and \mathrm{sin}x to see this thin space in a different context.

Note that \argminC and \argminA give the same result, as well as \argminD and \argminB. Explanation: \arg and \max are already defined as operators, so TeX inserts a thin space if one directly follows another one. Using \argminC or \argminD just makes TeX spin its wheels a little more, with no advantage over \argminA or \argminB.


(3) This should be the accepted answer. - Alex
Isn't \newcommand{\argmin}{\operatornamewithlimits{argmin}} different in that it doesn't use amsmath package? - Homero Esmeraldo
(1) @HomeroEsmeraldo \operatornamewithlimits is an obsolete command defined by amsopn (a package loaded by amsmath, rarely used by itself). - egreg
(1) Thanks. For some reason only \underset gave satisfactory results for with (with x being under argmin, not adjacent to it). - Dan M.
what about how to make it work in Jupyter? - Charlie Parker
@CharlieParker What's Jupyter? If it uses MathJax, then it's not LaTeX. - egreg
In Python code documentation I used the underset method including a \; to get the right spacing. It is the best approach for Sphinx mathjax extension. - Attack68
@Attack68 MathJax is not TeX - egreg
Indeed, but this answer and my comment are helpful to Mathjax users who may stumble across it as I did when searching. There are limited, if any, answers to the same question in a Mathjax context. - Attack68
@Attack68 I just tried with MathJax on Math.SE and \DeclareMathOperator*{\argmax}{arg\,max} provides the expected result. Code (click) Output (click} - egreg
2
[+44] [2017-05-25 16:54:47] iamaziz

Use \usepackage{amsmath} and then:

\operatorname*{argmin}_\theta f(x)

enter image description here

Or similarly (for adding a space),

\operatorname*{arg\,max}_\theta f(x)

enter image description here


concise and to the point! - Shawn Azdam
(1) It works when used in \begin{equation} but not using $$ - martin36
That's awesome, doesn't require defining any new functions - K.Mat
3
[+34] [2012-05-22 19:55:15] Michael Litvin

I use \newcommand{\argmin}{\operatornamewithlimits{argmin}}.


(4) +1 I like this - why overcomplicate things? This looks exactly the same as the \DeclareMathOperator* answer. - zelanix
(5) This yields "argmin". To get "arg min" (arguably the most usual notation), set it to {arg\,min}. - Waldir Leoncio
(3) Google gave me 669,000 answers for "arg min" and 520,000 answers for "argmin", so I'd call it a tie. ;-) - PatrickT
this does work...? is there a missing import statement? - Charlie Parker
(1) Google Scholar got me 171,000 for "arg min" and 116,000 for "argmin". - Lori
4
[+9] [2013-11-27 23:15:12] alfC

Just another alternative (in some sense the poorer solution around, see comment below), could be to define \argmin in terms of \min and \arg commands.

\newcommand{\argmin}{\arg\!\min}

In this way, 1) \argmin will behave always the same way as \min, 2) doesn't need amsmath or care about \operator... commands 3) yes, the variable in not centered (it is centered in the min part), but that may even be what you want (since it is centered in the same ways a \min, also the 'g' in \arg doesn't further lower the under-argument).

\documentclass[fleqn]{article}
\newcommand{\argmin}{\arg\!\min}
\begin{document}

\[ \argmin_x f(x) = \{x | f(x) = \min_{x'} f(x')\} \]
\[ \min_x f(x) = \{f(x) | f(x) < f(x_0) \forall x_0 \in R \]

\end{document}

argmin


(5) This doesn't look like a good idea. \argmin_{x\in X\cap Y} f(x) will break it completely. - yo'
(1) What is the \arg command for anyway? - Thomas Ahle
(1) @ThomasAhle, arg is for the argument function (angle in the complex plane, $z = |z| e^{i \arg z}$. tohecz is right by the way. - alfC
@boycott.se-yo', good point. - alfC
5
[+7] [2021-02-18 05:03:08] stackoverflowuser2010

Using \underset does not require any other package. An above answer says:

With \underset

This also was mentioned in the question and in one answer, but it is wrong, as the visual comparison will show.

If the only thing "wrong" is that there is no space between the argmax and the f(x), then just add \, (thin space) to fix the problem:

z = \underset{x}{\mathrm{argmax}}f(x)

enter image description here

z = \underset{x}{\mathrm{argmax}}\, f(x)

enter image description here


6
[+6] [2013-09-19 13:07:10] drben

or you could use the underset command. For example:

    R = \underset{n} {\mathrm{argmax}} ~P(L_n|\mathbf{x})

(13) Please don't use this. This makes argmin a mathord rather than a mathop and the spacing is incorrect. Moreover, this will not automatically adjust the limits in inline math mode. - Aditya
7
[+2] [2015-09-21 13:35:50] ASdeL

I would use \mathop and \mathrm, and eventually \limits or \nolimits, depending on the desired behavior when combined with _ and ^. See below.

\documentclass{article}

\begin{document}

{
nothing:
\newcommand{\argmin}{\mathop{\mathrm{argmin}}}
$\argmin_a^b$
$$\argmin_a^b$$
}

{
limits:
\newcommand{\argmin}{\mathop{\mathrm{argmin}}\limits}
$\argmin_a^b$
$$\argmin_a^b$$
}

{
nolimits:
\newcommand{\argmin}{\mathop{\mathrm{argmin}}\nolimits}
$\argmin_a^b$
$$\argmin_a^b$$
}

\end{document}

8
[-2] [2015-12-20 20:12:07] Miae Kim

This works without any additional package. However it will create a new line.

$$\pi(s) = arg\max_\theta$$

(1) $$...$$ is not recommended to use with LaTeX. Better use \[ ... \] for correct vertical spacing. - Stefan Kottwitz
(5) This solution will not typeset argmax correctly. Here arg will be in italic (and with bad spacing), and max will be upright. - Håkon Marthinsen
9