Subject: Fix Table.el export

From: Pranshu <[email-masked]>
Date: Tue, 04 Jun 2024 21:22:00 +1000

diff --git a/lisp/textmodes/table.el b/lisp/textmodes/table.el
index 19c6a8d7c4..28401084a6 100644
--- a/lisp/textmodes/table.el
+++ b/lisp/textmodes/table.el
@@ -697,6 +697,19 @@ table-word-continuation-char
   :type 'character
   :group 'table)
 
+(defcustom table-source-latex-escape-characters (cons "[#$~_^%{}&]" t)
+  "A cons cell containing which charecters to escape in the latex source
+of `table-generate-source'.  The head of the list, if non-nil contains a
+regexp that matches all text that is to be adding a preceding backslash
+to the matching text. If nil, no non-backslash charecters will be
+escaped.  The tail, if non-nil, escapes all the backslashes in the latex
+source."
+  :tag "Source Latex Escape Charecters Regexp"
+  :type '(cons (radio (regexp :tag "regexp")
+                        (const :tag "Off" nil))
+               boolean)
+  :group 'table)
+
 (defcustom table-detect-cell-alignment t
   "Detect cell contents alignment automatically.
 When non-nil cell alignment is automatically determined by the
@@ -3264,19 +3277,29 @@ table--generate-source-scan-lines
                   (lambda (from to)
                     (let ((line (table--buffer-substring-and-trim
                                  (table--goto-coordinate (cons from y))
-                                 (table--goto-coordinate (cons to y)))))
+                                 (table--goto-coordinate (cons to y))))
+                          (escape-char-reg
+                           (apply 'concat (append (and (car table-source-latex-escape-characters)
+                                               (list "\\("
+                                                     (car table-source-latex-escape-characters)
+                                                     "\\)"
+                                                     (and (cdr table-source-latex-escape-characters)
+                                                          "\\|")))
+                                   (and (cdr table-source-latex-escape-characters)
+                                        (list"\\(\\\\\\)"))))))
                       ;; escape special characters
                       (with-temp-buffer
                         (insert line)
                         (goto-char (point-min))
-                        (while (re-search-forward "\\([#$~_^%{}&]\\)\\|\\(\\\\\\)\\|\\([<>|]\\)" nil t)
-                          (if (match-beginning 1)
-                              (save-excursion
-                                (goto-char (match-beginning 1))
-                                (insert  "\\"))
-                            (if (match-beginning 2)
-                                (replace-match "$\\backslash$" t t)
-                              (replace-match (concat "$" (match-string 3) "$")) t t)))
+                        (when (or (car table-source-latex-escape-characters)
+                                  (cdr table-source-latex-escape-characters))
+                          (while (re-search-forward escape-char-reg nil t)
+                            (if (and (car table-source-latex-escape-characters)
+                                     (match-beginning 1))
+                                    (save-excursion
+                                      (goto-char (match-beginning 1))
+                                      (insert  "\\"))
+                              (replace-match "$\\backslash$" t t))))
                         (setq line (buffer-substring (point-min) (point-max))))
                       ;; insert a column separator and column/multicolumn contents
                       (with-current-buffer dest-buffer

Hello all,

When table.el exports to latex, charecters like '$' are all escaped.
This makes table.el tables practically useless for any sorts of latex
export.

Attached is a diff that adds a variable that controls what should be
escaped.

Kind Regards,
Pranshu