Automatically convert some unicode characters to their LaTeX command

This commit is contained in:
Georges Dupéron 2017-01-16 02:21:12 +01:00
parent 9a515b72f4
commit f761999a1b
2 changed files with 102 additions and 3 deletions

View File

@ -6,7 +6,9 @@
scribble/latex-properties
scriblib/render-cond
racket/runtime-path
setup/collects)
setup/collects
"katex-convert-unicode.rkt"
racket/list)
(provide $
$$
@ -190,13 +192,15 @@ EOTEX
(make-element math-inline-style-mathjax `("$" ,@strs "$")))
(define ($-katex strs)
(make-element math-inline-style-katex strs))
(make-element math-inline-style-katex
(map katex-convert-unicode (flatten strs))))
(define ($$-mathjax strs)
(make-element math-display-style-mathjax `("\\[" ,@strs "\\]")))
(define ($$-katex strs)
(make-element math-display-style-katex strs))
(make-element math-display-style-katex
(map katex-convert-unicode (flatten strs))))
(define $-html-handler (make-parameter $-katex))
(define $$-html-handler (make-parameter $$-katex))

95
katex-convert-unicode.rkt Normal file
View File

@ -0,0 +1,95 @@
#lang racket/base
(require racket/string)
(provide katex-convert-unicode)
(define (literal-alternatives→regexp literal-alternatives)
(string-append "("
(string-join (map regexp-quote literal-alternatives) "|")
")"))
(define (string-replace* str sym→*)
(define →* (map (λ (x)
(cons (symbol->string (car x))
(cadr x)))
sym→*))
(define hash→* (make-immutable-hash →*))
(regexp-replace* (literal-alternatives→regexp (map car →*))
str
(λ (found . _)
(hash-ref hash→* found))))
(define (katex-convert-unicode str)
(if (string? str)
(string-replace*
str
'([ "_0"]
[ "_1"]
[ "_2"]
[ "_3"]
[ "_4"]
[ "_5"]
[ "_6"]
[ "_7"]
[ "_8"]
[ "_9"]
[ "_i"]
[ "_j"]
[ "_k"]
[ "_l"]
[ "_m"]
[ "_n"]
[ "_o"]
[ "^0"]
[¹ "^1"]
[² "^2"]
[³ "^3"]
[ "^4"]
[ "^5"]
[ "^6"]
[ "^7"]
[ "^8"]
[ "^9"]
[ "^i"]
[ʲ "^j"]
[ "^k"]
[ˡ "^l"]
[ "^m"]
[ "^n"]
[ "^o"]
[ "\\subseteq" "\\ensuremath{\\subseteq}"]
[ "\\subset" "\\ensuremath{\\subset}"]
[ "\\supseteq" "\\ensuremath{\\supseteq}"]
[ "\\supset" "\\ensuremath{\\supset}"]
[ "\\rightarrow" "\\ensuremath{\\rightarrow}"]
[ "\\Rightarrow" "\\ensuremath{\\Rightarrow}"]
[ "\\leftarrow" "\\ensuremath{\\leftarrow}"]
[ "\\Leftarrow" "\\ensuremath{\\Leftarrow}"]
[ "\\leftrightarrow" "\\ensuremath{\\leftrightarrow}"]
[ "\\Leftrightarrow" "\\ensuremath{\\Leftrightarrow}"]
;; Partially extracted from my .XCompose generator
[ñ "\\tilde{n}" "{\\ifmmode\\tilde{n}\\else\\~{n}\\fi}"]
[Ñ "\\tilde{N}" "{\\ifmmode\\tilde{N}\\else\\~{N}\\fi}"]
[ "\\star" "\\ensuremath{\\star}"]
[ "\\ddot{\\star}}" "\\ensuremath{\\ddot{\\star}}"]
[ "^*" "^*"]
[ "\\langle" "\\ensuremath{\\mathsmaller{\\raisemath{.15ex}{\\langle}}}"]
[ "\\rangle" "\\ensuremath{\\mathsmaller{\\raisemath{.15ex}{\\rangle}}}"]
[ "\\cdots"]
[ "\\vdots"]
[ "\\iddots"]
[ "\\ddots"]
[ "\\mathbin{+\\mkern-6.5mu+}" "\\ensuremath{\\mathbin{+\\mkern-6.5mu+}}"]
[ "\\emptyset" "\\ensuremath{\\emptyset}"]
[ı⃗ "\\vec{\\i}}" "\\ensuremath{\\vec{\\i}"]
[ "\\oplus" "\\ensuremath{\\oplus}"]
[ "\\ominus" "\\ensuremath{\\ominus}"]
[ "\\Cup" "\\ensuremath{\\Cup}"]
;[ₗ "\\ensuremath{_{l}}"]
[ "::"]
[Λ "\\Lambda" "\\ensuremath{\\Lambda}"]
[ "\\forall"]
[ "\\exists"]
[ "\\equiv"]
[ "\not\\equiv"]))
str))