Is it possible to make LaTeX automatically not break any lines just before references and citations?

Long explanation (only for references, citations are analogous):

Suppose I have the following line of LaTeX: The brown fox jumps over another fox which is green \ref{sec: study about jumping foxes}.

Depending on various parameters LaTeX might decide to break the line just after the word "green", so that my reference would end up on the next line. Because this looks ugly, I can insert a ~ after green, to prohibit line breaking.

But doing so is tedious, when I have a lot of references. Can I get LaTeX to avoid this line-breaking automatically?
A preprocessing solution would be the last, brute-force solution I woulr resort to, where a script rewrites my code any puts ~'s before all the references in my text. But I really would not want to go this route, if possible, as it seems rather ugly and inelegant.

  • 1
    Well, it's only tedious if you are going back and putting them in at the end. If you get into the habit of using non-breaking spaces in the many places where they belong it's no more tedious than using speech marks – Au101 Jun 24 '17 at 21:25
  • @Au101 Actually I'd like to be even more extreme an shave off the half second I need to type them :) – l7ll7 Jun 24 '17 at 21:28
  • By spending how many hours avoiding the half second? You can always find and replace them. Any decent editor will support regular expressions, so it should be simple. – cfr Jun 25 '17 at 0:50
  • Don't forget to load the microtype package, which usually does a good job to avoid too long lines. – Keks Dose Jun 25 '17 at 9:15

The following will probably work:


For example, this document shows the difference:

\label{sec: study about jumping foxes}
Dummy paragraph.

\frenchspacing \hsize=25em % Just to illustrate the next bit

The brown fox jumps over another fox which is green \ref{sec: study about jumping foxes}. This is interesting. The quick brown fox jumps over the lazy dog.


The brown fox jumps over another fox which is green \ref{sec: study about jumping foxes}. This is interesting. The quick brown fox jumps over the lazy dog.

Typeset output

In the new \ref, the \unskip removes the glue inserted by the unwanted (breakable) space you typed before the \ref, and then we follow up with the equivalent of having correctly typed ~\ref in the first place.

(I haven't tested this “in production” with large documents and for all I know some LaTeX macro hackery may break.)

IMO though the above may work, it would be somewhat better to do a global search-and-replace that puts the ~ before each \ref appropriately. It would be much more preferable to get into the habit of correctly typing the ~ where they belong. Caring about your output is the only way to achieve fine typography; there are too many subjective considerations that cannot be automated.

Donald Knuth, the creator of TeX, writes with his co-author Michael Plass in their paper Breaking Paragraphs into Lines of seven typical cases where one should type ties (aka “auxiliary spaces” in the typographic literature). You can read the paper yourself for the examples, but in most cases they are not trivial to automate (leaving out the dollar signs below, to highlight the ties):

    1. Theorem~A but Programs E and~F
    1. string~s of length~l but string~s of length l~or more
    1. of~x but of u~and~v
    1. is~15 but is 15~times the height; for all large~n but for all n~greater than~m

They conclude with:

It would be nice to boil these seven rules down into one or two, and it would be even nicer if the rules could be automated so that keyboarding could be done without them; but subtle semantic considerations seem to be involved in many of these instances.


It seems to be time to resurrect such old traditions of fine printing.

Recent experience of the authors indicates that it is not a substantial additional burden to insert auxiliary spaces when entering a manuscript into a computer. The careful use of such spaces may in fact lead to greater job satisfaction on the part of the keyboard operator, since the quality of the output can be noticeably improved with comparatively little work. It is comforting at times to know that the machine needs your help.

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.