# Optimization formulas in LaTeX

This is the code I'm using for my optimization problem.

$$\begin{array}{rrclcl} \displaystyle \min_{w,b,\xi} & \multicolumn{3}{l}{\frac{1}{2}w^{t}w+C\sum_{i=1}^{N}{\xi_{i}}}\\ \textrm{s.t.} & y_{i}(w\phi(x_{i}+b))+\xi_{i}-1\\ &\xi\geq0 \\ \end{array}$$


This is the result:

I want third argument (\xi\geq0) under yi. How can I do that? How can I add i=1 and N under and above summation?

• Sorry. Third argument. I edited question. Mar 27, 2015 at 12:11

this is very easy to accomplish using the aligned environment from amsmath:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{aligned} \min_{w,b,\xi} \quad & \frac{1}{2}w^{t}w+C\sum_{i=1}^{N}{\xi_{i}}\\ \textrm{s.t.} \quad & y_{i}(w\phi(x_{i}+b))+\xi_{i}-1\\ &\xi\geq0 \\ \end{aligned}
\end{document}


(in the future, please make your code a full compilable example, from \documentclass through \end{document}.)

• Thank you for answer and comment. How can I left hand side all the question block? It is more appropriate for optimization problems in articles. Mar 27, 2015 at 12:17
• the option fleqn applied to either the document class or to amsmath will shift the display blocks to the left. Mar 27, 2015 at 12:22
• @barbarabeeton Is there a way to split the objective function up over multiple rows? I tried to do just this over three rows, and the vertical distance between the first and second row is much larger than that between the second and third row.
– M B
Nov 11, 2020 at 7:50

An even easier approach would be to use the package optidef:

\documentclass{article}
\usepackage{optidef}
\begin{document}
\begin{mini*}|s|
{w,b,\xi}{\frac{1}{2}w^{t}w+C\sum_{i=1}^{N}{\xi_{i}}}
{}{}
\end{mini*}
\end{document}


and get the exactly same output:

The advantage of the package is that you can easily change the alignment of the constraints using four different formats, use a long format for the problem description, or add any referencing of your like. For example, the code:

\documentclass{article}
\usepackage{optidef}
\begin{document}
\begin{mini!}|l|[3]
{w,b,\xi}{\frac{1}{2}w^{t}w+C\sum_{i=1}^{N}{\xi_{i}}}
{}{}
\end{mini!}
\end{document}


would produce three changes:

1. A long format: given by using |l| instead of |s|.

2. Multiple referencing: given by using \begin{mini!} instead of \begin{mini*}.

3. Put the constraints below the "subject to": given by using [3] instead of default.

In addition, the package also provides other features like line breaking line, various ways of referencing equations, or other environments for defining maximizition or arg mini problems.

A post explaining more about the package can be found here. The full documentation here and the repository with issue tracker here.

• To clarify, "Multiple referencing" means the objective and constraints each get their own indices, for instance (1a), (1b), (1c). Dec 10, 2019 at 3:04
• This package has problem when constraints are long and need to break.
– G.T.
May 17, 2020 at 8:19
• @G.T. The constraint can be split in multiple lines to avoid going beyond the margin. See github.com/jeslago/optidef/issues/26 May 18, 2020 at 9:28

The align environment from amsmath also helps you achieve this with ease, like so:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{align}
& \min_{w,b,\xi} & & \frac{1}{2}w^{t}w+C\sum_{i=1}^{N}{\xi_{i}} \nonumber \\
& \textrm{s.t.} & & y_{i}(w\phi(x_{i}+b))+\xi_{i}-1 \label{opt_prob:misc} \tag{42}\\
& & & \xi\geq0 \nonumber \\
\end{align}
\end{document}


The important thing to note is that in the align environment, the alignment switches between left aligned and right aligned for consecutive columns. Therefore, I simply added an empty column to change the alignment of the expressions to left aligned.

Furthermore, while working in a jupyter notebook I found that align lets me use labels with the equations, unlike the aligned environment. This is the reason I prefer align over the other.

Edit: You can use the same script in markdown cells in Jupyter notebooks, except that you only need to include the part between the align statements.

• Wow! I wasn't aware that you can align more than one element in the align environment but it seems (from the double &) that you can! Thanks for the info! Sep 16, 2022 at 9:21