# tex capacity exceeded when using breqn package with a separate macro

I am trying to use a nice solution for one problem I have which is to break long math sequence with commas in them as given here allowing-line-break-at-in-inline-math-mode only to to find I get tex capacity exceeded when I put it in my main code which happens to also need the breqn package. There seems to be a conflict which is beyond my understanding to resolve. The solution works when I do not use the breqn package, but I need this package.

here is MWE

\documentclass[11pt]{article}%
\usepackage{breqn}   %do not use this with tex4ht.

%code from https://tex.stackexchange.com/questions/1959/allowing-line-break-at-in-inline-math-mode/309558#309558
\makeatletter
\def\old@comma{,}
\catcode\,=13
\def,{%
\ifmmode%
\old@comma\discretionary{}{}{}%
\else%
\old@comma%
\fi%
}
\makeatother
\begin{document}
$\frac{1}{2},\frac{3}{5},\frac{8}{13},\frac{21}{34},\frac{55}{89}, \frac{144}{233},\frac{377}{610},\frac{987}{1597},\frac{2584}{4181}, \frac{6765}{10946},\frac{17711}{28657},\frac{46368}{75025}, \frac{121393}{196418},\frac{317811}{514229},\frac{832040}{1346269}, \frac{2178309}{3524578},\frac{5702887}{9227465}, \frac{14930352}{24157817},\frac{39088169}{63245986},\frac{102334155}{165580141}$
\end{document}


Then

pdflatex foo.tex

This is pdfTeX, Version 3.14159265-2.6-1.40.16 (TeX Live 2015) (preloaded format=pdflatex)
restricted \write18 enabled.
entering extended mode
(./foo.tex
LaTeX2e <2015/10/01> patch level 2
Babel <3.9m> and hyphenation patterns for 79 languages loaded.
(/usr/local/texlive/2015/texmf-dist/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
(/usr/local/texlive/2015/texmf-dist/tex/latex/base/size11.clo))
(/usr/local/texlive/2015/texmf-dist/tex/latex/breqn/breqn.sty
(/usr/local/texlive/2015/texmf-dist/tex/latex/l3kernel/expl3.sty
(/usr/local/texlive/2015/texmf-dist/tex/latex/l3kernel/expl3-code.tex)
(/usr/local/texlive/2015/texmf-dist/tex/latex/l3kernel/l3pdfmode.def))
(/usr/local/texlive/2015/texmf-dist/tex/latex/breqn/flexisym.sty
(/usr/local/texlive/2015/texmf-dist/tex/latex/breqn/cmbase.sym)
(/usr/local/texlive/2015/texmf-dist/tex/latex/breqn/mathstyle.sty))
(/usr/local/texlive/2015/texmf-dist/tex/latex/graphics/keyval.sty)
(/usr/local/texlive/2015/texmf-dist/tex/latex/tools/calc.sty)) (./foo.aux)
! TeX capacity exceeded, sorry [input stack size=5000].
,->\ifmmode \old@comma
\discretionary {}{}{}\else \old@comma \fi
l.18 $\frac{1}{2}, \frac{3}{5},\frac{8}{13},\frac{21}{34},\frac{55}{89}, ! ==> Fatal error occurred, no output PDF file produced! Transcript written on foo.log.  log file Here is how much of TeX's memory you used: 7740 strings out of 493054 142814 string characters out of 8884759 167252 words of memory out of 8000000 11311 multiletter control sequences out of 15000+600000 6379 words of font info for 23 fonts, out of 8000000 for 9000 1141 hyphenation exceptions out of 8191 5000i,3n,40p,10354b,275s stack positions out of 5000i,500n,10000p,30000000b,80000s ! ==> Fatal error occurred, no output PDF file produced!  Question is: What do I need to change to make the above macro works and still use breqn package? • Making the comma globally active is not really a great idea. – egreg May 14 '16 at 16:56 ## 1 Answer With breqn all characters are math active, so the solution is not going to work; but we can exploit the features of breqn: \documentclass{article} \usepackage{breqn} \newcommand{\splitatcommas}[1]{% \begingroup \begingroup\lccode~=, \lowercase{\endgroup \expandafter\def\expandafter~\expandafter{~\penalty0 \hspace{0pt plus 1em}}% }#1% \endgroup } \begin{document} \setlength{\lineskiplimit}{2pt}\setlength{\lineskip}{3pt} % for this particular case$\splitatcommas{
\frac{1}{2},\frac{3}{5},\frac{8}{13},\frac{21}{34},\frac{55}{89},
\frac{144}{233},\frac{377}{610},\frac{987}{1597},\frac{2584}{4181},
\frac{6765}{10946},\frac{17711}{28657},\frac{46368}{75025},
\frac{121393}{196418},\frac{317811}{514229},\frac{832040}{1346269},
\frac{2178309}{3524578},\frac{5702887}{9227465},
\frac{14930352}{24157817},\frac{39088169}{63245986},\frac{102334155}{165580141}
}\$

\end{document}
`