From db3fa01d6f15b3d3f499f77e342a608a822029c8 Mon Sep 17 00:00:00 2001 Message-ID: From: Ihor Radchenko Date: Thu, 18 Jan 2024 14:01:32 +0100 Subject: [PATCH] ox-latex: Make sure that [text] is not misinterpreted as LaTeX argument * lisp/ox-latex.el (org-latex-plain-text): Protect plain text starting from [. It might be misinterpreted as optional command argument if previous exported fragment ends with a command accepting such. * testing/lisp/test-ox-latex.el (text-ox-latex/protect-square-brackets): Add new test. Link: https://orgmode.org/list/87o7dju9vn.fsf@posteo.net --- lisp/ox-latex.el | 9 +++++++++ testing/lisp/test-ox-latex.el | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el index 432f09f4e..a3505c25f 100644 --- a/lisp/ox-latex.el +++ b/lisp/ox-latex.el @@ -3094,6 +3094,15 @@ (defun org-latex-plain-text (text info) "\\(?:[ \t]*\\\\\\\\\\)?[ \t]*\n" (concat org-latex-line-break-safe "\n") output nil t))) + ;; Protect [foo] at the beginning of lines / beginning of the + ;; plain-text object. This prevents LaTeX from unexpectedly + ;; interpreting @@latex:\pagebreak@@ [foo] as a command with + ;; optional argument. + (setq output (replace-regexp-in-string + (rx bol (0+ space) (group "[")) + "{[}" + output + nil nil 1)) ;; Return value. output)) diff --git a/testing/lisp/test-ox-latex.el b/testing/lisp/test-ox-latex.el index 41df1b823..237ad97ec 100644 --- a/testing/lisp/test-ox-latex.el +++ b/testing/lisp/test-ox-latex.el @@ -29,6 +29,29 @@ (unless (featurep 'ox-latex) +(ert-deftest text-ox-latex/protect-square-brackets () + "Test [foo] being interpreted as plain text even after LaTeX commands." + (org-test-with-exported-text + 'latex + "* This is test +lorem @@latex:\\pagebreak@@ [ipsum] + +#+begin_figure +[lorem] figure +#+end_figure + +| [foo] | 2 | +| [bar] | 3 | + +- [bax] +- [aur] +" + (goto-char (point-min)) + (should (search-forward "lorem \\pagebreak {[}ipsum]")) + (should (search-forward "{[}lorem] figure")) + (should (search-forward "{[}foo]")) + (should (search-forward "\\item {[}bax]")))) + (ert-deftest test-ox-latex/verse () "Test verse blocks." (org-test-with-exported-text -- 2.43.0