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.

fractions bleeding

It feels extra weird to me given it even worked properly with polynomials (it even breaks them in the middle if it needs to).

polynomials working properly

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$; ...

Fractions after Don Hosek

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!

fractions now working

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

2 Answers 2


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.
    – Rusca8
    Commented Jan 2, 2021 at 13:15
  • 1
    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.
    – Don Hosek
    Commented Jan 2, 2021 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!
    – Rusca8
    Commented Jan 8, 2021 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

$a:~part1; b:~part2; c:~part3$%

...instead of

$a:~part1;$ $b:~part2;$ $c:~part3$%

Changing this was the main issue, though Don Hosek's suggestions about penalty and ragged right also helped.

Now it still sparingly throws a break in the middle of a polynomial factorization, but its so uncommon I'll just take it (and after trying for a while it doesn't seem to bleed, which is what I was most concerned with), so yup.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .