# Auto-format equations with many for all conditionals

For an Atom package, I'm trying to display equations resulting from a GAMS model as Latex output (similar to how it's done in Hydrogen for regular Latex commands). Consider the following GAMS equation:

Objf.. NetReturn
=E=
Sum((Pixel,CropRot,Mana)
$DataPrd(Pixel,CropRot,Mana,"NReturn"), DataPrd(Pixel,CropRot,Mana,"NReturn") * CropProd(Pixel,CropRot,Mana)) + Sum((Pixel,CropRot,Mana,PolPrem)$(DataPrd(Pixel,CropRot,Mana,"NReturn") and
PolData(Mana,PolPrem)),
PolData(Mana,PolPrem) *
CropProd(Pixel,CropRot,Mana))
;


From the gams2tex converter, I get the following output:

$$NetReturn = \sum\limits_{Pixel,CropRot,Mana\thinspace \operatorname{if} \thinspace DataPrd_{Pixel,CropRot,Mana,\mathrm{NReturn}} } \left( DataPrd_{Pixel,CropRot,Mana,\mathrm{NReturn}} \cdot CropProd_{Pixel,CropRot,Mana} \right) + \sum\limits_{Pixel,CropRot,Mana,PolPrem\thinspace \operatorname{if} \thinspace DataPrd_{Pixel,CropRot,Mana,\mathrm{NReturn}} and PolData_{Mana,PolPrem} } \left( PolData_{Mana,PolPrem} \cdot CropProd_{Pixel,CropRot,Mana} \right) \mspace{40mu} \label{eq:Objf}$$


This output can (and has to be) altered, as it is now overflowing the page is not quiet readable. In the gams2tex converter, the output is generated through Dust.js templates from the abstract syntax tree.

{<equation}
{~n}
{@getGamsSymbol data=structure field="desc" property=name/}
{~n}{~n}
\begin{~lb}equation{~rb}{~n}{~s}{~s}{~s}{~s}
{+"{leftHand.type}" exp=leftHand setMode="index" /}
{operator|s}
{+"{rightHand.type}" exp=rightHand setMode="index" /}
\mspace{40mu}
{?setsAndConditions.sets}{~n}{~s}{~s}{~s}{~s}\forall\thinspace{~lb}{+sets sets=setsAndConditions.sets conditions=setsAndConditions.conditions setMode="set" /}{~rb}{/setsAndConditions.sets}
{~n}{~s}{~s}{~s}{~s}
\label{~lb}eq:{name}{~rb}{~n}
\end{~lb}equation{~rb} {~n}{~n}{~n}
{/equation}


A main issue regarding the equations resulting from a conversion from GAMS is the relatively high amount of conditionals in the equation. My first idea was to use the breqn package to automatically break the equation where ever suitable. Also, I wanted to strip away the condtionals (forall or if) and have them in a seperate table below the actual equation:

\begin{dmath}
NetReturn = \sum\limits_{Pixel,CropRot,Mana ^{1}} \left( DataPrd_{Pixel,CropRot,Mana,\mathrm{NReturn}}  \cdot CropProd_{Pixel,CropRot,Mana} \right)  +  \sum\limits_{Pixel,CropRot,Mana,PolPrem ^{2}} \left( PolData_{Mana,PolPrem}  \cdot CropProd_{Pixel,CropRot,Mana} \right)
\label{eq:Objf}
\end{dmath}

where
\begin{align*}
1 &= \forall \ DataPrd_{Pixel,CropRot,Mana,\mathrm{NReturn}} \\
2 &= \forall \ DataPrd_{Pixel,CropRot,Mana,\mathrm{NReturn}}  \text{ and } PolData_{Mana,PolPrem}
\end{align*}


This output improves readability of the equation (in my opinion), but I'm not absolutely sure if it's the best way of targeting the issue. Also, as the export is automated through the use of Dust.js templates, I don't know how I could automate the creation of the table and the reference symbols in the equation (1, 2 in the example). The solutions needs to be rather generic, and should work for all type of equations.

I would really appreciate if someone had a good idea on how to solve this issue!

Update: Here's a MWE:

\documentclass[a4paper,landscape]{article}
\usepackage[landscape]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{breqn}
\setkeys{breqn}{breakdepth={1}}

\begin{document}

% The output generated by the GAMS to Latex converter
% without any changes
$$NetReturn = \sum\limits_{Pixel,CropRot,Mana\thinspace \operatorname{if} \thinspace DataPrd_{Pixel,CropRot,Mana,\mathrm{NReturn}} } \left( DataPrd_{Pixel,CropRot,Mana,\mathrm{NReturn}} \cdot CropProd_{Pixel,CropRot,Mana} \right) + \sum\limits_{Pixel,CropRot,Mana,PolPrem\thinspace \operatorname{if} \thinspace DataPrd_{Pixel,CropRot,Mana,\mathrm{NReturn}} and PolData_{Mana,PolPrem} } \left( PolData_{Mana,PolPrem} \cdot CropProd_{Pixel,CropRot,Mana} \right) \mspace{40mu} \label{eq:Objf}$$

% My first proposal, however I don't know how I could automate this setup
% through a Dust.js template
% I'd really welcome better approaches

\begin{dmath}
NetReturn = \sum\limits_{Pixel,CropRot,Mana ^{1}} \left( DataPrd_{Pixel,CropRot,Mana,\mathrm{NReturn}}  \cdot CropProd_{Pixel,CropRot,Mana} \right)  +  \sum\limits_{Pixel,CropRot,Mana,PolPrem ^{2}} \left( PolData_{Mana,PolPrem}  \cdot CropProd_{Pixel,CropRot,Mana} \right)
\label{eq:Objf}
\end{dmath}

where
\begin{align*}
1 &= \forall \ DataPrd_{Pixel,CropRot,Mana,\mathrm{NReturn}} \\
2 &= \forall \ DataPrd_{Pixel,CropRot,Mana,\mathrm{NReturn}}  \text{ and } PolData_{Mana,PolPrem}
\end{align*}

\end{document}

• Welcome. Could you please add a complete code with \documentclass{ and \end{document} – Bobyandbob Apr 19 '18 at 10:22
• Sure, I updated the questions with a MWE. Thanks for having a look! – Christoph Pahmeyer Apr 19 '18 at 11:09