-
Notifications
You must be signed in to change notification settings - Fork 17
/
texclean.ado
102 lines (71 loc) · 3.07 KB
/
texclean.ado
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
********************************************************************************
* Description of the Program - *
* Utility for handling LaTeX spcial characters. The ref option is used when *
* string will be used as part of a reference label in LaTeX (e.g., special *
* characters are deleted); without this option the characters are escaped *
* and/or their text macros used in their place. *
* *
* Program Output - *
* r(clntex) - A LaTeX sanitized string *
* *
* Lines - *
* 101 *
* *
********************************************************************************
*! texclean
*! v 0.0.2
*! 03may2019
// Drop program from memory if previously loaded
cap prog drop texclean
// Define program
prog def texclean, rclass
// Set version used to interpret code
version 14
// Set program syntax
syntax anything(name=text id="LaTeX String to Escape" everything), [ Ref ]
// Check for LaTeX special characters
if regexm(`"`text'"', "([#\$%&~_\^\\\{\}<>\|¡¿£])") == 1 {
// Store argument in new local macro
loc cln `"`text'"'
// For use with reference labels
if "`ref'" != "" {
// Loop over the LaTeX special characters
foreach c in "£" "|" "¡" "¿" "{" "}" "<" ">" "\" "^" "_" "~" "&" ///
"%" "$" "#" {
// Remove the special characters
loc cln `: subinstr loc cln `"`c'"' "", all'
} // End Loop over LaTeX special characters
} // End IF Block for reference strings
// Otherwise
else {
// Handle \ character
loc cln `: subinstr loc cln "\" "\\", all'
// Handle # character
loc cln `: subinstr loc cln "#" "\#", all'
// Handle $ character
loc cln `: subinstr loc cln "$" "\$", all'
// Handle % character
loc cln `: subinstr loc cln "%" "\%", all'
// Handle & character
loc cln `: subinstr loc cln "&" "\&", all'
// Handle ~ character
loc cln `: subinstr loc cln "~" "\textasciitilde{}", all'
// Handle _ character
loc cln `: subinstr loc cln "_" "\_", all'
// Handle ^ character
loc cln `: subinstr loc cln "^" "\textasciicircum{}", all'
// Handle { character
loc cln `: subinstr loc cln `"{"' `"\{"', all'
// Handle } character
loc cln `: subinstr loc cln `"}"' `"\}"', all'
} // End ELSE Block for non reference label cases
// Return the cleaned string
ret loc clntex `cln'
} // End IF Block for cases w/LaTeX special characters
// If no special characters are in the string
else {
// Return string that does not include special characters
ret loc clntex `text'
} // End ELSE Block for text w/o special characters
// End Program definition
end