emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Ihor Radchenko <yantar92@gmail.com>
To: Max Nikulin <manikulin@gmail.com>, Daniel Fleischer <danflscr@gmail.com>
Cc: Stewart Thomas <sjt015@bucknell.edu>,  emacs-orgmode@gnu.org
Subject: [PATCH] Re: [BUG] Tables with square brackets do not compile in PDF (latex) export [9.5 (release_9.5-59-g52e6f1 @ /home/sjt015/repo/emacs/lisp/org/)]
Date: Fri, 07 Oct 2022 16:31:22 +0800	[thread overview]
Message-ID: <8735c0kpb9.fsf@localhost> (raw)
In-Reply-To: <ce760fc3-5aae-144d-2d02-7dea215f73fc@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 845 bytes --]

Max Nikulin <manikulin@gmail.com> writes:

> On 10/11/2021 23:16, Stewart Thomas wrote:
>> 
>> A table in which a first column begins with square brackets is not
>> compiling. The square bracket is interpreted by latex as an argument
>> to the preceding line break.
>
> Confirmed
>
> To fix the bug newline (in tables and paragraphs) should be exported as 
> "\\\relax" or "\\{}". I do not suggest curly brackets in the beginning 
> of line to avoid issues with spurious spaces. Percent character 
> (comment) at the end of line should be considered for a fix since it is 
> usually used in the opposite way: to suppress newline character and 
> spaces at the beginning of next source line.

I am attaching a tentative patch to fix this.
However, I am not sure if I replaced \\ with \\\relax in the right
places.

Daniel, can you please check?


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-ox-latex-Protect-.-after-to-be-interpreted-as-LaTeX-.patch --]
[-- Type: text/x-patch, Size: 6875 bytes --]

From 43d643197883e10a6c6551489d37053c2c89b4dd Mon Sep 17 00:00:00 2001
Message-Id: <43d643197883e10a6c6551489d37053c2c89b4dd.1665131437.git.yantar92@gmail.com>
From: Ihor Radchenko <yantar92@gmail.com>
Date: Fri, 7 Oct 2022 16:24:32 +0800
Subject: [PATCH] ox-latex: Protect [...] after \\ to be interpreted as LaTeX
 argument

* lisp/ox-latex.el (org-latex-linebreak-safe): New constant holding
safe version of LaTeX line break.
(org-latex-table-matrix-macros):
(org-latex-clock):
(org-latex-line-break):
(org-latex-plain-text):
(org-latex-planning):
(org-latex--org-table):
(org-latex--math-table):
(org-latex-table-row):
(org-latex-verse-block): Use the new constant.

* testing/lisp/test-org-table.el (test-org-table/to-latex): Update
tests.

Reported-by: Stewart Thomas <sjt015@bucknell.edu>
Link: https://orgmode.org/list/ce760fc3-5aae-144d-2d02-7dea215f73fc@gmail.com
---
 lisp/ox-latex.el               | 47 ++++++++++++++++++++++++----------
 testing/lisp/test-org-table.el |  6 ++---
 2 files changed, 36 insertions(+), 17 deletions(-)

diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 34ff52b81..127637ccf 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -278,10 +278,23 @@ (defconst org-latex-language-alist
 
 - `:lang-name' the actual name of the language.")
 
+(defconst org-latex-linebreak-safe "\\\\\\relax"
+  "Linebreak protecting the following [...].
 
-(defconst org-latex-table-matrix-macros '(("bordermatrix" . "\\cr")
+Without \"\\relax\" it would be interpreted as an optional argument to
+the \\\\.
+
+This constant, for example, makes the below code not err:
+
+\\begin{tabular}{c|c}
+    [t] & s\\\\\\relax
+    [I] & A\\\\\\relax
+    [m] & kg
+\\end{tabular}")
+
+(defconst org-latex-table-matrix-macros `(("bordermatrix" . "\\cr")
 					  ("qbordermatrix" . "\\cr")
-					  ("kbordermatrix" . "\\\\"))
+					  ("kbordermatrix" . ,org-latex-linebreak-safe))
   "Alist between matrix macros and their row ending.")
 
 (defconst org-latex-math-environments-re
@@ -2062,7 +2075,7 @@ (defun org-latex-clock (clock _contents info)
 	   (concat (org-timestamp-translate (org-element-property :value clock))
 		   (let ((time (org-element-property :duration clock)))
 		     (and time (format " (%s)" time)))))
-   "\\\\"))
+   org-latex-linebreak-safe))
 
 
 ;;;; Code
@@ -2659,7 +2672,7 @@ ;;;; Line Break
 (defun org-latex-line-break (_line-break _contents _info)
   "Transcode a LINE-BREAK object from Org to LaTeX.
 CONTENTS is nil.  INFO is a plist holding contextual information."
-  "\\\\\n")
+  (concat org-latex-linebreak-safe "\n"))
 
 
 ;;;; Link
@@ -3005,7 +3018,9 @@ (defun org-latex-plain-text (text info)
     ;; Handle break preservation if required.
     (when (plist-get info :preserve-breaks)
       (setq output (replace-regexp-in-string
-		    "\\(?:[ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n" output nil t)))
+		    "\\(?:[ \t]*\\\\\\\\\\)?[ \t]*\n"
+                    (concat org-latex-linebreak-safe "\n")
+                    output nil t)))
     ;; Return value.
     output))
 
@@ -3041,7 +3056,7 @@ (defun org-latex-planning (planning _contents info)
 		(format (plist-get info :latex-active-timestamp-format)
 			(org-timestamp-translate scheduled)))))))
     " ")
