-
Notifications
You must be signed in to change notification settings - Fork 17
/
maketexcomp.ado
135 lines (94 loc) · 4.23 KB
/
maketexcomp.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
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
********************************************************************************
* Description of the Program - *
* Utility to build a LaTeX compilation script. *
* *
* Program Output - *
* Bash/Batch script calling pdflatex on a given LaTeX source code file *
* *
* Lines - *
* 125 *
* *
********************************************************************************
*! maketexcomp
*! v 0.0.5
*! 01may2019
// Drop program from memory if it is already loaded
cap prog drop maketexcomp
// Define program
prog def maketexcomp, rclass
// Version Stata should use to interpret the code
version 14
// Define syntax structure for subroutine
syntax anything(name=filenm id="Name of LaTeX Source Code File"), ///
SCRiptname(string) root(string) [ PDFlatex(string asis) ]
// If path to pdfLaTeX not specified use *nix binary reference
if `"`pdflatex'"' == "" loc pdflatex pdflatex
// Clean the filename macro to remove any quotation marks from the path
loc filenm `: subinstr loc filenm `"""' "", all'
// Check if user is running on Windows
if `"`c(os)'"' == "Windows" {
// Creates file extension for Windows terminal executables
loc extension .bat
// Adds a comment header for Windows batch files
loc header "::Batch file to compile LaTeX source"
// Move to directory
loc dirmove `"chdir /d `root'"'
// Contructs the reference to the pdf LaTeX binary
loc bin `pdflatex'.exe
// Contructs the reference to the terminal command to delete/remove files
loc remove DEL
// Only used for parallel construction across OS
loc scriptexec
} // End of IF Block for Windows systems
// For *nix based systems
else {
// Create extension reference for a shell script
loc extension .sh
// Add the header for the Bourne Again SHell
loc header "#!/bin/bash"
// Move to directory
loc dirmove `"cd `root'"'
// Add reference to the binary for pdf LaTeX
loc bin `pdflatex'
// Add reference for the terminal command to delete/remove files
loc remove "rm"
// Constructs shell command to make the script executable
loc scriptexec ! chmod +x "`scriptname'`extension'"
} // End of ELSE Block for *nix based systems
// Opens a file connection to construct the compilation script
file open comp using `"`scriptname'`extension'"', w replace
// Writes the appropriate OS header for a terminal script
file write comp "`header'" _n
// Writes the command to move into the directory where the executable is located
file write comp "`dirmove'" _n
// Calls pdfLaTeX to do the first pass
file write comp `"`bin' "`filenm'.tex""' _n
// Calls pdfLaTeX to do the second pass; this should finalize hyperrefs to
// individual graphs and TOC references as well as doing the initial insert
// and build of the TOC
file write comp `"`bin' "`filenm'.tex""' _n
// Calls pdfLaTeX to do the last pass; this should finalize the TOC/TOF and
// any other references like that
file write comp `"`bin' "`filenm'.tex""' _n
// Loop over ancillary file extensions
foreach v in aux lof log lot out toc {
// If Windows these file name references need to have a Windows path
// delimiter
if `"`c(os)'"' == "Windows" loc filenm `: subinstr loc filenm "/" "\", all'
// Delete ancillary files
file write comp `"`remove' "`filenm'.`v'""' _n
} // End Loop over ancillary file extensions
// Adds empty line
file write comp "" _n
// Adds empty line
file write comp "" _n
// Closes the open file connection
file close comp
// Need to make the file executable on *nix systems
`scriptexec'
// Return shell command to execute compilation script on OSX
if `"`c(os)'"' == "MacOSX" ret loc comp ! open -a Terminal.app `scriptname'`extension'
// For other OS
else ret loc comp ! `scriptname'`extension'
// End of subroutine definition
end