4

Stumbled upon the following problem.

Whenever fill-paragraph finds a LaTeX citation such as \citep[][p. 10]{darwin1859} (which appears as (Darwin, 1859, p. 10)), the paragraph gets broken like this:

Text text text text \citep[][p.
10]{darwin1859} text text text.

How can I keep the citation intact, including the whitespace in [p. 10]?


Now for what I've found so far.

There's a fill-nobreak-predicate hook that may contain functions that check whether fill-paragraph should skip certain regexps from being split (if I'm not mistaken).

I tried with this code but failed:

(defun my-nobreak-p ()
  (looking-at-p "\\\\citep.*\\}"))
(add-hook 'fill-nobreak-predicate #'my-nobreak-p)

1 Answer 1

3

The function you put on fill-nobreak-predicate should return non-nil when point is at the space you don't want to break. So your my-nobreak-p doesn't help because that position is not followed by \cite... and hence doesn't match the regexp.

You can try something like

(defun my-nobreak-p ()
  (and (looking-at " *[0-9]")
       (looking-back "\\[p\\. *" (- (point) 5))))
(add-hook 'fill-nobreak-predicate #'my-nobreak-p)
4
  • Hello. Your function returns t at the right place but fill-paragraph still breaks the citations. Is there a way to explicitly tell fill-paragraph to avoid breaking it at all costs (e.g. moving the whole citation to a new line)?
    – undostres
    Commented May 15, 2015 at 20:29
  • I just fixed two details in my code (the name of the function, and the assumption that point is right before the space, whereas in practice it's normally right after the space). Now it "does the right thing" in my test.
    – Stefan
    Commented May 15, 2015 at 20:39
  • Excellent, it works great. Thank you! Now, in a general view, nobreak-p functions should return t when point is after, not right at the space? Or maybe both, such is the case for your answer?
    – undostres
    Commented May 15, 2015 at 21:49
  • Obviously, I can never remember where point is supposed to be exactly. And maybe there are also cases where point could be before the space. So it makes sense to just handle both cases at the same time. It's generally easy to handle both cases with a simple (save-excursion (skip-chars-backward " ") ...).
    – Stefan
    Commented May 16, 2015 at 13:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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