4

I would like to discourage linebreaks right before and after inline math displays, such that

now let $f$ be the function that connects

acts similar to

now let~$f$~be the function that connects 

It's not important to me that the behaviour is exactly the same - in fact, I only want to discourage linebreaks at these locations, not necessarily completely forbid them. I have a long document with lots of inline math (as a matter of fact, my Masters thesis) and I would like to see things like

                      ... now let f
be the function that connects ...

and

                        ... now let
f be the function that connects ...

less often, without having to edit every single occurence (which would be several hundred manual edits).

3
  • 1
    Do you have cases of inline math starting or ending a sentence? If so, do you want to prevent line breaks involving inline math expressions across sentences?
    – Mico
    Commented Aug 16, 2013 at 11:29
  • 3
    This would have been a lot easier if you'd have used the latex \( \) syntax, one reason for introducing that is it is then trivial to redefine the commands for special affects, eg define \( to include \nolinebreak[3] or whatever. Commented Aug 16, 2013 at 11:32
  • Inline math (almost) never starts or ends a sentence, since this is something I consider bad style. @ David Carlisle: I am aware of this. However, my workflow is to write each section as a separate document using LyX, and once I'm happy with the content, export the tex code and add it to my main document. Hence, unless you know a convenient method to configure LyX to use \(/\), I'll stick with $/$.
    – carsten
    Commented Aug 16, 2013 at 11:42

2 Answers 2

5

enter image description here

Inserting a penalty after the math isn't so hard, see how the break after $h$ is suppressed.

\documentclass{article}

\begin{document}

one two three $f$ four five six
one two three $f$ four five six
one two three $h$ four five six
one two three $f$ four five six
one two three $f$ four five six
one two $g$ three $f$ four five six
one two three $f$ four five six
one two three $f$ four five six
one two three $f$ four five six


\def\hmmm{\nolinebreak[3]}
\everymath{\aftergroup\hmmm}

one two three $f$ four five six
one two three $f$ four five six
one two three $h$ four five six
one two three $f$ four five six
one two three $f$ four five six
one two $g$ three $f$ four five six
one two three $f$ four five six
one two three $f$ four five six
one two three $f$ four five six



\end{document}

Suppressing breaks before the math is likely to be fragile when using the $ syntax most things you could think of would break something....

4
  • Isn't this a bit dangerous because it throws away whatever that was originally there in \everymath?
    – user202729
    Commented May 12 at 22:27
  • @user202729 well you could add to the register rather than set it, but there is nothing there (latex hides the real \everymath primitive for its own uses and so the user accessible \everymath is known empty here) Commented May 12 at 22:39
  • Does any popular/not-so-popular package modifies it then?
    – user202729
    Commented May 12 at 22:42
  • oh probably but 11 years ago I decided to keep the answer simple and to the point and not worry that some random unknown package on ctan might need a more complicated version. @user202729 these days the latex tagging code hooks into the same place to tag math as well. Commented May 12 at 22:51
0

This method works, but is fragile. Maybe Lua has a better solution by traversing the nodes.


You can redefine the dollar, so:

%! TEX program = pdflatex
\documentclass{article}
\begingroup
\ExplSyntaxOn
\catcode`\$=\active
\protected\gdef${
  \unless\ifmmode
    % ======== we're near the start of a formula
    \ifdim\lastskip>\c_zero_dim
      \l_tmpa_dim=\lastskip\relax
      \unskip
      \nolinebreak[3]
      \hskip\l_tmpa_dim
    \fi
    \(
  \else
    % ======== we're near the end of a formula ========
    \)\nolinebreak[3]
  \fi
}
\ExplSyntaxOff
\endgroup
\begin{document}

one two three $f$ four five six
one two three $f$ four five six
one two three $h$ four five six
one two three $f$ four five six
one two three $f$ four five six
one two $g$ three $f$ four five six
one two three $f$ four five six
one two three $f$ four five six
one two three $f$ four five six

\catcode`\$=\active

one two three $f$ four five six
one two three $f$ four five six
one two three $h$ four five six
one two three $f$ four five six
one two three $f$ four five six
one two $g$ three $f$ four five six
one two three $f$ four five six
one two three $f$ four five six
one two three $f$ four five six

\end{document}

Of course this will break things that rely on $ having the catcode it should have e.g. TikZ calc library:

\begin{tikzpicture}
  \draw (0, 0)--($(0, 0)+(1, 1)$);  % "Missing $ inserted." --- what?
\end{tikzpicture}

There are some ways to patch it e.g.

\AddToHook{env/tikzpicture/begin}{\catcode`\$=3\relax}

But then the text inside nodes don't get this applied (this is fragile!):

\begin{tikzpicture}
  \node [text width=10cm, above] {one two three $f$ four five six one two three $f$ four five six one a $f$ four five six one two three $f$ four five six};
  \node [text width=10cm, below] {one two three $f$ four five six one two three $f$ four five six one a~$f$~four five six one two three $f$ four five six};
\end{tikzpicture}

In this specific case you can get away with using TikZ' node font:

\begin{tikzpicture}[node font={\catcode`\$=\active}]
  \node [text width=10cm, above] {one two three $f$ four five six one two three $f$ four five six one a $f$ four five six one two three $f$ four five six};
  \node [text width=10cm, below] {one two three $f$ four five six one two three $f$ four five six one a~$f$~four five six one two three $f$ four five six};
\end{tikzpicture}

You must log in to answer this question.

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