diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el index df20345f8..a64dd5a87 100644 --- a/lisp/ox-latex.el +++ b/lisp/ox-latex.el @@ -3095,6 +3095,15 @@ contextual information." "\\(?:[ \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 @@ +(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