6

I have a long listing of 2-uple elements like $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), \}$ using inline math mode. LuaLaTeX isn't breaking in multiple lines. How can I force it to break in a comma not inside the tuple, otherwise the set elements goes off page?

The solution proposed at Allowing line break at ',' in inline math mode? didn't work. Seems to break polyglossia.

  • If the list is automatically generated, the easiest thing to do would be to make it so that the commas where you want to allow a break are followed by \penalty 0 or someting like that. – frougon Apr 6 at 7:26
  • @Lin just to clarify that is not a LuaLaTeX thing. TeX will not break at commas by default in math mode (as it would be bad to break at the comma inside (1,2). Instead you need to help TeX by telling it which commas are good to break at. This is done via adding \allowbreak after the relevant commas. – daleif Apr 6 at 8:57
4

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

| improve this answer | |
4

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}
| improve this answer | |
  • The third upvote of this answer raised my rep count on this site past 333,333. Many thanks! – Mico Apr 7 at 8:36
2

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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.