Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I need to write a mathematical programming model composed of several constraints. Constraints are represented as two-columns rows: the first column is the equation and the second is its domain, e.g.

x_i > l_i & \forall i \in S

But both columns can be quite long so if on one hand it would be preferrable to have all of them aligned horizontally in some way, on the other one the colum ns may end up being splitted vertically or printed overruning other elements of the page.

I thought then that a decent solution would be to use one align-like environment so that each first column is aligned to the others and each second column is aligned to the others as well. However if no space for the eq. number is left on the line then I would like the second column, i.e. the domain, to be right-aligned so to fill the space after the first column to avoid the eq. number to be printed on the new line.

Consider the following

\documentclass[a4,13pt,reqno,twoside, openright]{article}
\usepackage[utf8x]{inputenc}
\usepackage{float}
\usepackage{subfig}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{amsthm}
\usepackage{amsfonts}
\usepackage{amssymb}

\allowdisplaybreaks

\textwidth = 14cm
 \hoffset = -1.5cm
 \voffset = -2.0cm

\begin{document}
\begin{subequations}
\begin{align}
& y^D_{tzgm} \le y_{tzgm} & \forall t \in T, z \in Z, g \in G_z, m \in \overline{M}_{zg}
\\
  & x_{tzg} \leq \sum_{m \in M_{zg}} P_{zgm} \cdot y_{tzgm} + \sum_{m \in \bar M_{zg}} (P^D_{zgm} - P_{zgm}) \cdot y^D_{tzgm} & \forall t \in T, z \in Z, g \in G_z\\
  &x_{tzg} \geq \sum_{m \in M_{zg}} p_{zgm} \cdot y_{tzgm} + \sum_{m \in \bar M_{zg}} (p^D_{zgm} - p_{zgm}) \cdot y^D_{tzgm} & \forall t \in T, z \in Z, g \in G_z\\
  & o_{1zh} = v_{0h}  &\forall z \in Z, h \in H_z\\
  & o_{(|T|+1)zh} = v_{(|T|+1)h} & \forall z \in Z, h \in H_z\\
  & o_{tzh} + n_{th} + \eta_{h} \cdot m_{tzh} = o_{(t+1)zh} + s_{tzh} + l_{tzh} & \forall t \in T, z \in Z, h \in H_z
\end{align}
\end{subequations}

\end{document}

enter image description here

As you can see I get every eq. number on a new line because the first inequality has a too long domain. An approximation of the result I would like, obtained by adding space by hand, is

    \documentclass[a4,13pt,reqno,twoside, openright]{article}
\usepackage[utf8x]{inputenc}
\usepackage{float}
\usepackage{subfig}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{amsthm}
\usepackage{amsfonts}
\usepackage{amssymb}

\allowdisplaybreaks

\textwidth = 14cm
 \hoffset = -1.5cm
 \voffset = -2.0cm

\begin{document}
\begin{subequations}
\begin{align}
& y^D_{tzgm} \le y_{tzgm} \qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad \mathrlap{\forall t \in T, z \in Z, g \in G_z, m \in \overline{M}_{zg}}
\\
  & x_{tzg} \leq \sum_{m \in M_{zg}} P_{zgm} \cdot y_{tzgm} + \sum_{m \in \bar M_{zg}} (P^D_{zgm} - P_{zgm}) \cdot y^D_{tzgm} & \forall t \in T, z \in Z, g \in G_z\\
  &x_{tzg} \geq \sum_{m \in M_{zg}} p_{zgm} \cdot y_{tzgm} + \sum_{m \in \bar M_{zg}} (p^D_{zgm} - p_{zgm}) \cdot y^D_{tzgm} & \forall t \in T, z \in Z, g \in G_z\\
  & o_{1zh} = v_{0h}  &\forall z \in Z, h \in H_z\\
  & o_{(|T|+1)zh} = v_{(|T|+1)h} & \forall z \in Z, h \in H_z\\
  & o_{tzh} + n_{th} + \eta_{h} \cdot m_{tzh} = o_{(t+1)zh} + s_{tzh} + l_{tzh} & \forall t \in T, z \in Z, h \in H_z
\end{align}
\end{subequations}

\end{document}

enter image description here

On a second note, do you think there are better ways to typeset these models?

TIA

share|improve this question

4 Answers 4

up vote 2 down vote accepted

I just placed the first line label in a zero-width, right-aligned box.

\documentclass[a4,13pt,reqno,twoside, openright]{article}
\usepackage[utf8x]{inputenc}
\usepackage{float}
\usepackage{subfig}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{amsthm}
\usepackage{amsfonts}
\usepackage{amssymb}

\allowdisplaybreaks

\textwidth = 14cm
 \hoffset = -1.5cm
 \voffset = -2.0cm

