I'm generating math worksheets dynamically with pyLaTeX, and stumbled upon a weird behaviour that I don't quite find how to correct.
I have a code that puts all the answers for an exercise into a string, then places it in the solution environment from the LaTeX exam document type.
This works perfectly for everything except when my answers are all fractions. When this happens, the solutions environment doesn't automatically do line breaks as it should, and the fractions bleed to the right eternally instead.
It feels extra weird to me given it even worked properly with polynomials (it even breaks them in the middle if it needs to).
The code is the same in both examples: the only difference is the math text ($answer$) I put after the bold letter. It looks kinda like
a:~$stuff$; b:~$stuff$; ... on the code side (not even using graphix here).
Edit: After trying the solution by Don Hosek, my fraction solutions do break lines (and my polynomials break more often at their end), but I still get sometimes polynomials broken at the middle, and the fractions still bleed sometimes to the right:
a:~$stuff$; \penalty-200 b:~$stuff$; ...
Edit2: Okay after Przemysław's comment I went to extract the tex code itself (I was generating the pdf directly from python and couldn't really see the middle step unless there was a tex error), and just realized I was grouping the solution like
$part part part$ instead of
$part$ $part$ $part$.
Now I've split it and I get what Don meant with "ragged right" (and also the "grouping issues" thing. Thanks to both!