Permalink
Browse files

Support exporting latex equation fragments

- ox-blackfriday.el updated to do the escaping using backslashes as
  suggested in https://gohugo.io/content-management/formats#solution

> One solution is to simply escape each underscore in your math code
  by entering \_ instead of _.

Org reference for latex fragments:
http://orgmode.org/manual/LaTeX-fragments.html

Add test.
  • Loading branch information...
1 parent 5b966ad commit 8da68a667657c2f0fc71f0eb2b17856ca5c6c5be @kaushalmodi committed Jul 31, 2017
@@ -684,6 +684,28 @@ This gets both categories =catA= and =catB=.
:CUSTOM_ID: link-heading-2
:END:
- Link to [[#link-heading-1][Heading 1]]
+* Equations :equations:
+** Inline equations
+:PROPERTIES:
+:EXPORT_FILE_NAME: equation-latex-frag
+:EXPORT_DATE: 2017-07-31
+:END:
+Wrap the equations between =\(= and =\)=.
+
+For example, below in Org:
+#+BEGIN_SRC text
+LaTeX formatted equation: \( E = -J \sum_{i=1}^N s_i s_{i+1} \)
+#+END_SRC
+
+will look like this in Hugo rendered HTML (don't see this in Markdown,
+see what it looks after Hugo has processed it).
+
+LaTeX formatted equation: \( E = -J \sum_{i=1}^N s_i s_{i+1 }\)
+
+Here's another example similar to one in [[http://orgmode.org/manual/LaTeX-fragments.html][(org) LaTeX fragments]].
+
+If $a^2=b$ and \( b=2 \), then the solution must be either
+$a=+\sqrt{2}$ or \(a=-\sqrt{2}\).
* TODO Pre-Draft State
:PROPERTIES:
:EXPORT_FILE_NAME: draft-state-todo
@@ -0,0 +1,24 @@
++++
+title = "Inline equations"
+date = 2017-07-31
+tags = ["equations"]
+draft = false
++++
+
+Wrap the equations between `\(` and `\)`.
+
+For example, below in Org:
+
+```text
+LaTeX formatted equation: \( E = -J \sum_{i=1}^N s_i s_{i+1} \)
+```
+
+will look like this in Hugo rendered HTML (don't see this in Markdown,
+see what it looks after Hugo has processed it).
+
+LaTeX formatted equation: \\( E = -J \sum\_{i=1}^N s\_i s\_{i+1 }\\)
+
+Here's another example similar to one in [(org) LaTeX fragments](http://orgmode.org/manual/LaTeX-fragments.html).
+
+If \\(a^2=b\\) and \\( b=2 \\), then the solution must be either
+\\(a=+\sqrt{2}\\) or \\(a=-\sqrt{2}\\).
@@ -11,11 +11,10 @@
{{ .Title }} ❚ {{ .Site.Title }}
{{ end }}
</title>
+ {{ block "head" . }}{{ end }}
</head>
<body lang="en">
-
{{ block "main" . }}{{ end }}
-
</body>
</html>
@@ -1,3 +1,32 @@
+{{ define "head" }}
+
+<script type="text/x-mathjax-config">
+ MathJax.Hub.Config({
+ displayAlign: "center",
+ displayIndent: "0em",
+
+ "HTML-CSS": { scale: 100,
+ linebreaks: { automatic: "false" },
+ webFont: "TeX"
+ },
+ SVG: {scale: 100,
+ linebreaks: { automatic: "false" },
+ font: "TeX"},
+ NativeMML: {scale: 100},
+ TeX: { equationNumbers: {autoNumber: "AMS"},
+ MultLineWidth: "85%",
+ TagSide: "right",
+ TagIndent: ".8em"
+ }
+ });
+</script>
+<!-- https://gohugo.io/content-management/formats/#mathjax-with-hugo -->
+<script type="text/javascript"
+ src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS_HTML"></script>
+</head>
+
+{{ end }}
+
{{ define "main" }}
<div class="post">
View
@@ -69,6 +69,7 @@
(if a (org-blackfriday-export-to-markdown t s v)
(org-open-file (org-blackfriday-export-to-markdown nil s v)))))))
:translate-alist '((inner-template . org-blackfriday-inner-template)
+ (latex-fragment . org-blackfriday-latex-fragment)
(paragraph . org-blackfriday-paragraph)
(strike-through . org-blackfriday-strike-through)
(src-block . org-blackfriday-src-block)
@@ -81,6 +82,43 @@
;;; Transcode Functions
+;;;; Inner Template
+(defun org-blackfriday-inner-template (contents info)
+ "Return body of document after converting it to Markdown syntax.
+CONTENTS is the transcoded contents string. INFO is a plist
+holding export options."
+ (let* ((depth (plist-get info :with-toc))
+ (headlines (and depth (org-export-collect-headlines info depth)))
+ (toc-tail (if headlines "\n\n" ""))
+ (toc-string ""))
+
+ (when headlines
+ (dolist (headline headlines)
+ (setq toc-string (concat toc-string
+ (org-blackfriday-format-toc headline info)
+ "\n"))))
+ (org-trim (concat toc-string toc-tail contents "\n" (org-blackfriday-footnote-section info)))))
+
+;;;; Latex Fragment
+(defun org-blackfriday-latex-fragment (latex-fragment _contents info)
+ "Transcode a LATEX-FRAGMENT object from Org to Blackfriday Markdown.
+INFO is a plist holding contextual information."
+ (let ((latex-frag (org-element-property :value latex-fragment))
+ (processing-type (plist-get info :with-latex)))
+ (cond
+ ((memq processing-type '(t mathjax))
+ (let* ((frag (org-html-format-latex latex-frag 'mathjax info))
+ ;; https://gohugo.io/content-management/formats#solution
+ (frag (replace-regexp-in-string "\\(\\\\[()]\\)" "\\\\\\1" frag)) ;\( -> \\(, \) -> \\)
+ (frag (replace-regexp-in-string "_" "\\\\_" frag))) ;_ -> \_
+ frag))
+ ((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))
+ (org-html--format-image (match-string 1 formula-link) nil info))))
+ (t latex-frag))))
+
;;;; Paragraph
(defun org-blackfriday-paragraph (paragraph contents info)
"Transcode PARAGRAPH element into Blackfriday Markdown format.
@@ -314,24 +352,7 @@ INFO is a plist used as a communication channel."
fn-alist
"\n"))))))
-;;;; Template
-(defun org-blackfriday-inner-template (contents info)
- "Return body of document after converting it to Markdown syntax.
-CONTENTS is the transcoded contents string. INFO is a plist
-holding export options."
- (let* ((depth (plist-get info :with-toc))
- (headlines (and depth (org-export-collect-headlines info depth)))
- (toc-tail (if headlines "\n\n" ""))
- (toc-string ""))
-
- (when headlines
- (dolist (headline headlines)
- (setq toc-string (concat toc-string
- (org-blackfriday-format-toc headline info)
- "\n"))))
- (org-trim (concat toc-string toc-tail contents "\n" (org-blackfriday-footnote-section info)))))
-
-
+
;;; Interactive functions
;;;###autoload

0 comments on commit 8da68a6

Please sign in to comment.