Skip to content

Commit c940b46

Browse files
Rudolf Adamkovičyantar92
authored andcommitted
ox-texinfo: Include LaTeX in Texinfo exports
* lisp/ox-texinfo.el (org-texinfo-with-latex): New customize. * lisp/ox-texinfo.el (org-texinfo-latex-environment): New function. * lisp/ox-texinfo.el (org-texinfo-latex-fragment): New function. * lisp/ox-texinfo.el (org-texinfo-supports-math-p): New function. * lisp/ox-texinfo.el (org-texinfo-supports-math--cache): New variable. * lisp/ox-texinfo.el (texinfo): Set latex-environment. * lisp/ox-texinfo.el (texinfo): Set latex-fragment. * testing/lisp/test-ox-texinfo.el: Add basic tests.
1 parent 4075662 commit c940b46

File tree

3 files changed

+396
-2
lines changed

3 files changed

+396
-2
lines changed

etc/ORG-NEWS

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,13 @@ support in LaTeX. The new =babel= syntax for loading languages via
489489
=ini= files and the new command =\babelprovide= (see:
490490
https://mirrors.ctan.org/macros/latex/required/babel/base/babel.pdf)
491491
are also supported.
492+
*** Texinfo exports include LaTeX
493+
494+
With the new customization option ~org-texinfo-with-latex~ set to (its
495+
default value) ~'detect~, if the system runs Texinfo 6.8 (3 July 2021)
496+
or newer, Org will export all LaTeX fragments and environments using
497+
Texinfo ~@math~ and ~@displaymath~ commands respectively.
498+
492499
* Version 9.5
493500

494501
** Important announcements and breaking changes

lisp/ox-texinfo.el

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
(require 'ox)
3434

3535
(defvar orgtbl-exp-regexp)
36-
36+
(defvar org-texinfo-supports-math--cache)
3737

3838

3939
;;; Define Back-End
@@ -58,6 +58,8 @@
5858
(italic . org-texinfo-italic)
5959
(item . org-texinfo-item)
6060
(keyword . org-texinfo-keyword)
61+
(latex-environment . org-texinfo-latex-environment)
62+
(latex-fragment . org-texinfo-latex-fragment)
6163
(line-break . org-texinfo-line-break)
6264
(link . org-texinfo-link)
6365
(node-property . org-texinfo-node-property)
@@ -123,7 +125,9 @@
123125
(:texinfo-text-markup-alist nil nil org-texinfo-text-markup-alist)
124126
(:texinfo-format-drawer-function nil nil org-texinfo-format-drawer-function)
125127
(:texinfo-format-inlinetask-function nil nil org-texinfo-format-inlinetask-function)
126-
(:texinfo-compact-itemx nil "compact-itemx" org-texinfo-compact-itemx)))
128+
(:texinfo-compact-itemx nil "compact-itemx" org-texinfo-compact-itemx)
129+
;; Redefine regular options.
130+
(:with-latex nil "tex" org-texinfo-with-latex)))
127131

128132

129133
;;; User Configurable Variables
@@ -358,6 +362,22 @@ The function should return the string to be exported."
358362
:group 'org-export-texinfo
359363
:type 'function)
360364

365+
;;;; LaTeX
366+
367+
(defcustom org-texinfo-with-latex (and org-export-with-latex 'detect)
368+
"When non-nil, the Texinfo exporter attempts to process LaTeX math.
369+
370+
When set to t, the exporter will process LaTeX environments and
371+
fragments as Texinfo \"@displaymath\" and \"@math\" commands
372+
respectively. Alternatively, when set to `detect', the exporter
373+
does so only if the installed version of Texinfo supports the
374+
necessary commands."
375+
:group 'org-export-texinfo
376+
:type '(choice
377+
(const :tag "Detect" detect)
378+
(const :tag "Yes" t)
379+
(const :tag "No" nil)))
380+
361381
;;;; Itemx
362382

363383
(defcustom org-texinfo-compact-itemx nil
@@ -1215,6 +1235,52 @@ CONTENTS is nil. INFO is a plist holding contextual information."
12151235
(concat "@listoffloats "
12161236
(org-export-translate "Listing" :utf-8 info))))))))
12171237

1238+
;;;; LaTeX Environment
1239+
1240+
(defun org-texinfo-latex-environment (environment _contents info)
1241+
"Transcode a LaTeX ENVIRONMENT from Org to Texinfo.
1242+
CONTENTS is ignored. INFO is a plist holding contextual information."
1243+
(let ((with-latex (plist-get info :with-latex)))
1244+
(when (or (eq with-latex t)
1245+
(and (eq with-latex 'detect)
1246+
(org-texinfo-supports-math-p)))
1247+
(let ((value (org-element-property :value environment)))
1248+
(string-join (list "@displaymath"
1249+
(string-trim (org-remove-indentation value))
1250+
"@end displaymath")
1251+
"\n")))))
1252+
1253+
;;;; LaTeX Fragment
1254+
1255+
(defun org-texinfo-latex-fragment (fragment _contents info)
1256+
"Transcode a LaTeX FRAGMENT from Org to Texinfo.
1257+
INFO is a plist holding contextual information."
1258+
(let ((with-latex (plist-get info :with-latex)))
1259+
(when (or (eq with-latex t)
1260+
(and (eq with-latex 'detect)
1261+
(org-texinfo-supports-math-p)))
1262+
(let ((value (org-remove-indentation
1263+
(org-element-property :value fragment))))
1264+
(cond
1265+
((or (string-match-p "^\\\\\\[" value)
1266+
(string-match-p "^\\$\\$" value))
1267+
(concat "\n"
1268+
"@displaymath"
1269+
"\n"
1270+
(string-trim (substring value 2 -2))
1271+
"\n"
1272+
"@end displaymath"
1273+
"\n"))
1274+
((string-match-p "^\\$" value)
1275+
(concat "@math{"
1276+
(string-trim (substring value 1 -1))
1277+
"}"))
1278+
((string-match-p "^\\\\(" value)
1279+
(concat "@math{"
1280+
(string-trim (substring value 2 -2))
1281+
"}"))
1282+
(t value))))))
1283+
12181284
;;;; Line Break
12191285

12201286
(defun org-texinfo-line-break (_line-break _contents _info)
@@ -1951,6 +2017,31 @@ Return INFO file name or an error if it couldn't be produced."
19512017
(message "Process completed.")
19522018
output))
19532019

2020+
(defun org-texinfo-supports-math-p ()
2021+
"Return t if the installed version of Texinfo supports \"@math\".
2022+
2023+
Once computed, the results remain cached."
2024+
(unless (boundp 'org-texinfo-supports-math--cache)
2025+
(setq org-texinfo-supports-math--cache
2026+
(let ((math-example "1 + 1 = 2"))
2027+
(let* ((input-file
2028+
(make-temp-file "test" nil ".info"))
2029+
(input-content
2030+
(concat (format "@setfilename %s" input-file) "\n"
2031+
"@node Top" "\n"
2032+
(format "@displaymath{%s}" math-example) "\n")))
2033+
(with-temp-file input-file
2034+
(insert input-content))
2035+
(let* ((output-file (org-texinfo-compile input-file))
2036+
(output-content (with-temp-buffer
2037+
(insert-file-contents output-file)
2038+
(buffer-string))))
2039+
(let ((result (string-match-p (regexp-quote math-example)
2040+
output-content)))
2041+
(delete-file input-file)
2042+
(delete-file output-file)
2043+
(if result t nil)))))))
2044+
org-texinfo-supports-math--cache)
19542045

19552046
(provide 'ox-texinfo)
19562047

0 commit comments

Comments
 (0)