Repository navigation
Expand file tree
/
Copy pathmathml_tex.go
More file actions
252 lines (238 loc) · 7.38 KB
/
Copy pathmathml_tex.go
File metadata and controls
252 lines (238 loc) · 7.38 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package crossrefapi
import (
"regexp"
"strings"
"unicode"
"unicode/utf8"
)
// texPiece is the TeX of part of a formula and how many MathML tokens it holds;
// a piece of more than one token is put in braces when it is the base of a
// script.
type texPiece struct {
text string
tokens int
}
// letterCommandEnd matches a TeX command named with letters at the end of a
// string: such a command needs a space before a letter or digit that follows.
var letterCommandEnd = regexp.MustCompile(`\\[A-Za-z]+$`)
// mathTeX converts a MathML formula to TeX (without the surrounding math
// delimiters). Tokens become TeX through the symbol table (Greek letters and
// operators to commands; a multi-letter identifier to \mathrm{}; a mathvariant
// to its font command; mtext to \text{}), scripts become ^{} and _{}, fractions
// and roots \frac{}{}, \sqrt{}, \sqrt[]{}, and an over-accent a command from
// the table's accents. Any other element is reduced to its text inside \text{}
// with a warning.
func mathTeX(n *node, sym *Symbols, warnings *[]Warning) string {
return texNode(n, sym, warnings).text
}
// texNode converts one node of a formula.
func texNode(n *node, sym *Symbols, warnings *[]Warning) texPiece {
switch n.name {
case "":
return texToken(n.text, sym, texEscape)
case "math", "mrow", "mstyle", "mpadded":
return joinTeX(texKids(n, sym, warnings))
case "mi":
return texIdentifier(n, sym)
case "mn":
return texToken(allText(n), sym, texEscape)
case "mo":
return texToken(allText(n), sym, func(t string) string {
if cmd, ok := sym.TeX[t]; ok {
return cmd
}
return texEscape(t)
})
case "mtext", "ms":
return texToken(allText(n), sym, func(t string) string { return texText(t, sym) })
case "mspace":
return texPiece{"~", 1}
case "msup", "msub":
kids := mathChildren(n)
if len(kids) != 2 {
return texUnconverted(n, sym, warnings)
}
op := "_"
if n.name == "msup" {
op = "^"
}
base := texNode(kids[0], sym, warnings)
script := texNode(kids[1], sym, warnings)
return texPiece{texBase(base) + op + "{" + script.text + "}", base.tokens + script.tokens}
case "msubsup", "munderover":
kids := mathChildren(n)
if len(kids) != 3 {
return texUnconverted(n, sym, warnings)
}
base := texNode(kids[0], sym, warnings)
low := texNode(kids[1], sym, warnings)
high := texNode(kids[2], sym, warnings)
return texPiece{texBase(base) + "_{" + low.text + "}^{" + high.text + "}", base.tokens + low.tokens + high.tokens}
case "munder":
kids := mathChildren(n)
if len(kids) != 2 {
return texUnconverted(n, sym, warnings)
}
base := texNode(kids[0], sym, warnings)
under := texNode(kids[1], sym, warnings)
return texPiece{texBase(base) + "_{" + under.text + "}", base.tokens + under.tokens}
case "mover":
kids := mathChildren(n)
if len(kids) != 2 {
return texUnconverted(n, sym, warnings)
}
base := texNode(kids[0], sym, warnings)
if over := onlyChild(kids[1]); over.name == "mo" {
if cmd, ok := sym.Accents[tokenPiece(allText(over), sym).text]; ok {
return texPiece{cmd + "{" + base.text + "}", 1}
}
}
over := texNode(kids[1], sym, warnings)
return texPiece{texBase(base) + "^{" + over.text + "}", base.tokens + over.tokens}
case "mfrac":
kids := mathChildren(n)
if len(kids) != 2 {
return texUnconverted(n, sym, warnings)
}
num := texNode(kids[0], sym, warnings)
den := texNode(kids[1], sym, warnings)
return texPiece{`\frac{` + num.text + `}{` + den.text + `}`, num.tokens + den.tokens}
case "msqrt":
content := joinTeX(texKids(n, sym, warnings))
return texPiece{`\sqrt{` + content.text + `}`, content.tokens}
case "mroot":
kids := mathChildren(n)
if len(kids) != 2 {
return texUnconverted(n, sym, warnings)
}
base := texNode(kids[0], sym, warnings)
index := texNode(kids[1], sym, warnings)
return texPiece{`\sqrt[` + index.text + `]{` + base.text + `}`, base.tokens + index.tokens}
}
return texUnconverted(n, sym, warnings)
}
// texKids converts the children of a node.
func texKids(n *node, sym *Symbols, warnings *[]Warning) []texPiece {
var out []texPiece
for _, k := range mathChildren(n) {
out = append(out, texNode(k, sym, warnings))
}
return out
}
// onlyChild unwraps a row of one child, so an accent inside an mrow is found.
func onlyChild(n *node) *node {
for n.name == "mrow" {
kids := mathChildren(n)
if len(kids) != 1 {
break
}
n = kids[0]
}
return n
}
// joinTeX concatenates pieces, putting a space after a letter command when a
// letter or digit follows. Only the end of the previous piece is looked at, so
// a formula of many tokens is joined in linear time.
func joinTeX(ps []texPiece) texPiece {
var b strings.Builder
tokens := 0
prev := ""
for _, p := range ps {
if p.text == "" {
continue
}
if prev != "" {
tail := prev
if len(tail) > 40 {
tail = tail[len(tail)-40:]
}
if first, _ := utf8.DecodeRuneInString(p.text); letterCommandEnd.MatchString(tail) && (unicode.IsLetter(first) || unicode.IsDigit(first)) {
b.WriteByte(' ')
}
}
b.WriteString(p.text)
prev = p.text
tokens += p.tokens
}
return texPiece{b.String(), tokens}
}
// texBase returns the base of a script: empty becomes {} so the script has
// something to attach to, and a base of more than one token is put in braces.
func texBase(p texPiece) string {
switch {
case p.text == "":
return "{}"
case p.tokens > 1:
return "{" + p.text + "}"
}
return p.text
}
// texToken applies the table's token replacements to a token's text and, if
// anything is left, converts it with conv.
func texToken(text string, sym *Symbols, conv func(string) string) texPiece {
t := tokenPiece(text, sym)
if t.tokens == 0 {
return texPiece{}
}
return texPiece{conv(t.text), 1}
}
// texIdentifier converts an mi: one character through the table (a Greek letter
// to its command), several characters to \mathrm{}, and a mathvariant from the
// table to its font command around the result.
func texIdentifier(n *node, sym *Symbols) texPiece {
t := tokenPiece(allText(n), sym)
if t.tokens == 0 {
return texPiece{}
}
variant := sym.MathVariants[n.attrs["mathvariant"]]
if utf8.RuneCountInString(t.text) == 1 {
body := texEscape(t.text)
if cmd, ok := sym.TeX[t.text]; ok {
body = cmd
}
if variant != "" {
body = variant + "{" + body + "}"
}
return texPiece{body, 1}
}
if variant == "" {
variant = `\mathrm`
}
return texPiece{variant + "{" + texEscape(t.text) + "}", 1}
}
// texText converts the text of an mtext: one character with a table entry
// becomes that command, anything else \text{}.
func texText(t string, sym *Symbols) string {
if utf8.RuneCountInString(t) == 1 {
if cmd, ok := sym.TeX[t]; ok {
return cmd
}
}
return `\text{` + texEscape(t) + `}`
}
// texEscape escapes the characters TeX treats specially.
func texEscape(s string) string {
var b strings.Builder
for _, r := range s {
switch r {
case '\\':
b.WriteString(`\textbackslash{}`)
case '{', '}', '$', '&', '#', '%', '_':
b.WriteByte('\\')
b.WriteRune(r)
default:
b.WriteRune(r)
}
}
return b.String()
}
// texUnconverted reduces an element with no conversion to its text inside
// \text{} and records which element it was.
func texUnconverted(n *node, sym *Symbols, warnings *[]Warning) texPiece {
*warnings = append(*warnings, Warning{Code: WarnMathUnconverted, Fragment: n.name})
text := contentText(n, sym)
if text == "" {
return texPiece{}
}
return texPiece{`\text{` + texEscape(text) + `}`, 1}
}