0

When writing in Org mode, I have a minor pet peeve where the Org document becomes too "cramped", and so I like to add newlines separating the equations from the text, like this:

* Very comfortable to read
This document has separation between the text and the equations, making it very comfortable to read, with the equation

\begin{align*}
\frac{1}{2} \left( \frac{1}{\sqrt{2}} \right)
\end{align*}

being separated from the text.

However, when exporting to LaTeX, the newlines are treated as new paragraphs, and so the equation is indented. To avoid this behavior, I have to write my document like this:

* Too compact for my tastes
The text is too compact for me to read when I have LaTeX equations involved, such as
\begin{align*}
\frac{1}{2} \left( \frac{1}{\sqrt{2}} \right)
\end{align*}
but this is the proper way to write the document when exporting to LaTeX, as adding
newlines would treat the equation as a new paragraph, making the equation & the
line after it, indented. Also, this causes the LaTeX fragments to sit too closely to
the preceding & proceeding text.

Is there a way to have the best of both worlds? I want to be able to write my document with newlines separating the equations from the text, but I also want to be able to export to LaTeX without the equations being indented. Either some LaTeX/Org mode export option which would ignore newlines, or some piece of ELisp which would add "fake" newlines via

(put-text-property (point-at-eol) (1+ (point-at-eol)) 'line-spacing 10)

before and after the equation blocks, which would give it the appearance of extra newlines, but not actually be newlines. I am wondering what is the best way to tackle this issue.

5
  • Small correction: in the first case, the equation is not indented (but the line after it is). The equation has some additional vertical space above it, compared to the second case, but it is in the same horizontal position. If that doesn't bother you too much, then a compromise solution is to add the empty line before but not the empty line after. Try it and see. In any case, I don't know an out-of-the-box solution to the question as stated, but maybe someone will think of one.
    – NickD
    Commented Jul 6, 2023 at 21:40
  • @NickD When I said that the equation is indented, I meant that "there is an extra space between the equation and the line preceding it", as in, it was "indented vertically". I use vertical spacing instead of indentation for new paragraphs, and failed to mention that.
    – daedsidog
    Commented Jul 6, 2023 at 21:45
  • 1
    Actually a filter should be able to do it, assuming you can write a regexp that will match all the \begin{foo} / \end{foo} that you want to treat this way.
    – NickD
    Commented Jul 6, 2023 at 21:46
  • @NickD I think this is the best approach, and I'm currently in the process of writing one, but it will take some time as I'm very novice in ELisp.
    – daedsidog
    Commented Jul 6, 2023 at 21:48
  • A very similar question was asked here: emacs.stackexchange.com/questions/78025/…
    – NickD
    Commented Jul 18, 2023 at 13:15

1 Answer 1

2

I came up with the following solution, which may not be ideal, but does exactly what I want:

(defun org-latex-clear-newlines-around-equations (backend)
  (when (org-export-derived-backend-p backend 'latex)
    (let ((case-fold-search nil))
      (goto-char (point-min))
      (while (re-search-forward "\n\n\\\\begin{" nil t)
        (replace-match "\n\\\\begin{"))
      (goto-char (point-min))
      (while (re-search-forward "\\\\end{\\(.*\\)}\n\n" nil t)
        (replace-match (concat "\\\\end{" (match-string 1) "}\n"))))))

(add-hook 'org-export-before-parsing-hook 'org-latex-clear-newlines-around-equations)

EDIT: A better answer where instead of removing the spaces before export, we just modify the vertical spacing in the editor, can be found here.

9
  • You can accept your own answer, if you like...
    – Drew
    Commented Jul 7, 2023 at 0:59
  • @Drew Not immediately, apparently. Says I need to wait 2 days.
    – daedsidog
    Commented Jul 7, 2023 at 1:00
  • 1
    +1 - you might want to try a couple of small improvements: one is replacing two or more newlines before a \begin (or after an \end) with a single newline. The other is to replace the two loops by a single loop which should be faster - not that it will make much difference in this case; the downside is that the matching regexp is going to be more complicated.
    – NickD
    Commented Jul 7, 2023 at 2:10
  • Wait for several days (or months, or forever) to expect a better answer before you accept your own answer.
    – shynur
    Commented Jul 7, 2023 at 7:10
  • 2
    (@shynur) A question-poser-and-answerer can wait any length of time (after the minimum 2 days). But there's no requirement to wait longer, to wait for better answers. Why? Because the question-poser can always change the accepted answer (I believe).
    – Drew
    Commented Jul 7, 2023 at 16:21

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.