Hi

A small patch attached to fix the compilation of the following org table with latex:

#+attr_latex: :mode math :environment align*
| f(x) = | x^3 + 3*x^2 + 3*x + 1   |
| =      | (x^2 + 2*x + 1) * (x+1) |

align* (and align) don't like to be embedded in \[\] or \(\)

I would push this right now, but I don't want to interfere with the org-latex-preview process. It'll appear in the unmerged fontspec branch shortly.


Best, /PA
--
Pedro A. Aranda
ox-latex & ox-beamer maintainer

Sagen's Paradeiser! (ORF: Als Radiohören gefährlich war) => write BE!
IA: onomatopeya del rebuzno
2nd year of the New Koprocracy
From 5239a6ea8c69217a8f8e5b66e866ec6c08b0e8d6 Mon Sep 17 00:00:00 2001
From: "Pedro A. Aranda" <[email protected]>
Date: Wed, 21 Oct 2026 18:13:40 +0200
Subject: [PATCH 65/65] ox-latex: fix 'align' environments

* doc/org-manual.org (*** Tables in LaTeX export): Add a footnote.
* etc/ORG-NEWS: Announce support for 'align' with an example.
* lisp/ox-latex.el (org-latex--maybe-escape-matrix): New function to fix
the 'align' and 'align*' environments.
(org-latex--wrap-latex-matrices): Apply new function for inline-math and
math.
* testing/lisp/test-ox-latex.el (test-ox-latex/latex-align-env): New
test to check that the inlining is suppressed for 'align' and 'align*'.

When trying to use ':mode math :environment align*' LaTeX fails. The
problem is that it can't be embedded as inline math.
---
 doc/org-manual.org            |  3 ++-
 etc/ORG-NEWS                  | 11 +++++++++++
 lisp/ox-latex.el              | 13 +++++++++++--
 testing/lisp/test-ox-latex.el | 27 +++++++++++++++++++++++++++
 4 files changed, 51 insertions(+), 3 deletions(-)

diff --git a/doc/org-manual.org b/doc/org-manual.org
index c6bf49f42..682f3aa9a 100644
--- a/doc/org-manual.org
+++ b/doc/org-manual.org
@@ -14784,7 +14784,8 @@ include:
   =table=, =math=, =inline-math=, =verbatim= or =tabbing=.
 
   For =math= or =inline-math= mode, LaTeX export backend wraps the
-  table in a math environment, but every cell in it is exported as-is.
+  table in a math environment[fn::Except the ~align~ and ~align*~
+  environments!], but every cell in it is exported as-is.
   For =tabbing= the LaTeX tabbing environment is used and the correct
   tabbing delimiters =\>= are used.
   The LaTeX export backend determines the default mode from
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 690dc0e90..0f69cd5a4 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -74,6 +74,17 @@ assume that the outer list has length one, which means ~(car
 (org-babel-tangle-single-block ...))~ is not guaranteed to return
 the correct ~(FILE . ...)~ cons.
 
+*** ~ox-latex~ suppresses math inlining for LaTeX environments align and align*
+
+The multi-line equation environments =align= and =align*= are now usable
+for an equation table, like for example:
+
+#+begin_src org
+,#+attr_latex: :mode math :environment align*
+| f(x) = | x^2 + 2*x + 1 |
+| =      | (x+1)^2       |
+#+end_src
+
 ** New features
 
 # We list the most important features, and the features that may
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 7e2ad08e8..08adf509b 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -4175,12 +4175,21 @@ (defun org-latex--wrap-latex-matrices (data info)
     info)
   data)
 
+(defun org-latex--maybe-escape-matrix (fmt contents)
+  "Escape CONTENTS using FMT, expect for the align and align* environments.
+Remove the space between the equal sign and the column mark (&) at the
+end of a cell for these environments."
+  (if (string-match-p "\\\\begin{align\\*?}" contents)
+      (string-replace
+       "= &" "=&" contents)
+    (format fmt contents)))
+
 (defun org-latex-matrices (matrices contents info)
   "Transcode a MATRICES element from Org to LaTeX.
 CONTENTS is a string.  INFO is a plist used as a communication
 channel."
   (pcase (org-element-property :markup matrices)
-    (`inline (format "\\(%s\\)" contents))
+    (`inline (org-latex--maybe-escape-matrix "\\(%s\\)" contents))
     (`equation
      (let ((caption (org-latex--caption/label-string matrices info))
 	   (caption-above? (org-latex--caption-above-p matrices info)))
@@ -4190,7 +4199,7 @@ (defun org-latex-matrices (matrices contents info)
 	       (and (not caption-above?) caption)
 	       "\\end{equation}")))
     (_
-     (format "\\[\n%s\\]" contents))))
+     (org-latex--maybe-escape-matrix "\\[\n%s\\]" contents))))
 
 
 ;;;; Pseudo Object: LaTeX Math Block
diff --git a/testing/lisp/test-ox-latex.el b/testing/lisp/test-ox-latex.el
index 11837e0bf..affb9fec2 100644
--- a/testing/lisp/test-ox-latex.el
+++ b/testing/lisp/test-ox-latex.el
@@ -1286,5 +1286,32 @@ (ert-deftest test-ox-latex/sci-notation ()
    (should (search-forward "\\begin{tabular}"))
    (should (search-forward "2.00 & \\num{3.81e+12}"))))
 
+(ert-deftest test-ox-latex/latex-align-env ()
+  "Check that the align* environment is NOT surrounded by
+inline math \\[\\] delimiters."
+  (org-test-with-exported-text
+   'latex
+   "* A multi-line equation with the align environment
+We want a simple way for multiline equations:
+
+#+attr_latex: :mode math :environment align*
+| f(x) = | x^3 + 3*x^2 + 3*x + 1   |
+|      = | (x^2 + 2*x + 1) * (x+1) |
+
+This is a simple way to align multi-line equations. =align*= is provided by
+the ~amsmath~ package.
+"
+   ;; (message "latex-align-env: %s" (buffer-string))
+   (goto-char (point-min))
+   (search-forward "equations:
+
+\\begin{align*}")
+   (search-forward "f(x) =& x^3 + 3*x^2 + 3*x + 1 \\")
+   (search-forward " =& (x^2 + 2*x + 1) * (x+1) \\")
+   (search-forward "\\end{align*}
+
+This is a simple")
+   ))
+
 (provide 'test-ox-latex)
 ;;; test-ox-latex.el ends here
-- 
2.53.0

Reply via email to