3

I'm trying to properly align a simple linear Program: Right now I'm using

\usepackage{amsmath}
\begin{document}
\begin{equation*}
\begin{array}{r r c r c r}
\displaystyle \min &x_1 &+&x_2\\
\text{s.t.} & 10x_1& +&10x_2 &\leq &0\\
&111x_1 &+&x_2 &\geq &0\\
& x_1& &,x_2 & \geq & 0
\end{array}
\end{equation*}
\end{document}

yielding: enter image description here

But I would like to 'gather' the last constraint in such a way, that it is aligned to the right with all other 'x_2' but without the space between 'x_1' and ','. Is there a way to omit the space without interfering the other constraints?

5

I'd also avoid the large spaces between the columns.

\documentclass{article}
\usepackage{amsmath,array}


\begin{document}

\begin{equation*}
\setlength{\arraycolsep}{0pt}
\begin{array}{r @{\quad} r >{{}}c<{{}} r >{{}}c<{{}} r}
\min &x_1 &+&x_2\\
\text{s.t.} & 10x_1& +&10x_2 &\leq &0\\
&111x_1 &+&x_2 &\geq &0\\
\multicolumn{4}{r}{x_1,x_2} & \geq & 0
\end{array}
\end{equation*}

\end{document}

enter image description here

With \setlength{\arraycolsep}{0pt} we tell LaTeX not to add padding around columns; we override this between first and second column with @{\quad}; for the columns containing relation or operation symbols, I use

>{{}}c<{{}}

which means that {} and {} will be added around the actual cell entry; this way, we get, for instance {}+{} which makes TeX to add the usual space around the operation symbol; similarly for relations.

2
  • Thanks you! Would you be so kind to briefly explain what the arguments for array do in this case? – Max_M Dec 14 '20 at 13:32
  • @Max_M Added as requested – egreg Dec 14 '20 at 14:16
3

You also can have a whole bunch of layouts and referencing with the dedicated package, optidef. Here is the basic one, with a very simple syntax:

\documentclass{article}
\usepackage[short]{optidef}

\begin{document}

\begin{mini*}{}{x_1 + x_2}
{}{}
\addConstraint{10x_1 + 10x_2}{\leq 0}
\addConstraint{111x_1 + 10x_2}{\geq 0}
\addConstraint{x_1,x_2}{\geq 0}
\end{mini*}

\end{document} 

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.