-   "\\\\"))
+   org-latex-linebreak-safe))
 
 
 ;;;; Property Drawer
@@ -3821,11 +3836,11 @@ (defun org-latex--org-table (table contents info)
 		(format "\\begin{%s}%s{%s}\n" table-env width alignment)
 		(and above?
 		     (org-string-nw-p caption)
-		     (concat caption "\\\\\n"))
+		     (concat caption org-latex-linebreak-safe "\n"))
 		contents
 		(and (not above?)
 		     (org-string-nw-p caption)
-		     (concat caption "\\\\\n"))
+		     (concat caption org-latex-linebreak-safe "\n"))
 		(format "\\end{%s}" table-env)
 		(and fontsize "}"))))
      (t
@@ -3910,7 +3925,7 @@ (defun org-latex--math-table (table info)
 		 (lambda (cell)
 		   (substring (org-element-interpret-data cell) 0 -1))
 		 (org-element-map row 'table-cell #'identity info) "&")
-		(or (cdr (assoc env org-latex-table-matrix-macros)) "\\\\")
+		(or (cdr (assoc env org-latex-table-matrix-macros)) org-latex-linebreak-safe)
 		"\n")))
 	   (org-element-map table 'table-row #'identity info) "")))
     (concat
@@ -3982,7 +3997,7 @@ (defun org-latex-table-row (table-row contents info)
        ;; hline was specifically marked.
        (and booktabsp (not (org-export-get-previous-element table-row info))
 	    "\\toprule\n")
-       contents "\\\\\n"
+       contents org-latex-linebreak-safe "\n"
        (cond
 	;; Special case for long tables.  Define header and footers.
 	((and longtablep (org-export-table-row-ends-header-p table-row info))
@@ -3990,9 +4005,9 @@ (defun org-latex-table-row (table-row contents info)
 			      (org-export-get-parent-table table-row) info))))
 	   (format "%s
 \\endfirsthead
-\\multicolumn{%d}{l}{%s} \\\\
+\\multicolumn{%d}{l}{%s} \\\\\\relax
 %s
-%s \\\\\n
+%s \\\\\\relax\n
 %s
 \\endhead
 %s\\multicolumn{%d}{r}{%s} \\\\
@@ -4092,8 +4107,12 @@ (defun org-latex-verse-block (verse-block contents info)
 	       (replace-regexp-in-string
 	        "^[ \t]*\\\\\\\\$" "\\vspace*{1em}"
 	        (replace-regexp-in-string
-	         "\\([ \t]*\\\\\\\\\\)?[ \t]*\n" "\\\\\n"
-	         contents nil t) nil t) nil t) linreset)
+	         "\\([ \t]*\\\\\\\\\\)?[ \t]*\n"
+                 (concat org-latex-linebreak-safe "\n")
+	         contents nil t)
+                nil t)
+               nil t)
+              linreset)
       info)
      ;; Insert footnote definitions, if any, after the environment, so
      ;; the special formatting above is not applied to them.
diff --git a/testing/lisp/test-org-table.el b/testing/lisp/test-org-table.el
index 76fe41630..3e7f18a10 100644
--- a/testing/lisp/test-org-table.el
+++ b/testing/lisp/test-org-table.el
@@ -1635,11 +1635,11 @@ (ert-deftest test-org-table/to-generic ()
 (ert-deftest test-org-table/to-latex ()
   "Test `orgtbl-to-latex' specifications."
   (should
-   (equal "\\begin{tabular}{l}\na\\\\\n\\end{tabular}"
+   (equal "\\begin{tabular}{l}\na\\\\\\relax\n\\end{tabular}"
 	  (orgtbl-to-latex (org-table-to-lisp "| a |") nil)))
   ;; Test :environment parameter.
   (should
-   (equal "\\begin{tabularx}{l}\na\\\\\n\\end{tabularx}"
+   (equal "\\begin{tabularx}{l}\na\\\\\\relax\n\\end{tabularx}"
 	  (orgtbl-to-latex (org-table-to-lisp "| a |")
 			   '(:environment "tabularx"))))
   ;; Test :booktabs parameter.
@@ -1648,7 +1648,7 @@ (ert-deftest test-org-table/to-latex ()
     "\\toprule" (orgtbl-to-latex (org-table-to-lisp "| a |") '(:booktabs t))))
   ;; Handle LaTeX snippets.
   (should
-   (equal "\\begin{tabular}{l}\n\\(x\\)\\\\\n\\end{tabular}"
+   (equal "\\begin{tabular}{l}\n\\(x\\)\\\\\\relax\n\\end{tabular}"
 	  (orgtbl-to-latex (org-table-to-lisp "| $x$ |") nil)))
   ;; Test pseudo objects and :raw parameter.
   (should
-- 
2.35.1


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

  reply	other threads:[~2022-10-07  8:34 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-10 16:16 [BUG] Tables with square brackets do not compile in PDF (latex) export [9.5 (release_9.5-59-g52e6f1 @ /home/sjt015/repo/emacs/lisp/org/)] Stewart Thomas
2021-11-13 14:57 ` Max Nikulin
2022-10-07  8:31   ` Ihor Radchenko [this message]
2022-10-07 11:34     ` [PATCH] " Max Nikulin
2022-10-08  5:17       ` [PATCH v2] " Ihor Radchenko
2022-10-10 16:30         ` Max Nikulin
2022-10-11  3:36           ` 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

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8735c0kpb9.fsf@localhost \
    --to=yantar92@gmail.com \
    --cc=danflscr@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=manikulin@gmail.com \
    --cc=sjt015@bucknell.edu \
    /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 public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).