# How to prevent bleeding with fractions in solution environment

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).

Any suggestion?

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!

• Could you add MWE with the code including at least the source of the two above examples? Jan 2 at 13:39

Tbe problem here is that you don't really have a lot of good breakpoints in your first example. I would make two recommendations: First set the solutions ragged right by putting \SolutionEmphasis{\raggedright} in the document preamble and second, add \penalty-100 before each letter for a sub-part so that LaTeX will be encouraged to break lines there (the mid-expression breakpoints in your polynomial factorizations aren't very reader-friendly).

• This made the fractions end up in multiple lines as expected (and the polynomial factorizations break in the middle a lot less frequently), but it doesn't fully solve the problem (the fractions still bleed a bit through the square sometimes, and the polynomial factorizations still get broken in the middle from time to time). Making the penalty higher (-200, -400) doesn't seem to make it better. Jan 2 at 13:15
• I’m on my phone right now but it looks like the \raggedright isn’t taking effect. I’m guessing it’s an issue with grouping in the solution environment. Try putting \par at the end and see if that makes things ragged right. I’ll look at the source code for the class later and see if there’s a better solution. Jan 2 at 15:33
• Right on spot. I didn't realize I wasn't splitting the math environment so as to embrace each part with its own environment, so it was just basically one huge equation I guess. Now it leaves more space between parts and I think breaks always as it should. Thanks! Jan 8 at 17:51

I just solved it with the help of Don Hosek and also Przemysław's comment.

I didn't realize my solution was like

\begin{solution}%
$a:~part1; b:~part2; c:~part3$%
\begin{solution}%


\begin{solution}%
$a:~part1;$ $b:~part2;$ $c:~part3$%
`