Lua Latex: How to break a long line of elements of a set in inline mode?

Since you mention that you use LuaLaTeX, here's a solution that works by setting up (a) a Lua function that inserts \allowbreak instructions if a comma follows a ) character and (b) a LaTeX front-end macro called \AllowBreaks.

enter image description here

The vertical lines in the preceding screenshot denote the edges of the textblock.

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{showframe}
\usepackage{luacode} % for '\luaexec' and '\luastring' macros
\luaexec{
function AllowBreaks ( s )
  tex.sprint ( ( s:gsub ( "\%)\%s-," , "),\\hspace{1sp}\\allowbreak" ) )  )
end
}
\newcommand\AllowBreaks[1]{\luaexec{AllowBreaks(\luastring{#1})}}

\newcommand\LongString{test test test $A = \{ (1,2), (1,3), (1,5), (2,1), (2,2), (2,3), (1,2), (1,3), (1,5), (2,1), (2,2), (2,3),(1,2), (1,3), (1,5), (2,1), (2,2), (2,3), (1,2), (1,3), (1,5), (2,1), (2,2), (2,3),(1,2), (1,3), (1,5), (2,1), (2,2), (2,3) \}$ test test test test}

\begin{document}
\LongString

\bigskip
\AllowBreaks{\LongString}
\end{document}

Here is a possibility using a command called \mySpecialCommas to make commas active in math mode only when said command is used. When \mySpecialCommas is used inside a math formula, a line break after a comma is allowed if, and only if the comma is followed by a space token (which can be obtained from the end-of-line, as usual). So, make sure commas inside your tuples are not followed by a space. Beware of the final comma in your example, unless you are fine with the closing brace appearing at the beginning of a line. This should work with any engine.

\documentclass{article}
\usepackage{lipsum}

\makeatletter
\newcommand*{\my@specialComma}{%
  \futurelet\next\my@specialComma@next
}

\newcommand*{\my@specialComma@next}{%
  \mathchar "613B             % the normal mathematical comma
  \ifx\next\@sptoken
    \penalty 0                % higher values make a line break less desirable
    \mskip 0mu plus 2mu\relax % add stretchability for better line breaks
  \fi
}

{\catcode`\,=\active
 \gdef,{\my@specialComma}%
}

\newcommand*{\mySpecialCommas}{\mathcode`\,="8000 } %space intended
\makeatother

\begin{document}

\lipsum*[1][1-3]
This is a long equation: $\mySpecialCommas
A = \{ (1,2), (1,3), (1,5), (2,1), (2,2), (2,3), (1,2), (1,3), (1,5), (2,1),
(2,2), (2,3),(1,2), (1,3), (1,5), (2,1), (2,2), (2,3),(1,2), (1,3), (1,5),
(2,1), (2,2), (2,3),(1,2), (1,3), (1,5), (2,1), (2,2), (2,3) \}$.

\medskip
This is a long equation: $\mySpecialCommas
A = \{ (1,2), (1,3), (1,5), (2,1), (2,2), (2,3), (1,2), (1,3), (1,5), (2,1),
(2,2), (2,3),(1,2), (1,3), (1,5), (2,1), (2,2), (2,3),(1,2), (1,3), (1,5),
(2,1), (2,2), (2,3),(1,2), (1,3), (1,5), (2,1), (2,2), (2,3),\}$.

\medskip
Normal text can have commas, no problem.

\end{document}

enter image description here


This answer does not require lualatex, but instead relies on the tokcycle package to go through the token stream of the environment content and add \allowbreak after commas that fall outside of a paren group. Also, to mimic something of \sloppy input, it throws in some extra glue with spaces to allow stretching/compression (you can adjust the amounts).

\documentclass{article}
\usepackage[showframe,pass]{geometry}
\usepackage{tokcycle}
\Characterdirective{\ifx(#1\def\commaaction{}\else
  \ifx)#1\def\commaaction{\allowbreak}\fi\fi
  \addcytoks{#1}\ifx,#1\addcytoks[1]{\commaaction}\fi}
\Spacedirective{\addcytoks{\hspace{0pt plus 2pt minus 1pt}#1}}
\begin{document}
\tokencyclexpress
$A = \{ (1,2), (1,3), (1,5), (2,1), (2,2), (2,3), (1,2), (1,3), (1,5), (2,1), (2,2), (2,3),(1,2), (1,3), (1,5), (2,1), (2,2), (2,3),(1,2), (1,3), (1,5), (2,1), (2,2), (2,3),(1,2), (1,3), (1,5), (2,1), (2,2), (2,3), \}$
\endtokencyclexpress
\end{document}

enter image description here