www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

util0.rkt (3508B)


      1 #lang racket
      2 
      3 (require scribble/manual
      4          scribble/core
      5          scribble/latex-properties
      6          scribble/html-properties
      7          scribble-math
      8          scribble-math/mathjax-convert-unicode
      9          scriblib/render-cond)
     10 
     11 (provide mathtext
     12          $p
     13          (rename-out [$* $]
     14                      [$$* $$]))
     15 
     16 (define (mathtext . l)
     17   ;; TODO: (clean-$ l), but wrap converted stuff with $…$
     18   (apply elem #:style (style "mathText"
     19                              (list (tex-addition #"\\def\\mathText#1{#1}")
     20                                    'exact-chars))
     21          (clean-$ l #f)))
     22 
     23 ;; Math stuff
     24 (define (clean-$ e mathmode?)
     25   (cond [(pair? e) (cons (clean-$ (car e) mathmode?)
     26                          (clean-$ (cdr e) mathmode?))]
     27         [(traverse-element? e)
     28          (traverse-element (λ (a b)
     29                              (clean-$ ((traverse-element-traverse e) a b)
     30                                       mathmode?)))]
     31         [(match e
     32            [(element (style (or "math"
     33                                 (regexp #px"^texMathInline")
     34                                 (regexp #px"^texMathDisplay"))
     35                             _)
     36                      content)
     37             #t]
     38            [_ #f])
     39          ;; No need to go down recursively, as the contents should already have
     40          ;; been cleaned when the e was created. Plus we risk re-escaping
     41          ;; things within \text{…}.
     42          (if mathmode?
     43              (element-content e)
     44              (list "$" (element-content e) "$"))]
     45         [(match e
     46            [(element (style "mathText" _)
     47                      content)
     48             #t]
     49            [_ #f])
     50          ;; No need to go down recursively, as the contents should already have
     51          ;; been cleaned when the e was created. Plus we risk re-escaping
     52          ;; things within \text{…}.
     53          ;; TODO: when a "math" "texMathInline" "texMathDisplay" is encountered
     54          ;; within a "mathText", we should wrap it with $…$.
     55          (element-content e)]
     56         [(element? e)
     57          (element (element-style e)
     58                   (clean-$ (element-content e)
     59                            mathmode?))]
     60         [(string? e)
     61          ;; TODO: do this only when compiling to HTML.
     62          (mathjax-convert-unicode e mathmode?)]
     63         [else e]))
     64 
     65 (define ($* . elts)
     66   (apply $ (clean-$ elts #t)))
     67 
     68 (define ($$* . elts)
     69   (apply $$ (clean-$ elts #t)))
     70 
     71 (define tex-mathpar
     72   (string->bytes/utf-8 #<<EOTEX
     73 \def\texMathDisplayMathpar#1{\ifmmode #1\else\begin{mathpar}#1\end{mathpar}\fi}
     74 EOTEX
     75                        ))
     76 
     77 (define math-mathpar-style-latex
     78   (style "texMathDisplayMathpar" ;; MUST start with texMathDisplay.
     79          (list (tex-addition tex-mathpar)
     80                'exact-chars)))
     81 
     82 (define $p-css
     83   (string->bytes/utf-8 #<<EOF
     84 .mathpar {
     85     text-align: center;
     86     margin-top: 0.25em;
     87     margin-bottom: 0.25em;
     88     margin-left: -1.5em;
     89     margin-right: -1.5em;
     90 }
     91 
     92 .mathpar .MathJax_Display {
     93     width: auto;
     94     display: inline-block!important;
     95     margin-top: 0.75em;
     96     margin-bottom: 0.75em;
     97     margin-left: 1.5em;
     98     margin-right: 1.5em;
     99 }
    100 
    101 .mathpar::after {
    102     clear: both;
    103     content: "";
    104     display: block;
    105 }
    106 EOF
    107                  ))
    108 (define $p-html-style
    109   (style "mathpar" (list (alt-tag "div") (css-addition $p-css))))
    110 
    111 (define ($p . elts)
    112   (cond-element
    113    [latex (apply $$ #:latex-style math-mathpar-style-latex
    114                  (clean-$ (add-between elts "\\and") #t))]
    115    [else (apply elem #:style $p-html-style elts)]))