\begin{document}
\begin{subequations}
\begin{align}
& y^D_{tzgm} \le y_{tzgm} & \makebox[0pt][r]{$\forall t \in T, z \in Z, g \in G_z, m \in \overline{M}_{zg}$}
\\
  & x_{tzg} \leq \sum_{m \in M_{zg}} P_{zgm} \cdot y_{tzgm} + \sum_{m \in \bar M_{zg}} (P^D_{zgm} - P_{zgm}) \cdot y^D_{tzgm} & \forall t \in T, z \in Z, g \in G_z\\
  &x_{tzg} \geq \sum_{m \in M_{zg}} p_{zgm} \cdot y_{tzgm} + \sum_{m \in \bar M_{zg}} (p^D_{zgm} - p_{zgm}) \cdot y^D_{tzgm} & \forall t \in T, z \in Z, g \in G_z\\
  & o_{1zh} = v_{0h}  &\forall z \in Z, h \in H_z\\
  & o_{(|T|+1)zh} = v_{(|T|+1)h} & \forall z \in Z, h \in H_z\\
  & o_{tzh} + n_{th} + \eta_{h} \cdot m_{tzh} = o_{(t+1)zh} + s_{tzh} + l_{tzh} & \forall t \in T, z \in Z, h \in H_z
\end{align}
\end{subequations}

\end{document}

enter image description here

share|improve this answer

Since you don't really want alignment you could just use gather and force the conditions apart with fill glue, except as noted here you can't normally use \hfill in ams alignments, so need to tweak it a bit first.

to keep the code down this redefines gather it would of course be possible to make a new environment based on this , leaving the original gather as it was.

enter image description here

\documentclass[a4,13pt,reqno,twoside, openright]{article}
\usepackage[utf8x]{inputenc}
\usepackage{float}
\usepackage{subfig}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{amsthm}
\usepackage{amsfonts}
\usepackage{amssymb}

\allowdisplaybreaks

\textwidth = 14cm
 \hoffset = -1.5cm
 \voffset = -2.0cm

\makeatletter
\def\set@gather@field{%
    \iftagsleft@
        \global\lineht@\ht\z@
    \else
        \global\lineht@\dp\z@
    \fi
    \kern\eqnshift@
    \unhbox\z@
    \hfil
}

