-
Notifications
You must be signed in to change notification settings - Fork 13
/
tex_renderer.go
194 lines (175 loc) · 4.64 KB
/
tex_renderer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package mathjax
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"strings"
)
const common = `
\documentclass[preview]{standalone}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{stmaryrd}
\begin{document}
%s
\end{document}
`
const displayInlineFormula = `
\documentclass[11pt]{article}
\usepackage[active,tightpage,textmath]{preview}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{stmaryrd}
\begin{document}
\(%s\)
\end{document}
`
const displayBlockFormula = `
\documentclass[11pt,preview]{standalone}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{stmaryrd}
\begin{document}
\begin{equation*}
%s
\end{equation*}
\end{document}
`
const tmpl = `
\documentclass[11pt]{article}
\usepackage[paperwidth=180in,paperheight=180in]{geometry}
\batchmode
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{stmaryrd}
\usepackage[verbose]{newunicodechar}
\pagestyle{empty}
\setlength{\topskip}{0pt}
\setlength{\parindent}{0pt}
\setlength{\abovedisplayskip}{0pt}
\setlength{\belowdisplayskip}{0pt}
\begin{document}
%s
\end{document}
`
const tikz = `
\documentclass[11pt]{article}
\usepackage{tikz}
\usepackage{lipsum}
\usepackage{paralist,pst-func, pst-plot, pst-math, pstricks-add,pgfplots}
\usetikzlibrary{patterns,matrix,arrows}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{1pt}
\begin{document}
%s
\end{document}
`
type TexRenderer struct {
texPath string
docTemplate string
inlineFormulaImpl string
commonBlockTmpl string
blockFormulaTmpl string
tikzTmpl string
tmpDir string
}
func NewDefaultTexRenderer() *TexRenderer {
var wd, _ = os.Getwd()
var texPath = os.Getenv("TEX_PATH")
var tmpDir = wd + "/tmp/"
var defaultRenderer = &TexRenderer{
texPath: texPath,
docTemplate: tmpl,
inlineFormulaImpl: displayInlineFormula,
commonBlockTmpl: common,
blockFormulaTmpl: displayBlockFormula,
tmpDir: tmpDir,
tikzTmpl: tikz,
}
return defaultRenderer
}
func (r *TexRenderer) RunInline(formula string) []byte {
return r.runRaw(fmt.Sprintf(r.inlineFormulaImpl, formula))
}
func (r *TexRenderer) Run(formula string) []byte {
var tmpl string
formula = strings.TrimSpace(formula)
if strings.Contains(formula, `\begin{tikzpicture}`) {
tmpl = r.tikzTmpl
} else if (strings.HasPrefix(formula, `\begin{`)) {
tmpl = r.commonBlockTmpl
} else {
tmpl = r.blockFormulaTmpl
}
return r.runRaw(fmt.Sprintf(tmpl, strings.TrimSpace(formula)))
}
func (r *TexRenderer) runRaw(formula string) []byte {
f, err := ioutil.TempFile(r.tmpDir, "doc")
if err != nil {
log.Fatalf("%v", err)
}
f.WriteString(formula)
f.Sync()
f.Close()
r.runPdfLatex(f.Name())
r.runPdf2Svg(f.Name())
svgf, err := os.Open(f.Name() + ".svg")
if err != nil {
return nil
}
svg, err := ioutil.ReadAll(svgf)
if err != nil {
return nil
}
return svg
}
func (r *TexRenderer) runDvi2Svg(fname string) {
// fmt.Println([]string{fmt.Sprintf("%sdvisvgm", r.texPath), fmt.Sprintf("%s.dvi", fname), "-o", fmt.Sprintf("%s.svg", fname), "-n", "--exact", "-v0", "--relative", "--zoom=1.2546875"})
cmd := exec.Command(fmt.Sprintf("%sdvisvgm", r.texPath), fmt.Sprintf("%s.dvi", fname), "-o", fmt.Sprintf("%s.svg", fname), "-n", "--exact", "-v0", "--relative", "--zoom=1.2546875")
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
fmt.Printf("dvi2svg cmd.Run() failed with %s\n", err)
}
// outStr, errStr := string(stdout.Bytes()), string(stderr.Bytes())
// fmt.Printf("out:\n%s\nerr:\n%s\n", outStr, errStr)
}
func (r *TexRenderer) runLatex(fname string) {
cmd := exec.Command(fmt.Sprintf("%slatex", r.texPath), "-output-directory", r.tmpDir, fname)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
fmt.Printf("latex cmd.Run() failed with %s\n", err)
}
// outStr, errStr := string(stdout.Bytes()), string(stderr.Bytes())
// fmt.Printf("out:\n%s\nerr:\n%s\n", outStr, errStr)
}
func (r *TexRenderer) runPdfLatex(fname string) {
cmd := exec.Command(fmt.Sprintf("%spdflatex", r.texPath), "-output-directory", r.tmpDir, fname)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
fmt.Printf("pdflatex %s cmd.Run() failed with %s\n", fname, err)
}
}
func (r *TexRenderer) runPdf2Svg(fname string) {
cmd := exec.Command("pdf2svg", fmt.Sprintf("%s.pdf", fname), fmt.Sprintf("%s.svg", fname))
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
fmt.Printf("pdf2svg cmd.Run() failed with %s\n", err)
}
}