4

I would like left-justified text with embedded math which wraps in a manner similar to the behaviour with fully justified text. It seems LaTeX will wrap math at "sensible" points (e.g. between certain symbols) but that this changes when the text is left-justified.

The left column shows my attempt with flushleft. The right column shows the desired wrapping behaviour (which happens if I omit left-justification). I used the multicol environment just to make the column width smaller.

I originally thought the problem was related to list environments, so the first row of the example uses an itemize environment. The second row below shows that the same change in wrapping behaviour occurs without the list environment.

\begin{document}
\begin{multicols}{2}
  \begin{flushleft}
    \begin{itemize}
    \item If $A \in \{B \times C \times G\}$ then $D \times E \times F =
      M + N + O$
    \end{itemize}
  \end{flushleft}
    \begin{itemize}
    \item If $A \in \{B \times C \times G\}$ then $D \times E \times F =
      M + N + O$
    \end{itemize}
\end{multicols}

\begin{multicols}{2}
  \begin{flushleft}
    If $A \in \{B \times C \times G\}$ then $D \times E \times F = M + N
    + O$
  \end{flushleft}
    If $A \in \{B \times C \times G\}$ then $D \times E \times F = M + N
    + O$
\end{multicols}
\end{document}

enter image description here

4
  • 1
    flushleft and \raggedright do the exact same thing. One is an environment and the other isn't. Mar 7 '16 at 2:35
  • Ok, I deleted the \raggedright part of the example. Thanks.
    – Roly
    Mar 7 '16 at 9:37
  • @campa Clarified in the question that I mean observable behaviour. Thanks.
    – Roly
    Mar 7 '16 at 10:09
  • I haven’t got time to test any code now, so I won’t post an answer, but I’ll rather leave a hint: try decreasing \binoppenalty. 700 units of badness are probably too many in a ragged-right context. Edit: now I see that you have also an “=” sign: then decrease \relpenalty too.
    – GuM
    Mar 7 '16 at 12:18
5

As a matter of fact, the way \raggedright is implemented in LaTeX (with \@flushglue) will never permit, in practice, that a line be broken in the middle of a formula, because this implementation tells the underlying TeX engine to always prefer a line break between two words independently of how far from the right margin it occurs. This behavior is indeed questionable, since short lines should actually be regarded as “bad”, and in many circumstances it is clearly preferable to break a long in-line formula at some sensible place, as the question suggests, rather than to move that formula to a new line, leaving an awkward blank gap in the preceding one. See also the discussion about how to set up a “good” ragged-right setting found on page 101 of The TeXbook, right after Exercise 14.17, as well as the definition of \raggedright in plain TeX (cf. page 356).

However, it is easy to define a “mitigated” version of \raggedright that corrects this defect: the following code defines it under the name \mathmitigatedraggedright. Note that the value chosen for \relpenalty and \binoppenalty may need to be fine-tuned after some trial “on the battlefield”; you might also prefer to set \@rightskip (and \rightskip) to \z@ \@plus 3em, or even to \z@ \@plus 2em, rather than to \z@ \@plus 4em. Fiddle with the values of those three quantities until you are satisfied.

\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage{lipsum}

\makeatletter

% This time, we make it independent:
\newcommand*\mathmitigatedraggedright{%
    \relpenalty   50 % adjust until satisfied
    \binoppenalty 70 % as above
    \@rightskip \z@ \@plus 4em \relax % on second thought, 4em should be enough
    \rightskip \@rightskip
    \spaceskip .33333em \xspaceskip .5em
    % Other settings from LaTeX's "\raggedright":
    \let\\\@centercr
    \leftskip\z@skip
    \parindent\z@
}

\makeatother

\begin{document}

To show the prevailing margins, we typeset a little of ``Lorem ipsum''.

\lipsum[1]

% No need for an environment: just limit the ragged-right setting within the 
% scope of a group...
\begingroup
    \mathmitigatedraggedright
    % \tracingparagraphs = 1 % uncomment to see line break calculations
    Text text text text text text text text text text
    text text text text text text text text text text.
    Some other words, in order to obtain a different line length,
    thus showing that a ragged-right setting is actually in force
    in this paragraph.
    Text text text text text text text text text text
    %text %text % uncomment last two words to see changes
    \( a+b+c+d+e = f+g+h \).\par % <- ... but do not forget to say "\par" 
                                 % **before** closing the group!
\endgroup

\end{document}

The output with all comments in place is:

Output of amended code

Addition

I thought that the corresponding variant of the flushleft environment could be desired too; this is even more easily accomplished:

\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage{lipsum}

\makeatletter

% This time, we make it independent:
\newcommand*\mathmitigatedraggedright{%
    \relpenalty   50 % adjust until satisfied
    \binoppenalty 70 % as above
    \@rightskip \z@ \@plus 4em \relax % on second thought, 4em should be enough
    \rightskip \@rightskip
    \spaceskip .33333em
    \xspaceskip .5em % questionable: what if "\frenchspacing" is desired?
    % Other settings from LaTeX's "\raggedright":
    \let\\\@centercr
    \leftskip\z@skip
    \parindent\z@
}

\makeatother

% Define the "environmental" version too, cf. flushleft:
\newenvironment*{mathmitigatedflushleft}{%
    \trivlist \mathmitigatedraggedright \item \relax
}{\endtrivlist}



\begin{document}

To show the prevailing margins, we typeset a little of ``Lorem ipsum''.

\lipsum[1]

% No need for an environment: just limit the ragged-right setting within the 
% scope of a group...
\begingroup
    \mathmitigatedraggedright
    % \tracingparagraphs = 1 % uncomment to see line break calculations
    Text text text text text text text text text text
    text text text text text text text text text text.
    Some other words, in order to obtain a different line length,
    thus showing that a ragged-right setting is actually in force
    in this paragraph.
    Text text text text text text text text text text
    %text %text % uncomment last two words to see changes
    \( a+b+c+d+e = f+g+h \).\par % <- ... but do not forget to say "\par" 
                                 % **before** closing the group!
\endgroup

% The mathmitigatedflushleft environment, however, also provides the usual 
% vertical spacing (and penalties!):

\begin{mathmitigatedflushleft}
    % \tracingparagraphs = 1 % uncomment to see line break calculations
    Text text text text text text text text text text
    text text text text text text text text text text.
    Some other words, in order to obtain a different line length,
    thus showing that a ragged-right setting is actually in force
    in this paragraph.
    Text text text text text text text text text text
    %text %text % uncomment last two words to see changes
    \( a+b+c+d+e = f+g+h \). 
\end{mathmitigatedflushleft}

\end{document}

The corresponding output follows:

Output of the code for flushleft

2
  • Thanks for the great answer; I'll give it a try. (Pedantic response to your opening remarks: the "purpose" of \raggedright, in the mind of its one designer, isn't particularly relevant. Moreover both breaks are bad for some value of bad: one generates gaps in formulae, which you consider bad, and the other generates prematurely terminated lines, which I consider bad! So yes, better to avoid challenging the reasons behind a question :) Anyway, I don't want to be ungrateful: this is a fabulous answer. Thank you!
    – Roly
    Mar 8 '16 at 8:33
  • @Roly: Actually, in the meanwhile I have completely changed my mind, and I now agree with you 100% :-) After all, Knuth in person shares your opinion too, see the discussion on ragged-right setting he makes in The TeXbook, on page 101, right after Exercise 14.17. I shall rewrite the opening passage as soon as I have a minute.
    – GuM
    Mar 8 '16 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.