From: Ihor Radchenko <yantar92@posteo.net>
To: "Juan Manuel Macías" <maciaschain@posteo.net>
Cc: orgmode <emacs-orgmode@gnu.org>, Max Nikulin <manikulin@gmail.com>
Subject: Re: [possible patch] Remove the '\\[0pt]' string from the last line of a verse block in LaTeX export
Date: Thu, 18 Jan 2024 13:05:46 +0000 [thread overview]
Message-ID: <8734uuaj11.fsf@localhost> (raw)
In-Reply-To: <87o7dju9vn.fsf@posteo.net>
[-- Attachment #1: Type: text/plain, Size: 1866 bytes --]
Juan Manuel Macías <maciaschain@posteo.net> writes:
>> As for {[}{]}, it is a bit difficult to implement. Especially when we
>> also consider user filters and derived backends. If we have several
>> transcoders of consequent elements, there is always a risk that even
>> when a given filter/transcoder is generating a valid LaTeX code,
>> concatenating them may still cause issues like we have with \\.
>
> I see. I think pandoc's solution is what Leslie Lamport recommends
> (naturally, Lamport doesn't say to enclose /all/ brackets in curly
> braces).
This turned out to be a lot easier than I thought.
See the attached patch.
>> \command
>> [unrelated text]
>>
>> If there are, we may actually want to consider pandoc's approach
>> seriously.
>
> In principle, any environment that takes an optional argument in a
> "dangerous" position. Just do a simple test. Something like this:
>
> #+begin_figure
> [lorem] ipsum
> #+end_figure
>
> will throw an error like ''LaTeX Error: Unknown float option...''
>
> Of course, putting an empty line after #+begin... usually solves it. But
> the user may not know it.
>
> There are also a number of commands with an optional argument. For
> example \pagebreak. Something like this will give an error:
>
> lorem @@latex:\pagebreak@@ [ipsum]
>
> \item is another typical example, but in this case org adds \relax.
With the patch, I am getting the following:
* This is test
lorem @@latex:\pagebreak@@ [ipsum]
#+begin_figure
[lorem] figure
#+end_figure
| [foo] | 2 |
| [bar] | 3 |
- [bax]
- [aur]
exports to
lorem \pagebreak {[}ipsum]
\begin{figure}
{[}lorem] figure
\end{figure}
\begin{center}
\begin{tabular}{lr}
{[}foo] & 2\\[0pt]
{[}bar] & 3\\[0pt]
\end{tabular}
\end{center}
\begin{itemize}
\item {[}bax]
\item {[}aur]
\end{itemize}
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-ox-latex-Make-sure-that-text-is-not-misinterpreted-a.patch --]
[-- Type: text/x-patch, Size: 2511 bytes --]
From db3fa01d6f15b3d3f499f77e342a608a822029c8 Mon Sep 17 00:00:00 2001
Message-ID: <db3fa01d6f15b3d3f499f77e342a608a822029c8.1705583005.git.yantar92@posteo.net>
From: Ihor Radchenko <yantar92@posteo.net>
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)
\f
+(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
[-- Attachment #3: Type: text/plain, Size: 224 bytes --]
--
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>
next prev parent reply other threads:[~2024-01-18 13:03 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-02 23:46 [possible patch] Remove the '\\[0pt]' string from the last line of a verse block in LaTeX export Juan Manuel Macías
2024-01-13 15:08 ` Ihor Radchenko
2024-01-13 16:05 ` Juan Manuel Macías
2024-01-13 18:28 ` Ihor Radchenko
2024-01-13 20:22 ` Juan Manuel Macías
2024-01-14 12:33 ` Ihor Radchenko
2024-01-14 21:58 ` Juan Manuel Macías
2024-01-16 14:09 ` Ihor Radchenko
2024-01-16 19:33 ` Juan Manuel Macías
2024-01-17 13:00 ` Ihor Radchenko
2024-01-17 15:58 ` Max Nikulin
2024-01-17 17:50 ` Juan Manuel Macías
2024-01-18 13:05 ` Ihor Radchenko [this message]
2024-01-19 17:28 ` Juan Manuel Macías
2024-01-20 12:34 ` Ihor Radchenko
2024-01-20 13:22 ` Juan Manuel Macías
2024-01-20 13:46 ` Ihor Radchenko
2024-01-20 15:41 ` Juan Manuel Macías
2024-01-20 18:47 ` Ihor Radchenko
2024-01-20 20:27 ` Juan Manuel Macías
2024-01-21 13:42 ` Ihor Radchenko
2024-01-21 19:25 ` Juan Manuel Macías
2024-01-31 11:39 ` Ihor Radchenko
2024-01-21 6:06 ` Max Nikulin
2024-01-20 10:09 ` Max Nikulin
2024-01-20 10:57 ` Juan Manuel Macías
2024-01-20 12:41 ` Ihor Radchenko
2024-01-21 5:56 ` Max Nikulin
2024-01-20 10:27 ` Max Nikulin
2024-01-20 12:35 ` Ihor Radchenko
2024-01-21 5:44 ` Max Nikulin
2024-01-31 15:09 ` Ihor Radchenko
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=8734uuaj11.fsf@localhost \
--to=yantar92@posteo.net \
--cc=emacs-orgmode@gnu.org \
--cc=maciaschain@posteo.net \
--cc=manikulin@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.