pwl
pwl

Reputation: 113

Paragraph filling for org-mode inside latex environment

I started using org-mode to write mathematical papers so I make a heavy use of latex environments such as proof, theorem, lemma, etc. For example I often write

\begin{proof}

a very long proof follows

\end{proof}

The problem is that in org-mode the fill-paragraph (or M-q) doesn't work inside latex environments. This complicates my life because some proofs can be very long, reaching several pages when compiled to pdf, and I am unable to efficiently format them in org-mode. I couldn't find any information in the manual on options controlling paragraph filling. Is it possible to enable fill-paragraph in this case?

Upvotes: 1

Views: 1171

Answers (2)

fniessen
fniessen

Reputation: 4506

I have a command that you could bind to M-q in Org:

(defun leuven-good-old-fill-paragraph ()
  (interactive)
  (let ((fill-paragraph-function nil)
        (adaptive-fill-function nil))
    (fill-paragraph)))

Though, I don't understand either why it's not enabled by default. Maybe a question for the Org ML?

Upvotes: 1

olaf b
olaf b

Reputation: 606

The problem disappears if you use an org block instead of a latex environment:

#+BEGIN_proof 
...
#+END_proof 

This gets exported as \begin{proof}...\end{proof}. It also lets you use org syntax inside the block, and fill paragraph works.

if you don't want to do that, maybe try visual-line-mode as a workaround.

Edit: change fill-paragraph behaviour

If you want fill paragraph to work in latex environments, you have to dig a little deeper. Filling is done by org-fill-paragraph in org.el and this function ignores latex environments by default. To change this, go to the end of the function and replace

  ;; Ignore every other element.
  (otherwise t)

with

  (latex-environment nil) ;; use default fill-paragraph
  ;; Ignore every other element.
  (otherwise t)

If you'd rather not change the org sources, you could use advice instead, e.g.

(defun org-fill-paragraph--latex-environment (&rest args)
  "Use default fill-paragraph in latex environments."
  (not (eql (org-element-type (org-element-context)) 'latex-environment)))

(advice-add 'org-fill-paragraph :before-while #'org-fill-paragraph--latex-environment)

Upvotes: 1

Related Questions