4

I am having the following isssue: I have to write a mathematical paper/book with many different optimization problems formulated.

To be consistent all over I'd like to create an environment to typeset these opt problems. I'll give you a minimal running example:

\documentclass[a4paper,10pt]{scrartcl}
\usepackage{amsmath,amsfonts,amssymb}
\usepackage{environ}
\begin{document}

\NewEnviron{OptEnv}[1]{%
\begin{align}
\text{min. } & #1 \\
\text{s.t. } & \BODY
\end{align}
}

\begin{OptEnv}{a}
a \leq 1 \\
& b \leq a \\
& 3 \leq b
\end{OptEnv}

\end{document}

My question now: Is there an easy way to omit the & in the environemnt and append in the definition of the environment on each \\?

Thanks

David Carlisle
  • 757,742

3 Answers3

8

Better method with expl3

If you're sure that every \\ in the environment has to be replaced, you can do it with a regex replacement:

\documentclass[a4paper,10pt]{scrartcl}
\usepackage{amsmath,amssymb}

\ExplSyntaxOn \NewDocumentEnvironment{OptEnv}{mb} { \tl_set:Nn \l_tmpa_tl { #2 } \regex_replace_all:nnN { \c{\} } { \c{\} \cT& } \l_tmpa_tl \begin{align} \text{min.~} & #1 \ \text{s.t.~} & \tl_use:N \l_tmpa_tl \end{align} } \ExplSyntaxOff

\begin{document}

\begin{OptEnv}{a} a \leq 1 \ b \leq a \ 3 \leq b \end{OptEnv}

\end{document}

enter image description here

Old answer

If you're sure that every \\ in the environment has to be replaced, you can do it with xstring:

\documentclass[a4paper,10pt]{scrartcl}
\usepackage{amsmath,amssymb}
\usepackage{environ,xstring}

\NewEnviron{OptEnv}[1]{% \noexpandarg % don't expand arguments % with \expandafter get the expansion of \BODY \expandafter\StrSubstitute\expandafter{\BODY}{\}{\&}[\BODY] \begin{align} \text{min. } & #1 \ \text{s.t. } & \BODY \end{align} }

\begin{document}

\begin{OptEnv}{a} a \leq 1 \ b \leq a \ 3 \leq b \end{OptEnv}

\end{document}

egreg
  • 1,121,712
  • I want to apply a standard set of replacements in math (alignat) mode. '->' to '\rightarrow', '=' but not '==' to '&&=' Tree to textbf{Tree} and a bunch of other keywords like Tree. Can this be made to work for that? If its different enough to this I can make another question – Rusi Jul 16 '22 at 04:38
  • 1
    @RusiYes, it’s different enough. Please add use cases to your new question. – egreg Jul 16 '22 at 07:46
  • 1
    Thanks @egreg. Done here – Rusi Jul 16 '22 at 13:46
4

If you encase \BODY in a top-aligned, left-justified array environment, there's no further need to provide & alignment indicators:

enter image description here

\documentclass[a4paper]{scrartcl}
\usepackage{amsmath,amsfonts,amssymb,environ}
\setlength\textwidth{3in} % just for this example
\NewEnviron{OptEnv}[1]{%
  \begin{align}
  \min\  & #1 \\
  \text{s.t.\ } & \begin{array}[t]{@{}l} \BODY \end{array}
  \end{align}
  }
\begin{document}
\begin{OptEnv}{a}
  a \leq 1 \\
  b \leq a \\
  3 \leq b
\end{OptEnv}
\end{document}

If you would rather have the conditions centered vertically, just leave off the [t] specifier.

A consequence of this setup is that the condition, or conditions, associated with the minimization problem will be assigned a single equation number instead of one number per condition. This may, or may not, be to your liking.

egreg
  • 1,121,712
Mico
  • 506,678
4

hacking a bit into amsmath one can avoid loading further packages:

environ

\documentclass[a4paper,10pt]{scrartcl}
\usepackage{amsmath,amsfonts,amssymb}
\usepackage{environ}

\makeatletter
\NewEnviron{OptEnv}[1]{%
  \def\Let@{\def\\{\math@cr &}}%
  \begin{align}
  \text{min. } & #1 \math@cr
  \text{s.t. } & \BODY
  \end{align}
}
\makeatother

\begin{document}\thispagestyle{empty}

\begin{OptEnv}{a}
a \leq 1 \\
b \leq a \\
3 \leq b
\end{OptEnv}

\end{document}
  • You're allowed to fix all posts in a thread that exhibits the \\ site bug. – egreg Jun 03 '17 at 09:59
  • @egreg oh I missed that. But it looks as if I have fixed all my posts now. I wasn't such an over-active poster... ;-) –  Jun 03 '17 at 10:01
  • Good for you! Mine will take a bit longer. ;-) What I wanted to point out is that also the other answers here were affected by the bug and you could have edited them as well. – egreg Jun 03 '17 at 10:02
  • @egreg David appears to have almost finished it about 125 to go ;-) –  Jun 03 '17 at 10:02
  • @egreg If I had known, I would have waited that you fix all the posts in all the threads you contributed too ;-) that would have surely taken care of a great deal of mine ... –  Jun 03 '17 at 10:06