2

I'm having trouble using the align environment to stack up equations for a linear program. The issue I'm having is that the contents of the center column in the top row are so long that they push the lower rows into the margin and affect the alignment of the row numbers. What I'd like to do is maybe put in extra sub-columns in the lower rows and/or join them such that the "for each" statements are aligned, but under the overhang of the top equation. I tried a handful of commands, but no success. Here is the current state of affairs...

By the way... I'm pretty much a noob at LaTex. I only have reason to use it occasionally, and I usually throw code at it till it works, so assume nothing in your answer! :)

\usepackage{amsmath}

\begin{flalign}
max  \quad &Z=\sum_{d \in D} w_1*dep_d - \sum_{V, a \in G^v} w_2*x_{v,a} \sum_{S, a \in G^s} w_3*x_{s, a}\\
s.t. \quad & x_{s,a} \le x_{v,a}                             &\forall s \in S, v \in V, a \in CN\\
           &count_{cfg} * x_{v,a} \le \sum_{s \in S} x_{s,a} &\forall v \in V, a \in CN, cfg \in C_d
\end{flalign}

enter image description here

4
  • 1
    Welcome to TeX.SE!
    – Mensch
    Commented Aug 5, 2022 at 16:38
  • 1
    Aha!!! Just discovered the magic of \span after trying several other incantations. It works. Will answer my own question, which I'm hesitant to do, in case it helps others w/ same.
    – AirSquid
    Commented Aug 5, 2022 at 16:46
  • What does * represent? Ordinary multiplication? Something else? Please advise.
    – Mico
    Commented Aug 5, 2022 at 16:57
  • guilty. It is (was) multiplication. cleaned up in solution below. Still buffing out the dents. :)
    – AirSquid
    Commented Aug 5, 2022 at 17:18

3 Answers 3

2

(Remark: I modified this answer to incorporate a couple of pieces of information provided by the OP, e.g., that a - symbol was missing between of the summation terms in row 1, and that * symbols denote multiplication and aren't strictly needed.)

Here's a solution that employs an align environment and a single & alignment marker per row.

enter image description here

\documentclass{article} % or some other suitable document class
\usepackage{mathtools}  % for \smashoperator macro; loads amsmath automatically
\newcommand\vn[1]{\textit{#1}} % 'variable name'

\begin{document}

\begin{align}
\max\quad &Z= \sum_{d \in D} w_1\,\vn{dep}_d 
             -\smashoperator{\sum_{v,a\in G^v}} w_2\,x_{v,a}
             -\smashoperator{\sum_{s,a\in G^s}} w_3\,x_{s,a}\\
\textup{s.t.}\quad 
          & x_{s,a} \le x_{v,a} 
            \qquad\forall s \in S,\ v \in V,\ a \in \vn{CN}\\
          & \vn{count}^{}_{\vn{cfg}} \, x_{v,a} \le \sum_{s \in S} x_{s,a} 
            \qquad\forall v \in V,\ a \in \vn{CN},\ \vn{cfg} \in C_d
\end{align}
\end{document}
5
  • This is great, thanks. The asterisks weren't needed and I dropped a minus sign in editing... it is/was work in progress, and I edited my solution. (Was too excited that \span worked). Appreciate your suggestions. I'm mentally parsing... This stuff is a deeper swamp than regex!
    – AirSquid
    Commented Aug 5, 2022 at 17:17
  • the textup and vn are awesome tweaks. I take it the #1 is a spacing command... will have to investigate & experiment and risk learning something here.
    – AirSquid
    Commented Aug 5, 2022 at 17:28
  • @AirSquid - \vn is a macro that takes one argument, and #1 is a placeholder for that argument. The name \vn is meant to signal that it's supposed to be applied to variable names. E.g., compare the appearance of "cfg" in your posting and mine. In your solution, the combination of the letters c, f, and g is treated as the product of variables called c, f, and g; note the big space between f and g. In my solution, the individual letters are spaced tightly, as befits them being one (abbreviated?) word.
    – Mico
    Commented Aug 5, 2022 at 17:37
  • I'll go ahead and modify my answer to get rid of the parentheses and insert the misssing - symbol.
    – Mico
    Commented Aug 5, 2022 at 17:38
  • 1
    ahhh. thanks. I got the 1 argument thing, I just didn't know the syntax connection in tex to associate that with the #1. got it now. And yes, I saw the compaction, which is desirable. Good hack!
    – AirSquid
    Commented Aug 5, 2022 at 17:42
2

After fumbling w/ multispan and others without success, I stumbled on span which works and allows the long segment to "span" the last column.

This works fine:

    \begin{flalign}
max  \quad &Z=\sum_{d \in D} w_1dep_d - \sum_{V, a \in G^v} w_2x_{v,a} -\sum_{S, a \in G^s} w_3x_{s, a}\span\\
s.t. \quad & x_{s,a} \le x_{v,a}                             &\forall s \in S, v \in V, a \in CN\\
           &count_{cfg} x_{v,a} \le \sum_{s \in S} x_{s,a} &\forall v \in V, a \in CN, cfg \in C_d
\end{flalign}

enter image description here

2

I think you should use the dedicated package for optimisation problems – optidef:

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

        \begin{document}

    \begin{maxi!}{}
    {Z=\sum_{d \in D} w_1\cdot\operatorname{dep}_d - \sum_{V, a \in G^v} w_2\cdot x_{v,a} \sum_{S, a \in G^s} w_3*x_{s, a}}{}{}
    \addConstraint{x_{s,a}}{ \le x_{v,a}}{\forall s \in S, v \in V, a \in CN}
    \addConstraint{\operatorname{count}_{cfg}\cdot x_{v,a}}{ \le \sum_{s \in S} x_{s,a}\quad}{ \forall v \in V, a \in CN, %
    cfg \in C_d}
    \end{maxi!}

    \end{document} 

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .