# Number a linear program on the left-hand side

I want to write a linear program which is numbered on the right-hand side, like it normally is, but which also has a number on the left-hand side. It will be more of a label instead of a number because I want it to have the letter (P) or any other kind of label. I already have written some code, only I don't know how to add the (P) label to the left. How can I do this?

This is the code:

$$\label{lp:primal} \begin{array}{ll@{}ll} \text{minimize} & \displaystyle\sum\limits_{j=1}^{n} &c_{j}x_{j} &\\ \text{subject to}& \displaystyle\sum\limits_{j=1}^{n} &a_{ij}x_{j} \geq b_{i}, &i=1 ,..., m,\\ & &x_{j} \geq 0, &j=1 ,..., n. \end{array}$$


And this is how it should look like:

In order to position the left-side label, I've used some zref magic from I want to indent the next line by an exactly specified position, and therefore the solution requires at least two compilations in order for the references to settle:

\documentclass{article}
\usepackage{amsmath,xparse,zref-savepos}
\makeatletter
% Some ideas taken from https://tex.stackexchange.com/a/69076/5764
\@ifundefined{zsaveposx}{\let\zsaveposx\zsavepos}{}
\newcounter{hposcnt}
\renewcommand*{\thehposcnt}{hpos\number\value{hposcnt}}
\NewDocumentCommand{\lplabel}{o m}{%
\stepcounter{hposcnt}%
\zsaveposx{\thehposcnt l}%
\zref@refused{\thehposcnt l}%
\zref@refused{hpos0l}%
\makebox[0pt][r]{\makebox[\dimexpr\zposx{\thehposcnt l}sp-\zposx{hpos0l}sp][l]{#2}}%
\IfNoValueF{#1}
{\def\@currentlabel{#2}\ltx@label{#1}}
}
\makeatother
\AtBeginDocument{\zsaveposx{hpos0l}}

\begin{document}

Consider the linear program~\ref{lp:primal} give by
\begin{alignat}{2}
\lplabel[lp:primal]{(P)}\text{subject to} \quad \sum_{j=1}^n a_{ij}x_{j} & \geq b_i, & \quad & i = 1,\dots,m, \\
x_{j} & \geq 0,   & \quad & j = 1,\dots,n.
\end{alignat}

\end{document}


The above example provides \lplabel[<label>]{<stuff>} that prints <stuff> against the left text block boundary and can be referenced using \ref{<label>} (if the first optional argument is supplied).

Here is a toned-down version which sets the LP label to the left of where it's placed by a distance of 2em:

\documentclass{article}
\usepackage{amsmath,xparse}
\makeatletter
\NewDocumentCommand{\lplabel}{o m}{%
\makebox[0pt][r]{#2\hspace*{2em}}%
\IfNoValueF{#1}
{\def\@currentlabel{#2}\ltx@label{#1}}
}
\makeatother

\begin{document}

Consider the linear program~\ref{lp:primal} give by
\begin{alignat}{2}
\lplabel[lp:primal]{(P)}\text{subject to} \quad \sum_{j=1}^n a_{ij}x_{j} & \geq b_i, & \quad & i = 1,\dots,m, \\
x_{j} & \geq 0,   & \quad & j = 1,\dots,n.
\end{alignat}

\end{document}


showframe provided the left/right boundary.

• Thank you very much for your response. I am still trying to figure out some parts of your code, but it is just what I needed and I only need one compilation. Just out of curiosity: can I change the position of the label, for instance to make it closer to the text? – Jessy09 Jun 25 '14 at 7:56
• @Jessy09: If you want it closer to the text then it's easier to just use \makebox[0pt][r]{(P)\hspace*{<len>}} where you specify <len> (say 50pt or 2em...). You can place this just before \text{subject to}. It won't work in the same reference-style way, so you'll have to type (P) manually in the text, rather than using \ref{lp:primal}. – Werner Jun 25 '14 at 14:02
• I understand. Then I will leave it as it is because the referencing is more important. I just wanted to know how it can be done. Thank you for answering. – Jessy09 Jun 25 '14 at 14:07
• @Jessy09: I've added an alternative. – Werner Jun 25 '14 at 16:46

Another solution needn't two compilations. It is simply \hbox / \vbox arithmetic.

\documentclass{article}

\usepackage{amsmath}
\newdimen\prevdp
\def\leftlabel#1{\noalign{\prevdp=\prevdepth
\kern-\prevdp\nointerlineskip\vbox to0pt{\vss\hbox{#1}}\kern\prevdp}}

\begin{document}

\noindent Consider the linear program~(P) given by
\begin{alignat}{2}