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
This commit is contained in:
Ihor Radchenko 2024-01-18 14:01:32 +01:00
parent 15e51d7083
commit bd305ecdf6
No known key found for this signature in database
GPG Key ID: 6470762A7DA11D8B
2 changed files with 32 additions and 0 deletions

View File

@ -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))

View File

@ -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