From e03b29319b602b0dea3c15604d711010bddaa3ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rudolf=20Adamkovi=C4=8D?= Date: Sat, 26 Mar 2022 16:46:47 +0100 Subject: [PATCH] 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 (texinfo): Set latex-environment. * lisp/ox-texinfo.el (texinfo): Set latex-fragment. * testing/lisp/test-ox-texinfo.el: Add basic tests. Include inline and display style (La)TeX mathematics in Texinfo exports. --- etc/ORG-NEWS | 1 + lisp/ox-texinfo.el | 86 ++++++++++++- testing/lisp/test-ox-texinfo.el | 221 ++++++++++++++++++++++++++++++++ 3 files changed, 307 insertions(+), 1 deletion(-) create mode 100644 testing/lisp/test-ox-texinfo.el diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS index 00fe101dc..52fa881f3 100644 --- a/etc/ORG-NEWS +++ b/etc/ORG-NEWS @@ -270,6 +270,7 @@ example, prints a sub-bibliography containing the book entries with =ai= among their keywords. +*** Support for LaTeX mathematics in Texinfo exports ** New options *** A new custom setting =org-hide-drawer-startup= to control initial folding state of drawers diff --git a/lisp/ox-texinfo.el b/lisp/ox-texinfo.el index 1eec586fd..a88224197 100644 --- a/lisp/ox-texinfo.el +++ b/lisp/ox-texinfo.el @@ -55,6 +55,8 @@ (italic . org-texinfo-italic) (item . org-texinfo-item) (keyword . org-texinfo-keyword) + (latex-environment . org-texinfo-latex-environment) + (latex-fragment . org-texinfo-latex-fragment) (line-break . org-texinfo-line-break) (link . org-texinfo-link) (node-property . org-texinfo-node-property) @@ -120,7 +122,9 @@ (:texinfo-text-markup-alist nil nil org-texinfo-text-markup-alist) (:texinfo-format-drawer-function nil nil org-texinfo-format-drawer-function) (:texinfo-format-inlinetask-function nil nil org-texinfo-format-inlinetask-function) - (:texinfo-compact-itemx nil "compact-itemx" org-texinfo-compact-itemx))) + (:texinfo-compact-itemx nil "compact-itemx" org-texinfo-compact-itemx) + ;; Redefine regular options. + (:with-latex nil "tex" org-texinfo-with-latex))) ;;; User Configurable Variables @@ -355,6 +359,22 @@ The function should return the string to be exported." :group 'org-export-texinfo :type 'function) +;;;; LaTeX + +(defcustom org-texinfo-with-latex (and org-export-with-latex 'auto) + "If non-nil, the Texinfo exporter attempts to process LaTeX math. + +When set to t, the exporter always processes LaTeX environments +and fragments as Texinfo \"@displaymath\" and \"@math\" commands +respectively. Alternatively, when set to 'detect, the exporter +does so only if the installed version of Texinfo supports the +necessary commands." + :group 'org-export-texinfo + :type '(choice + (const :tag "Detect" detect) + (const :tag "Yes" t) + (const :tag "No" nil))) + ;;;; Itemx (defcustom org-texinfo-compact-itemx nil @@ -1212,6 +1232,52 @@ CONTENTS is nil. INFO is a plist holding contextual information." (concat "@listoffloats " (org-export-translate "Listing" :utf-8 info)))))))) +;;;; LaTeX Environment + +(defun org-texinfo-latex-environment (environment _contents info) + "Transcode a LaTeX ENVIRONMENT from Org to Texinfo. CONTENTS is +nil. INFO is a plist holding contextual information." + (let ((with-latex (plist-get info :with-latex))) + (when (or (eq with-latex t) + (and (eq with-latex 'detect) + (org-texinfo-supports-math-p))) + (let ((value (org-element-property :value environment))) + (string-join (list "@displaymath" + (string-trim (org-remove-indentation value)) + "@end displaymath") + "\n"))))) + +;;;; LaTeX Fragment + +(defun org-texinfo-latex-fragment (fragment _contents info) + "Transcode a LaTeX FRAGMENT from Org to Texinfo. CONTENTS is +nil. INFO is a plist holding contextual information." + (let ((with-latex (plist-get info :with-latex))) + (when (or (eq with-latex t) + (and (eq with-latex 'detect) + (org-texinfo-supports-math-p))) + (let ((value (org-remove-indentation + (org-element-property :value fragment)))) + (cond + ((or (string-match-p "^\\\\\\[" value) + (string-match-p "^\\$\\$" value)) + (concat "\n" + "@displaymath" + "\n" + (string-trim (substring value 2 -2)) + "\n" + "@end displaymath" + "\n")) + ((string-match-p "^\\$" value) + (concat "@math{" + (string-trim (substring value 1 -1)) + "}")) + ((string-match-p "^\\\\(" value) + (concat "@math{" + (string-trim (substring value 2 -2)) + "}")) + (t value)))))) + ;;;; Line Break (defun org-texinfo-line-break (_line-break _contents _info) @@ -1948,6 +2014,24 @@ Return INFO file name or an error if it couldn't be produced." (message "Process completed.") output)) +(defun org-texinfo-supports-math-p () + "Return t if the installed version of Texinfo supports \"@math\"." + (let ((math-example "1 + 1 = 2")) + (let* ((input-file (make-temp-file "test" nil ".info")) + (input-content (concat (format "@setfilename %s" input-file) "\n" + "@node Top" "\n" + (format "@math{%s}" math-example) "\n"))) + (with-temp-file input-file + (insert input-content)) + (let* ((output-file (org-texinfo-compile input-file)) + (output-content (with-temp-buffer + (insert-file-contents output-file) + (buffer-string)))) + (let ((result (string-match-p (regexp-quote math-example) + output-content))) + (delete-file input-file) + (delete-file output-file) + (if result t nil)))))) (provide 'ox-texinfo) diff --git a/testing/lisp/test-ox-texinfo.el b/testing/lisp/test-ox-texinfo.el new file mode 100644 index 000000000..316b7cb1d --- /dev/null +++ b/testing/lisp/test-ox-texinfo.el @@ -0,0 +1,221 @@ +;;; test-ox-texinfo.el --- Tests for ox-texinfo.el + +;; Copyright (C) 2022 Rudolf Adamkovič + +;; Author: Rudolf Adamkovič + +;; This file is not part of GNU Emacs. + +;; This program is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with this program. If not, see . + +;;; Code: + +(require 'cl-lib) +(require 'ox-texinfo) + +(unless (featurep 'ox-texinfo) + (signal 'missing-test-dependency "org-export-texinfo")) + +(ert-deftest test-org-export-texinfo/latex-fragment () + "Test `org-texinfo-latex-fragment' output." + + ;; inline TeX fragment + (should + (equal "@math{a^2 = b}" + (org-texinfo-latex-fragment + (org-element-create 'latex-fragment + '(:value "$a^2 = b$")) + nil + '(:with-latex t)))) + + ;; inline TeX fragment, padded + (should + (equal "@math{a^2 = b}" + (org-texinfo-latex-fragment + (org-element-create 'latex-fragment + '(:value "$ a^2 = b $")) + nil + '(:with-latex t)))) + + ;; inline LaTeX fragment + (should + (equal "@math{a^2 = b}" + (org-texinfo-latex-fragment + (org-element-create 'latex-fragment + '(:value "\\(a^2 = b\\)")) + nil + '(:with-latex t)))) + + ;; inline LaTeX fragment, padded + (should + (equal "@math{a^2 = b}" + (org-texinfo-latex-fragment + (org-element-create 'latex-fragment + '(:value "\\( a^2 = b \\)")) + nil + '(:with-latex t)))) + + ;; displayed TeX fragment, inline + (should + (equal (string-join + (list "" + "@displaymath" + "a ^ 2 = b" + "@end displaymath" + "") + "\n") + (org-texinfo-latex-fragment + (org-element-create 'latex-fragment + (list :value "$$a ^ 2 = b$$")) + nil + '(:with-latex t)))) + + ;; displayed TeX fragment, inline, padded + (should + (equal (string-join + (list "" + "@displaymath" + "a ^ 2 = b" + "@end displaymath" + "") + "\n") + (org-texinfo-latex-fragment + (org-element-create 'latex-fragment + (list :value "$$ a ^ 2 = b $$")) + nil + '(:with-latex t)))) + + ;; displayed TeX fragment, multi-line + (should + (equal (string-join + (list "" + "@displaymath" + "a ^ 2 = b" + "b ^ 2 = c" + "@end displaymath" + "") + "\n") + (org-texinfo-latex-fragment + (org-element-create 'latex-fragment + (list :value + (string-join + (list "$$" + "a ^ 2 = b" + "b ^ 2 = c" + "$$") + "\n"))) + nil + '(:with-latex t)))) + + ;; displayed TeX fragment, multi-line, indented + (should + (equal (string-join + (list "" + "@displaymath" + "a ^ 2 = b" + "b ^ 2 = c" + "@end displaymath" + "") + "\n") + (org-texinfo-latex-fragment + (org-element-create 'latex-fragment + (list :value + (string-join + (list " $$" + " a ^ 2 = b" + " b ^ 2 = c" + " $$") + "\n"))) + nil + '(:with-latex t)))) + + ;; displayed LaTeX fragment, inline + (should + (equal (string-join + (list "" + "@displaymath" + "a ^ 2 = b" + "@end displaymath" + "") + "\n") + (org-texinfo-latex-fragment + (org-element-create 'latex-fragment + (list :value "\\[a ^ 2 = b\\]")) + nil + '(:with-latex t)))) + + ;; displayed LaTeX fragment, inline, padded + (should + (equal (string-join + (list "" + "@displaymath" + "a ^ 2 = b" + "@end displaymath" + "") + "\n") + (org-texinfo-latex-fragment + (org-element-create 'latex-fragment + (list :value "\\[ a ^ 2 = b \\]")) + nil + '(:with-latex t))))) + +(ert-deftest test-org-export-texinfo/latex-environment () + "Test `org-texinfo-latex-environment' output." + + ;; LaTeX environment + (should + (equal (string-join + (list "@displaymath" + "\\begin{equation}" + "a ^ 2 = b" + "b ^ 2 = c" + "\\end{equation}" + "@end displaymath") + "\n") + (org-texinfo-latex-environment + (org-element-create 'latex-environment + (list :value + (string-join + (list "\\begin{equation}" + "a ^ 2 = b" + "b ^ 2 = c" + "\\end{equation}") + "\n"))) + nil + '(:with-latex t)))) + + ;; LaTeX environment, indented + (should + (equal (string-join + (list "@displaymath" + "\\begin{equation}" + "a ^ 2 = b" + "b ^ 2 = c" + "\\end{equation}" + "@end displaymath") + "\n") + (org-texinfo-latex-environment + (org-element-create 'latex-environment + (list :value + (string-join + (list " \\begin{equation}" + " a ^ 2 = b" + " b ^ 2 = c" + " \\end{equation}") + "\n"))) + nil + '(:with-latex t))))) + +(provide 'test-ox-texinfo) +;;; test-ox-texinfo.el end here -- 2.37.1