[Tex/LaTex] Environment to allow line breaks at “,” in math mode

environmentsline-breakingmath-mode

I find the question and accepted answer to Allowing line break at , in inline math mode? very useful, BUT I would like to make an environment (e.g. "allowbreaks") out of it. The problem is that I'm not able to. Let me explain the idea:

\begin{allowbreaks}
Commas are modified in such a way that they allow breaks in
inline mathmode $(a,b,c)$ only within this environment.
\end{allowbreaks}

Is it possible somehow, please?

edit: I renamed the environment to allowbreaks, because of naming conflicts with the command allowbreak.

Best Answer

You can locally modify the behavior of the comma, allowing a break. The trick is to make it "math active":

\documentclass{article}
\newenvironment{allowbreaks}
  {\mathactivatecomma
   \mathcode`\,=\string"8000
   \ignorespaces}
  {\ignorespacesafterend}

\newcommand{\mathactivatecomma}{%
  \begingroup\lccode`~=`\,
  \lowercase{\endgroup\edef~}{\mathchar\the\mathcode`\,\penalty0 }}

\begin{document}
\parbox{1cm}{abc def
\begin{allowbreaks}
$a,b,c,d,0,1,2,3,4,5,6,7$
\end{allowbreaks}
haaa 
$a,b,c,d,0,1,2,3,4,5,6,7$}
\end{document}

You'll see that the first math formula is broken, while the second one isn't.

However, the method proposed in this answer of mine is, in my opinion, better: use \mathlist{a,b,c,d} for a list of math variables that can be broken at commas.

Related Question