latexpict
(require latexpict)  package: latexpict 
;;; 
;;; TeX Formulas 
;;; 

;; This module contains a few functions that can turn a piece of TeX (string) 
;; into a pict that renders the formula. 

;; The module relies on pdflatex to render the formula into a pdf file 
;; and Poppler which is a C library, that renders pdf formula into Cairo contexts. 
;; Cairo being the underlying graphics libray or racket/draw (and picts). 
;; Note that introduces a dependecy on racketpopper. 

;; Results are cached to speed things up, when rendering the same document 
;; multiple times (caching is based on the entire tex document, so changes 
;; in options etc. will trigger a rerendering). 

;; TeX and LaTeX has three different rendering modes: 

;; text for the body text 
;; math for math formulas inline with text $ or \( \) 
;; displaymath for larger math formulass, not inline $$ or \[ \] 
;; 


;; Extracting a formula in "math" mode gives a tight bounding box around 
;; the formula. The mode "displaymath" on the other hand has large 
;; left and right margins. Therefore texdisplaymath renders its 
;; input as \( \displaystyle <yourformula> \). 
;; In case you really need the real display environment, you can 
;; use textrealdisplaymath, which uses \[ <yourformula> \]. 

;; The main exports of this library is therefore: 

(provide texmath ; \( <formula> \) 
texdisplaymath ; \( \displaystyle <formula> \) 
texrealdisplaymath) ; \[ <formula> \] 

;; You will notice, that there is export that renders plain text, 
;; but that is currently not supported by the "preview" package. 

;; When a snippet of TeX without any context is rendered, it 
;; is first put into a TeX document, then pdflatex is invoked, 
;; size information is extracted from the log file, and then 
;; the pdf file is used to produce the pict. 

;; It would be great if the user of this library didn't need to know 
;; any details, about the TeX document, but TeX can be difficult to 
;; work with. Especiallu if one doesn't know which document class 
;; and packages that are used. 

;; Here is the latex document that pdflatex will render to pdf, 
;; when the mode texmath is used: 

;; \documentclass{standalone} 
;; \usepackage[active,tightpage,lyx,pdftex,<modeoption>]{preview} 
;; \usepackage{amsmath} 
;; <yourpreamble> 
;; \begin{document} 
;; \( <yourlatexsnippet> \) 
;; \end{document} 

;; where <modeoption> is one of "textmath" and "displaymath", 
;; <yourpreamble> defaults as "", but you can put \usepackage{a_nice_pkg} here 
;; <yourlatexsnippet> is surrounded 

;; First of all the document classes "standalone" and the 
;; package "preview" is used. The combination renders the 
;; document into a pdf with a minimal margin around the context. 

;; The options for preview are: 
;; active  actually use preview (otherwise preview is ignored) 
;; tightpage  no margins, option is needed to use the pdftex option 
;; pdftex  assume PDFTeX is the output driver 
;; lyx  makes pdftex write size information in the log file 
;; (we use it to determine size and placement of baseline) 

;; The package "preview" by David Kastrup and the AUCTeX Team is available here: 
;; https://ctan.org/pkg/preview?lang=en 

;; The package "amsmath" by the American Mathematical Society contains 
;; most math related commands, that you will neeed. The documentation is here: 
;; https://ctan.org/pkg/amsmath?lang=en 

;; The document class "standalone" is documented here: 
;; https://ctan.org/pkg/standalone?lang=en 


;; The Racket functions texmath, texdisplaymath, and, readdisplaymath passes 
;; the appropriate <modeoption> automatically. 

;; The keyword argument #:documentclassoptions can be used to pass along 
;; options (in the form of a list of symbols/strings) to the "standalone" 
;; documentclass. Among the interesting options are 10pt, 11pt and 12pt. 

;; The option #:preamble allows you to pass along a preamble, where you 
;; can declare any packages you need. If you need the same preamble multiple 
;; times, you can use the parameter currentpremable. 
;; The default preamble is 
;; \usepackage{amsmath} 
;; If you set the preamble and still need amsmath, remember to include it 
;; in your own preamble. 

;; The option #:preview allows you to pass along options in the form 
;; of a list of symbols or strings to the "preview" TeX package. 
;; If you need the same options multiple times, you can use the 
;; parameter currentpreviewoptions 

(provide currentdocumentclassoptions currentpreamble currentpreviewoptions) 

;; Note that it is convenient to se the atnotation from Scribble to call, 
;; the text functions: 

;; #lang atexp racket 
;; (require metapict/tex) 
;; @texmath{ a^2 + b^2 = c^2 } 

;; Or perhaps introduce $ and $$ : 

;; #lang atexp racket 
;; (require metapict/tex) 
;; (define $ texmath) 
;; (define $$ texdisplaymath) 
;; @${ c = \sqrt{ a^2 + b^2 = c^2} } 

;; You can of course also use the functions without the atnotation: 
;; ($ "c = \\sqrt{ a^2 + b^2 = c^2}") 
;; Just remember that \ needs to be quoted as \\ in Racket strings. 


;;; 
;;; Configuration 
;;; 

; Preamble, used if the option #:preamble isn't passed. 
(define currentpreamble (makeparameter "")) 

; Document class options 
(define currentdocumentclassoptions (makeparameter '())) 

; Preview options 
(define currentpreviewoptions (makeparameter '())) 


; Scale factor applied to the pict before it is rendered. 
; The parameter value is used, if the option #:scale isn't used. 
(define currentscalefactor (makeparameter 2)) 