\def\gather@#1{%
    \ingather@true \let\split\insplit@
    \let\tag\tag@in@align \let\label\label@in@display
    \chardef\dspbrk@context\z@
    \intertext@ \displ@y@ \Let@
    \let\math@cr@@@\math@cr@@@gather
    \gmeasure@{#1}%
    \global\shifttag@false
    \tabskip\z@skip
    \global\row@\@ne
    \halign to\displaywidth\bgroup
        \strut@
        \setboxz@h{$\m@th\displaystyle##$}%
        \calc@shift@gather
        \set@gather@field
        \tabskip\@centering
       &\setboxz@h{\strut@{##}}%
        \place@tag@gather
        \tabskip \iftagsleft@ \gdisplaywidth@ \else \z@skip \span\fi
        \crcr
        #1%
}

\makeatother


\begin{document}
\begin{subequations}
\begin{gather}
 y^D_{tzgm} \le y_{tzgm} \quad\hfill \forall t \in T, z \in Z, g \in G_z, m \in \overline{M}_{zg}
\\
   x_{tzg} \leq \sum_{m \in M_{zg}} P_{zgm} \cdot y_{tzgm} + \sum_{m \in \bar M_{zg}} (P^D_{zgm} - P_{zgm}) \cdot y^D_{tzgm} \quad\hfill\forall t \in T, z \in Z, g \in G_z\\
  x_{tzg} \geq \sum_{m \in M_{zg}} p_{zgm} \cdot y_{tzgm} + \sum_{m \in \bar M_{zg}} (p^D_{zgm} - p_{zgm}) \cdot y^D_{tzgm} \quad\hfill\forall t \in T, z \in Z, g \in G_z\\
   o_{1zh} = v_{0h} \quad\hfill\forall z \in Z, h \in H_z\\
   o_{(|T|+1)zh} = v_{(|T|+1)h} \quad\hfill\forall z \in Z, h \in H_z\\
   o_{tzh} + n_{th} + \eta_{h} \cdot m_{tzh} = o_{(t+1)zh} + s_{tzh} + l_{tzh} \quad\hfill\forall t \in T, z \in Z, h \in H_z
\end{gather}
\end{subequations}

\end{document}
share|improve this answer

You can define a particular command for this purpose:

\documentclass[a4paper]{article}
\usepackage[textwidth=14cm,showframe]{geometry}
\usepackage{amsmath}

\newcommand{\eqcond}[3][3em]{%
  \makebox[\dimexpr\displaywidth-#1][s]{%
    $\displaystyle#2\hfill#3$}%
}

\allowdisplaybreaks

\begin{document}
\begin{subequations}
\begin{align}
&\eqcond
   {y^D_{tzgm} \le y_{tzgm}}
   {\forall t \in T, z \in Z, g \in G_z, m \in \overline{M}_{zg}}
\\
&\eqcond
   {x_{tzg} \leq \sum_{m \in M_{zg}} P_{zgm} \cdot y_{tzgm} + 
    \sum_{m \in \bar M_{zg}} (P^D_{zgm} - P_{zgm}) \cdot y^D_{tzgm}}
   {\forall t \in T, z \in Z, g \in G_z}
\\
&\eqcond
   {x_{tzg} \geq \sum_{m \in M_{zg}} p_{zgm} \cdot y_{tzgm} + 
    \sum_{m \in \bar M_{zg}} (p^D_{zgm} - p_{zgm}) \cdot y^D_{tzgm}}
   {\forall t \in T, z \in Z, g \in G_z}
\\
&\eqcond
   {o_{1zh} = v_{0h}}
   {\forall z \in Z, h \in H_z}
\\
&\eqcond
   {o_{(|T|+1)zh} = v_{(|T|+1)h}}
   {\forall z \in Z, h \in H_z}
\\
&\eqcond
   {o_{tzh} + n_{th} + \eta_{h} \cdot m_{tzh} = 
    o_{(t+1)zh} + s_{tzh} + l_{tzh}}
   {\forall t \in T, z \in Z, h \in H_z}
\end{align}
\end{subequations}

\end{document}

There is an optional argument to \eqcond; with \eqcond[6em]{...}{...} you get reduced width of the box. With \eqcond[0em] you'd get full width (and the equation number would be pushed below the line.

I used geometry, that's better than tampering with \hoffset and \voffset; the showframe option is just for showing the margins.

One could also add a check for an overfull line.

enter image description here

share|improve this answer

Another solution consists in placing the quantifiers on a line below (unnumbered) only when necessary. This can be done with the flalignenvironment in two ways, according to the alignment of the quantifiers. I took the opportunity to suppress extraneous space after the sigma, due to extra-large subscripts.

        \documentclass[a4,11pt,reqno,twoside, openright]{article}
        \usepackage[utf8x]{inputenc}
        \usepackage{graphicx}
        \usepackage{mathtools}
        \usepackage{amsfonts}
        \usepackage{amssymb}
        \usepackage[textwidth = 14cm, nomarginpar, showframe]{geometry}%
        \allowdisplaybreaks

        \begin{document}

        \begin{subequations}
        \begin{flalign}
        \hspace{4em}  &  & y^D_{tzgm} & ≤ y_{tzgm}  &\forall t \in T, z &\in Z, g \in G_z, m \in \overline{M}_{zg} \\%
          & & x_{tzg} &\leq \mathrlap{\sum_{\mathclap{m \in M_{zg}}} P_{zgm} · y_{tzgm} + \sum_{\mathclap{m \in \bar M_{zg}}} (P^D_{zgm} - P_{zgm}) · y^D_{tzgm}}\\[-12pt]
        \notag &  &  &  &   & \forall t \in T, z \in Z, g \in G_z \\
         & & x_{tzg} & \geq \mathrlap{\sum_{\mathclap{m \in M_{zg}}} p_{zgm} · y_{tzgm} + \sum_{\mathclap{m \in \bar M_{zg}}} (p^D_{zgm} - p_{zgm}) · y^D_{tzgm}}\\[-12pt]
         \notag &  &  &  &  &\forall t \in T, z \in Z,  g \in G_z \\
         &   & o_{1zh}  & = v_{0h}  & & \forall z \in Z, h \in H_z \\
         &   & o_{(|T|+1)zh} &  = v_{(|T|+1)h} & & \forall z \in Z, h\in H_z \\
        &   & \mathllap{o_{tzh} + n_{th} + η_{h} · m_{tzh}}  & =\mathrlap{ o_{(t+1)zh} + s_{tzh} + l_{tzh} } &  & \forall t \in T, z \in Z, h \in H_z
        \end{flalign}
        \end{subequations}

        \begin{subequations}
        \begin{flalign}
          &  & y^D_{tzgm}   &  ≤ y_{tzgm}  & \hspace{8em}&\  \mathllap{\forall t \in T, z \in Z, g \in G_z, m \in \overline{M}_{zg}} \\%
          & & x_{tzg} &\leq \mathrlap{\sum_{\mathclap{m \in M_{zg}}} P_{zgm}   · y_{tzgm} + \sum_{\mathclap{m \in \bar M_{zg}}} (P^D_{zgm} - P_{zgm})   · y^D_{tzgm}}\\[-12pt]
        \notag &  &  &  &   & \mathllap{\forall t \in T, z \in Z, g \in G_z} \\
         & & x_{tzg}  & \geq \mathrlap{\sum_{\mathclap{m \in M_{zg}}} p_{zgm} · y_{tzgm} + \sum_{\mathclap{m \in \bar M_{zg}}} (p^D_{zgm} - p_{zgm}) · y^D_{tzgm}}\\[-12pt]
         \notag &  &  &  &  & \mathllap{\forall t \in T, z \in Z, g \in G_z} \\
         &   & o_{1zh}  & = v_{0h}  & & \mathllap{∀z \in Z, h \in H_z} \\
         &   & o_{(|T|+1)zh} &  = v_{(|T|+1)h} & &  \mathllap{\forall z \in Z, h \in H_z} \\
        &   & \mathllap{o_{tzh} + n_{th} + η_{h} · m_{tzh}} & =\mathrlap{ o_{(t+1)zh} + s_{tzh} + l_{tzh} } & & \mathllap{\forall t \in T, z \in Z, h \in H_z}
        \end{flalign}
        \end{subequations}
        \end{document} 

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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