jeslago / optidef

A Latex library for optimization problems
57 stars 3 forks source link

upper and lower bound variable constraints #30

Open gretarsson opened 5 years ago

gretarsson commented 5 years ago

Hi

I am really enjoying your package, but I would like to see a feature (or maybe it is already implemented, though I could not find any documentation on it) for having constraints defined with upper and lower bounds that aligns (aesthetically) well with typical constraints. Here is an example.

\begin{mini} {v}{c^Tv}{}{} {\label{OP:FBA}} \addConstraint{Sv}{=0}{} \addConstraint{L \leq}{v \leq U}{} \end{mini}

I think it would be advantageous if the "v" in the second constraint would align with the "=' for the first constraint. Such constraints are abundant in optimization problems using in cell metabolism modelling.

jeslago commented 5 years ago

This can be done using either the alignment format 0 or 1 (Section 6.1 in the manual)

gretarsson commented 5 years ago

\usepackage[short]{optidef}
\begin{mini}[0 % or 1] {v}{c^Tv}{}{} {\label{OP:FBA}} \addConstraint{Sv}{=0}{} \addConstraint{L \leq}{v \leq U}{} \end{mini}

It is not very pretty. It is not aligned and one will have to do a bit of manual spacing to have consistent spacing between \leq and v, and \geq and v.

Using alignment format = 3 gives consistent spacing between \leq and v, and \geq and v, but does not align v with the equal sign in the constraint above.

jeslago commented 5 years ago

I see what you mean. To be honest, at the moment I have no clue with format 3 gives consistent spacing and 1 and 2 do not.

I will look into into, but I can't say when the issue will be resolved so I am afraid for now you will need to use manual spacing.

gretarsson commented 5 years ago

That is completely fine. I just wanted to make you aware of this as it would be quite convenient. Thank you for your quick response.

jeslago commented 5 years ago

I have have checked the issue. It is easier to understand but I have no clue on how to fix it. So, underneath, the optidef package uses the alignat environment.

When using align format 2 or 3, the only alignment point is located at the beginning of the equations (right after subject to). In that case, all mathematical symbols insert a space around them and that space is respected.

When using align format 0 or 1, there are alignment points in all the points equations are split. In that case, the usual spaces after splitting the equation get deleted. You can understand that by comparing your code with this one:

\begin{mini}[1]
    {v}{c^Tv}{}{}
    {\label{OP:FBA}}
    \addConstraint{Sv=}{0}{}
    \addConstraint{L \leq v}{\leq U}{}
\end{mini}

In this, the space between the 0 and the equal sign gets deleted because any space after {Sv=} disappears. In the case before, it was the space after {L \leq}.

To be honest, I have no clue of why this happens. This is the code that goes underneath:

\begin{alignat}{5}
\text{subject to\quad}&&Sv&=0\\
&&  L \leq &v\leq U
\end{alignat}

As you can see, the code above has the same issue. To the best of knowledge, I don't know how to fix it. If you are able to fix the problem with the alignat environment, fixing it within optidef would be easy.