-
Notifications
You must be signed in to change notification settings - Fork 0
/
ggtex.awk
executable file
·78 lines (57 loc) · 1.68 KB
/
ggtex.awk
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
#! /usr/bin/env -S gawk -f
# A gawk script to convert modified LaTeX math-mode code to Geogebra code.
#
# For syntax and more info, see http://github.com/fnaufel/ggtex-gawk.
#
# To install gawk for Windows, see http://gnuwin32.sourceforge.net/packages/gawk.htm
#
# Usage:
#
# gawk -f ggtex.awk TEXFILE.tex
#
# Or, if you're on GNU-Linux and ggtex.awk has been made executable
#
# ggtex.awk TEXFILE.tex
#
# The result will be in file TEXFILE.gg.tex
BEGIN {
# Each chunk ends with a math mode delimiter
RS = "(\\\\\\[)" "|" "(\\\\\\])"
# Output filename
output = ARGV[1]
sub(/\.tex$/, "", output)
sub(/$/, ".gg.tex", output)
}
{
# For each chunk
if (RT != "\\]")
# it's not math mode: just print
printf("%s", $0) > output
else
# it's math mode: translate and print
printf("%s", process($0)) > output
}
END {
close(output)
}
function process(c) {
original = "\\[" c "\\]"
before_trans = "\n\n\\noindent\\hrulefill\n\\begin{verbatim}\n"
after_trans = "\n\\end{verbatim}\n\\hrulefill\\\\"
translation = translate(c)
return original before_trans translation after_trans
}
function translate(chunk) {
# Strip leading and trailing newlines
sub(/^\n/, "", chunk)
sub(/\n$/, "", chunk)
# Add calls and trailing parens
chunk = "FormulaText(Simplify(\n\"" chunk "\"\n))"
# Add " + before and add + " after Geogebra expressions escaped with @
chunk = gensub(/@([^@]+)@/, "\" + \\1 + \"", "g", chunk)
# Delete possible " " + at the beginning
sub(/"\s*" \+\s*/, "", chunk)
# Delete possible + "" at the end
sub(/\s*\+ "\s*"/, "", chunk)
return chunk
}