# Preventing entries in brackets from going over margins [duplicate]

How can I prevent the following text from exceeding the margins of my document?

\documentclass{article}

\begin{document}

We first left the DC loop open and inputted AC at frequencies of $\{50, 51, 67, 95, 102, 127, 147, 150\}$ Hz into the AC loop. Text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text.

\end{document}


Output:

• the brackets are not involved it is just that linebreaks are not allowed at commas by default. It would be easier to test examples if you provided a full example that produces the output shown. – David Carlisle Oct 4 '18 at 23:01
• Thanks, @DavidCarlisle. Is there a way to allow line breaks at commas? – Vivek Subramanian Oct 4 '18 at 23:02
• see GuM's answer, I would define \newcommand\foo{,\linebreak[0] } then use \foo rather than ,  between your numbers. – David Carlisle Oct 4 '18 at 23:17

You could define a command as follows:

\newcommand\mycomma{,\allowbreak}


And then write

We first left the DC loop open and inputted AC at a
frequencies of $\{50\mycomma 51\mycomma 67\mycomma 95\mycomma 102\mycomma 127\mycomma 147\mycomma 150\}$ Hz
into the AC loop.


Here's a version that works with the catcode of commas:

\begingroup
\catcode\,=\active
\gdef,{\normalcomma\formattingcode}%
\endgroup

\newcommand\myspecialcommagroup[1]{%%
\begingroup
\let\normalcomma=,
\def\formattingcode{\allowbreak}%%
\catcode\,=\active
\scantokens{#1}%%
\endgroup}


Then

We first left the DC loop open and inputted AC at a
frequencies of  \myspecialcommagroup{$\{50, 51, 67, 95, 102, 127, 147, 150\}$}


or

We first left the DC loop open and inputted AC at a
frequencies of  $\{\myspecialcommagroup{50, 51, 67, 95, 102, 127, 147, 150}\}$


will produce

If you accept a quick-n-dirty solution that needs to be adjusted by hand, the following simple trick will do:

% My standard header for TeX.SX answers:
\documentclass[a4paper]{article} % To avoid confusion, let us explicitly
% declare the paper format.

\usepackage[T1]{fontenc}         % Not always necessary, but recommended.
% End of standard header.  What follows pertains to the problem at hand.

\newcommand*{\allowinlinmathbreak}{\penalty \exhyphenpenalty}

\begin{document}

We first left the DC loop open and inputted AC at a frequencies of $\{50, 51, 67,\allowinlinmathbreak 95, 102, 127, 147, 150\}$ Hz into the AC loop.

\end{document}


A better solution would be to make the comma active in math mode and to define it to insert a comma followed by the \penalty item, but I haven’t got time to write it now (surely somebody else will before tomorrow!).

Well, in the end I’ve found the time to develop my idea:

% My standard header for TeX.SX answers:
\documentclass[a4paper]{article} % To avoid confusion, let us explicitly
% declare the paper format.

\usepackage[T1]{fontenc}         % Not always necessary, but recommended.
% End of standard header.  What follows pertains to the problem at hand.

\newcommand*{\allowinlinemathbreak}{\penalty \exhyphenpenalty}
\newcommand*{\allowinlinemathbreakatcommas}{\mathcode\,="8000}
\newcommand*{\oldcomma}{} % to reserve the name
\edef\oldcomma{\the\mathcode,}
\begingroup
\lccode\~ = \,
\lowercase{\endgroup
\def~{\mathchar\oldcomma\allowinlinemathbreak}}

\begin{document}

We first left the DC loop open and inputted AC at a frequencies of
$\allowinlinemathbreakatcommas \{50, 51, 67, 95, 102, 127, 147, 150\}$
Hz into the AC loop.

The meaning of the comma is changed only locally:

We first left the DC loop open and inputted AC at a frequencies of
$\{50, 51, 67,\allowinlinemathbreak 95, 102, 127, 147, 150\}$
Hz into the AC loop.

\end{document}


Anyway, although I have to admit that your question does make for an amusing puzzle, I cannot fully understand why you insist on placing the numbers inside a set in math mode, instead of simply writing the numbers separated by commas and spaces in ordinary text: is it a notation meant to suggest that the unit of measure, “Hz”, should be distributed among them?