From mboxrd@z Thu Jan 1 00:00:00 1970 From: Thibault Marin Subject: Re: Equation references in HTML export Date: Tue, 16 Jan 2018 22:39:39 -0600 Message-ID: <87k1whgktw.fsf@dell-desktop.WORKGROUP> References: <87k1ww50uj.fsf@dell-desktop.WORKGROUP> <87shbj6zh5.fsf@nicolasgoaziou.fr> <87k1wughjp.fsf@dell-desktop.WORKGROUP> <87h8ru3eq8.fsf@nicolasgoaziou.fr> <87h8rtf3bo.fsf@dell-desktop.WORKGROUP> <87efmpy8u2.fsf@nicolasgoaziou.fr> Reply-To: thibault.marin@gmx.com Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:39082) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ebfVs-0005Fa-Vz for emacs-orgmode@gnu.org; Tue, 16 Jan 2018 23:39:50 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ebfVp-0006Qb-Pv for emacs-orgmode@gnu.org; Tue, 16 Jan 2018 23:39:49 -0500 Received: from mout.gmx.net ([212.227.15.19]:52040) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ebfVp-0006PA-AH for emacs-orgmode@gnu.org; Tue, 16 Jan 2018 23:39:45 -0500 In-reply-to: <87efmpy8u2.fsf@nicolasgoaziou.fr> List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: Nicolas Goaziou Cc: emacs-org list --=-=-= Content-Type: text/plain Hi, thanks for the review. Please find attached an updated patch. > I guess so. OTOH, I assume latex environments are always math > environments. One can use LaTex export blocks for "regular" LaTeX. I left the math check in the attached patch for now. Please let me know if you would like me to remove it. > Nitpick: since this is a predicate, it should be named > `org-html--math-environment-p'. Done. > Meanwhile, I think the implementation is a bit convoluted. What about > the following? > > (replace-regexp-in-string > "\\`[ \t]*\\\\begin{\\([^*]+?\\)}" > "\\1*" > (replace-regexp-in-string "^[ \t]*\\\\end{\\([^*]+?\\)}[ \r\t\n]*\\'" > "\\1*" > latex-frag nil nil 1) > nil nil 1) Done. > IMO, this doesn't deserve to be a function, you can just use something > like this: > > (if (org-string-nw-p label) > (replace-regexp-in-string "\\`.*" > (format "\\&\n\\\\label{%s}" label) > latex-frag) > latex-frag) Done. Thanks, thibault --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-ox-html.el-Add-label-and-number-to-equations-in-HTML.patch >From c600a62d67decf957e9170cf9f055915bdfae05a Mon Sep 17 00:00:00 2001 From: thibault Date: Sun, 7 Jan 2018 03:04:39 -0600 Subject: [PATCH] ox-html.el: Add label and number to equations in HTML export * lisp/ox-html.el (org-html--wrap-latex-environment): New function wrapping the content of latex environments in HTML
tags, adding "id" and equation number. (org-html--math-environment-p): New function determining if a latex environment is a math environment. (org-html-latex-environment): Use `org-html--wrap-latex-environment' to wrap equation in HTML container (non-MathJax modes). Make latex environment unnumbered when compiling equations to images. Insert latex label in environment in MathJax mode. (org-html-link): Calculate equation number limiting counter to equation environments. Use eqref for link when using MathJax (org-html--make-unlabelled-latex-environment): New function to convert latex math environments to unnumbered versions (e.g. "equation" -> "equation*"). --- lisp/ox-html.el | 128 ++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 101 insertions(+), 27 deletions(-) diff --git a/lisp/ox-html.el b/lisp/ox-html.el index d242c613c..7b7d52141 100644 --- a/lisp/ox-html.el +++ b/lisp/ox-html.el @@ -430,6 +430,19 @@ for the JavaScript code in this tag. .footdef { margin-bottom: 1em; } .figure { padding: 1em; } .figure p { text-align: center; } + .equation-container { + display: table; + text-align: center; + width: 100%; + } + .equation { + vertical-align: middle; + } + .equation-label { + display: table-cell; + text-align: right; + vertical-align: middle; + } .inlinetask { padding: 10px; border: 2px solid gray; @@ -2823,26 +2836,75 @@ INFO is a plist containing export properties." "Creating LaTeX Image..." nil processing-type) (buffer-string)))) +(defun org-html--wrap-latex-environment (contents info &optional caption label) + "Wrap CONTENTS string within appropriate environment for equations. +INFO is a plist used as a communication channel. When optional +arguments CAPTION and LABEL are given, use them for caption and +\"id\" attribute." + (format "\n\n%s%s\n
" + ;; ID. + (if (org-string-nw-p label) (format " id=\"%s\"" label) "") + ;; Contents. + (format "\n%s\n" contents) + ;; Caption. + (if (not (org-string-nw-p caption)) "" + (format "\n\n%s\n" + caption)))) + +(defun org-html--math-environment-p (element &optional info) + "Non-nil when ELEMENT is a LaTeX math environment. +Math environments match the regular expression defined in +`org-latex-math-environments-re'. +INFO is a plist used as a communication channel. This function +is meant to be used as a predicate for `org-export-get-ordinal' or +a value to `org-html-standalone-image-predicate'." + (string-match-p org-latex-math-environments-re + (org-element-property :value element))) + +(defun org-html--make-unlabelled-latex-environment (latex-frag) + "Change environment in LATEX-FRAG to an unnumbered environment. +For instance, change an 'equation' environment to 'equation*'." + (replace-regexp-in-string + "\\`[ \t]*\\\\begin{\\([^*]+?\\)}" + "\\1*" + (replace-regexp-in-string "^[ \t]*\\\\end{\\([^*]+?\\)}[ \r\t\n]*\\'" + "\\1*" + latex-frag nil nil 1) + nil nil 1)) + (defun org-html-latex-environment (latex-environment _contents info) "Transcode a LATEX-ENVIRONMENT element from Org to HTML. CONTENTS is nil. INFO is a plist holding contextual information." (let ((processing-type (plist-get info :with-latex)) (latex-frag (org-remove-indentation (org-element-property :value latex-environment))) - (attributes (org-export-read-attribute :attr_html latex-environment))) + (attributes (org-export-read-attribute :attr_html latex-environment)) + (label (and (org-element-property :name latex-environment) + (org-export-get-reference latex-environment info))) + (caption (number-to-string + (org-export-get-ordinal + latex-environment info nil + #'org-html--math-environment-p)))) (cond ((memq processing-type '(t mathjax)) - (org-html-format-latex latex-frag 'mathjax info)) + (org-html-format-latex + (if (org-string-nw-p label) + (replace-regexp-in-string "\\`.*" + (format "\\&\n\\\\label{%s}" label) + latex-frag) + latex-frag) + 'mathjax info)) ((assq processing-type org-preview-latex-process-alist) (let ((formula-link - (org-html-format-latex latex-frag processing-type info))) - (when (and formula-link (string-match "file:\\([^]]*\\)" formula-link)) - ;; Do not provide a caption or a name to be consistent with - ;; `mathjax' handling. - (org-html--wrap-image - (org-html--format-image - (match-string 1 formula-link) attributes info) info)))) - (t latex-frag)))) + (org-html-format-latex + (org-html--make-unlabelled-latex-environment latex-frag) + processing-type info))) + (when (and formula-link (string-match "file:\\([^]]*\\)" formula-link)) + (org-html--wrap-latex-environment + (org-html--format-image + (match-string 1 formula-link) attributes info) + info caption label)))) + (t (org-html--wrap-latex-environment latex-frag info caption label))))) ;;;; Latex Fragment @@ -3062,23 +3124,35 @@ INFO is a plist holding contextual information. See (format "%s" href attributes desc))) ;; Fuzzy link points to a target or an element. (_ - (let* ((ref (org-export-get-reference destination info)) - (org-html-standalone-image-predicate - #'org-html--has-caption-p) - (number (cond - (desc nil) - ((org-html-standalone-image-p destination info) - (org-export-get-ordinal - (org-element-map destination 'link - #'identity info t) - info 'link 'org-html-standalone-image-p)) - (t (org-export-get-ordinal - destination info nil 'org-html--has-caption-p)))) - (desc (cond (desc) - ((not number) "No description for this link") - ((numberp number) (number-to-string number)) - (t (mapconcat #'number-to-string number "."))))) - (format "%s" ref attributes desc)))))) + (if (and destination + (memq (plist-get info :with-latex) '(mathjax t)) + (eq 'latex-environment (org-element-type destination)) + (eq 'math (org-latex--environment-type destination))) + ;; Caption and labels are introduced within LaTeX environment. Use + ;; "eqref" macro to refer to those in the document. + (format "\\eqref{%s}" + (org-export-get-reference destination info)) + (let* ((ref (org-export-get-reference destination info)) + (org-html-standalone-image-predicate + #'org-html--has-caption-p) + (ordinal-counter-predicate + (if (string= (car destination) "latex-environment") + #'org-html--math-environment-p + #'org-html--has-caption-p)) + (number (cond + (desc nil) + ((org-html-standalone-image-p destination info) + (org-export-get-ordinal + (org-element-map destination 'link + #'identity info t) + info 'link 'org-html-standalone-image-p)) + (t (org-export-get-ordinal + destination info nil ordinal-counter-predicate)))) + (desc (cond (desc) + ((not number) "No description for this link") + ((numberp number) (number-to-string number)) + (t (mapconcat #'number-to-string number "."))))) + (format "%s" ref attributes desc))))))) ;; Coderef: replace link with the reference name or the ;; equivalent line number. ((string= type "coderef") -- 2.15.1 --=-=-=--