# How can I DeclareMathOperator* with spacing after it, preserving the alignment?

In the context of optimization problems, I'm using

\usepackage{amsmath}
\DeclareMathOperator*{\minimize}{\text{minimize}}
\DeclareMathOperator*{\subjto}{\text{subject to}}


to declare some operators to be used as \minimize_{x, \beta}, etc.

These work:

However, to get the correct spacing, I have to use

\minimize_{x, \beta} \quad& x^T x - 2\beta \\
\subjto \quad& \beta > 0 \\
\subjto \quad& \sum_{i=1}^{n} x_i^2 < 1


I don't want to have to use manual \quad spacing, for obvious reasons: convenience, elegance, flexibility (what if I want \  or \qquad later?), etc.

I tried

• using \DeclareMathOperator*{\minimize}{\text{minimize}\quad}, but this causes the subscript spacing of x, \beta to be wrong—it's shifted to the right because the box is wider.
• using \DeclareMathOperator*{\temp@minimize}{\text{minimize}} with \newcommand{\minimize}[0]{\temp@minimize\quad} (with appropriate catcodes), but this didn't work because the operator doesn't take a command, it expects a subscript.
• using a similar version with [1] argument, failing for the same reason.
• using a similar version with \def, failing for the same reason.

Ideas? What's the most elegant way to do this?

-

You can set the problem in an array to adjust the spacing. For consistency, it's best to define this as an environment:

\documentclass{article}
\usepackage{amsmath,array,environ}
\DeclareMathOperator*{\minimize}{\text{minimize}}
\DeclareMathOperator*{\subjto}{\text{subject to}}
\newcolumntype{R}{>{\displaystyle}r}
\newcolumntype{L}{>{\displaystyle}l}
\newenvironment{problem}
{\renewcommand{\arraystretch}{1.5}%
{\end{array}}
\begin{document}

\begin{align*}
\minimize_{x, \beta} \quad& x^T x - 2\beta \\
\subjto \quad& \beta > 0 \\
\subjto \quad& \sum_{i=1}^{n} x_i^2 < 1
\end{align*}

$\begin{problem} \minimize_{x, \beta} & x^T x - 2\beta \\ \subjto & \beta > 0 \\ \subjto & \sum_{i=1}^{n} x_i^2 < 1 \end{problem}$

\end{document}


The latter option allows you to adjust the column separation \quad "in bulk".

-
Are the \text{...} wrappers in the second argument of \DeclareMathOperator* needed (or possibly even counterproductive)? If the wrapper must be kept, it's probably better to change it to \textup{...}; this avoids setting minimize and subject to in italics if the material happens to occur in a chunk -- e.g., the body of a theorem -- that's set in italics. – Mico Jul 20 '14 at 5:59
@Mico: Great comment. Perhaps up to discretion of the user. – Werner Jul 20 '14 at 6:20

You could simply not use the \text{...} "wrapper" in the second argument of \DeclareMathOperator*} macro. Instead, just write

\newcommand\myspacer{\quad}
\DeclareMathOperator*{\minimize}{minimize\myspacer}
\DeclareMathOperator*{\subjto}{subject\ to\myspacer}


The macro \myspacer is set to \quad for now. If you wish to change it to \qquad or \, that's easily done, isn't it?

A separate advantage of not using the \text wrapper is that the strings "minimize" and "subject to" will always be typeset in upright roman even if the chunk of code in which they may appear happens to be typeset in italics -- as may happen, say, in the body of a theorem.

Finally, I suggest writing x^T\! x instead of just x^T x. Doing so reduces the (in my opinion unsightly) visual "gap" between the transpose symbol and the second x.

\documentclass{article}
\usepackage{amsmath}

Look at the subscript on "minimize," though—the $x, \beta$ is shifted to the right because its centering includes the space. This is what I was trying to avoid. Thanks for the hint about the transpose spacing, though. (I really should define a \transpose to do that.) – WChargin Jul 20 '14 at 14:19