0

In the following example, I was wondering how to "properly" eliminate (or reduce) the horizontal spacing between "minimize"/"subject to" and the corresponding equations, while keeping the alignment at the first letters of the two words, and at the equal signs, in addition, all equations numbered.

By "properly", I suggest not to use negative horizontal spacing \hspace{-...}.

Thanks.

\documentclass{article} 
\usepackage{amsmath}
\begin{document}
    \begin{align}
        &\text{minimize} &S(f) &=\int_{-\infty}^{\infty} f^{\prime\prime}(x)^2 \, \text{d}x  \label{eq:min-energy} \\
        &\text{subject~to} &f(x_i) &= y_i, \enspace \text{for} \; i=1,\ldots,m  \label{eq:interp-cond} \\
        & &f^{\prime\prime}(x_1) &= f^{\prime\prime}(x_m)=0  \label{eq:natural-bc} \\
        & &f &\in C^2(-\infty,\infty).  \label{eq:C2-constraint}
    \end{align}
\end{document}

enter image description here

AEW
  • 819

3 Answers3

4

This puts the text left aligned in constant width boxes to the left of the alignment point. \mathllap overlaps part of this space, also using the alignment point. The choice of the width is a question of aesthetics.

\documentclass{article} 
\usepackage{mathtools}
\usepackage{showframe}% alignment tool
\begin{document}
    \begin{align}
        \makebox[1.25in][l]{minimize}\mathllap{S(f)} &=\int_{-\infty}^{\infty} f^{\prime\prime}(x)^2 \, \text{d}x  \label{eq:min-energy} \\
        \makebox[1.25in][l]{subject~to}\mathllap{f(x_i)} &= y_i, \enspace \text{for} \; i=1,\ldots,m  \label{eq:interp-cond} \\
        f^{\prime\prime}(x_1) &= f^{\prime\prime}(x_m)=0  \label{eq:natural-bc} \\
        f &\in C^2(-\infty,\infty).  \label{eq:C2-constraint}
    \end{align}
\end{document}

Frankly, I prefer a flalign solution:

\documentclass{article} 
\usepackage{mathtools}
\usepackage{showframe}% alignment tool
\begin{document}
    \begin{flalign}
        &\text{minimize} & S(f) &=\int_{-\infty}^{\infty} f^{\prime\prime}(x)^2 \, \text{d}x  \label{eq:min-energy} && \\
        &\text{subject~to} & f(x_i) &= y_i, \enspace \text{for} \; i=1,\ldots,m  \label{eq:interp-cond} && \\
        && f^{\prime\prime}(x_1) &= f^{\prime\prime}(x_m)=0  \label{eq:natural-bc} && \\ %actually, on needed in one line
        && f &\in C^2(-\infty,\infty).  \label{eq:C2-constraint}
    \end{flalign}
\end{document}
John Kormylo
  • 79,712
  • 3
  • 50
  • 120
3

If you don't want the automatic spacing inserted by align, you should have a look at alignat. (Others have done a better job than I can explaining how to use it, so I will just include an example below of what I think has relatively pleasant spacing.)

\documentclass{article} 
\usepackage{amsmath}
\begin{document}
\begin{alignat}{10}
        &\text{minimize} &S(f) &=\int_{-\infty}^{\infty} f^{\prime\prime}(x)^2 \, \text{d}x  \label{eq:min-energy} \\
        &\text{subject~to}\; &f(x_i) &= y_i, \enspace \text{for} \; i=1,\ldots,m  \label{eq:interp-cond} \\
        & &f^{\prime\prime}(x_1) &= f^{\prime\prime}(x_m)=0  \label{eq:natural-bc} \\
        & &f &\in C^2(-\infty,\infty).  \label{eq:C2-constraint}
    \end{alignat}
\end{document}

Notes:

  1. After \text{subject~to} I inserted \;. Because of the f'' on the next line, I find that without the extra space it looks a bit cramped. You may decide otherwise.
  2. I specified 10 as the mandatory argument for alignat because I am lazy; see the link for explanation.

My personal opinion, however, is that it is better in this case not to align at the equality signs. I think something like

\begin{alignat}{10}
    &\text{minimize} &&S(f) =\int_{-\infty}^{\infty} f^{\prime\prime}(x)^2 \, \text{d}x  \label{eq:min-energy} \\
    & &&f(x_i) = y_i, \enspace \text{for} \; i=1,\ldots,m  \label{eq:interp-cond} \\
    &\text{subject to}\, \smash{\left\lbrace\vphantom{\begin{array}{l} f \\ f \\ f\end{array}} \right.} &&f^{\prime\prime}(x_1) = f^{\prime\prime}(x_m)=0  \label{eq:natural-bc} \\
    & &&f \in C^2(-\infty,\infty).  \label{eq:C2-constraint}
\end{alignat}

looks better.

enter image description here

(There are other methods of getting a large brace spanning multiple lines in an alignat, the one given above is a bit of a hack that nonetheless doesn't require any additional packages. If you are willing to be less lazy, the \vphantom{\begin{array}... \end{array}} can be replaced with \rule{0pt}{<height>} with a well-chosen height; 5.5ex for the height seems to work ok.)

Willie Wong
  • 24,733
  • 8
  • 74
  • 106
2

You can use optidef package to reduce the horizontal spacing between "minimize"/"subject to" and the corresponding equations. Here there is a example.

\documentclass[a4paper,12pt]{article}
\usepackage{optidef}
\usepackage{amsmath,amssymb}
\begin{document}
\begin{mini!}<b>
{}{S(f) =\int_{-\infty}^{\infty} f''^2(x) \, \textup{d}x  \label{eq:min-energy}}{}{}
\addConstraint {f(x_i)}{= y_i,}{ \textup{for} \; i=1,\ldots, m  \label{eq:interp-cond} }
\addConstraint { f''(x_1) }{= f''(x_m)=0  \label{eq:natural-bc}}{}
\addConstraint { f}{\in C^2(-\infty,\infty).  \label{eq:C2-constraint}}{}
\end{mini!}
\end{document}

enter image description here

Sebastiano
  • 54,118