[Tex/LaTex] Splitting a set over two lines

line-breaking

I am trying to include the following in my document:

$$\mathcal{C} = \{84, 156, 204, 228, 276, 312, 348, 372, 408, 440, 444, 456, 468, 492, 555, 696, 708, 732, 744, 780, 804, 816, 828, 852, 876, 888, 912, 920, 936, 948, 952, 984, 996\}$$.

However, it continues on one line. How do I fix this so it splits into multiple lines? If possible, I would like the parenthesis to surround all lines.

Best Answer

You can let TeX compute the line breaks.

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}

\usepackage{lipsum} % just for the example

\ExplSyntaxOn
\NewDocumentCommand{\longset}{mm}
 {
  \begin{gathered}
  \egreg_longset:nn { #1 } { #2 }
  \end{gathered}
 }

\box_new:N \l_egreg_longset_set_box
\seq_new:N \l_egreg_longset_elements_seq

\cs_new_protected:Nn \egreg_longset:nn
 {
  % measure the left hand side (with the open brace)
  \hbox_set:Nn \l_egreg_longset_set_box { $#1=\lbrace\,$ }
  % typeset the left hand side and the open brace
  #1 = \lbrace\,
  % add a minipage, the width is \displaywidth, minus the left hand side
  % minus 4em to have space for the equation number
  \begin{minipage}[t]
   {
    \dim_eval:n { \displaywidth - \box_wd:N \l_egreg_longset_set_box - 4em }
   }
  % add some stretching to \thinmuskip
  \muskip_set:Nn \thinmuskip { 1\thinmuskip plus 5mu }
  % split the input at commas
  \seq_set_from_clist:Nn \l_egreg_longset_elements_seq { #2 }
  % use the items, adding back the comma, followed by a penalty
  % so a line break can be taken
  $\seq_use:Nn \l_egreg_longset_elements_seq { , \penalty0~ }\,\rbrace$
  % the end
  \end{minipage}
 }
\ExplSyntaxOff

\begin{document}

\lipsum*[2]
\begin{equation*}
\longset{\mathcal{C}}{
  84, 156, 204, 228, 276, 312, 348, 372, 408, 440, 444, 456, 468, 492,
  555, 696, 708,732, 744, 780, 804, 816, 828, 852, 876, 888, 912, 920,
  936, 948, 952, 984, 996
}
\end{equation*}
\lipsum*[3]
\begin{equation}
\longset{\mathcal{C}}{
  84, 156, 204, 228, 276, 312, 348, 372, 408, 440, 444, 456, 468, 492,
  555, 696, 708,732, 744, 780, 804, 816, 828, 852, 876, 888, 912, 920,
  936, 948, 952, 984, 996
}
\end{equation}
\lipsum[4]

\end{document}

enter image description here