-
-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathLatexCompiler.kt
507 lines (419 loc) · 18.4 KB
/
LatexCompiler.kt
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
package nl.hannahsten.texifyidea.run.compiler
import com.intellij.execution.configurations.GeneralCommandLine
import com.intellij.openapi.project.Project
import com.intellij.openapi.projectRoots.ProjectJdkTable
import com.intellij.openapi.roots.ProjectRootManager
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.util.execution.ParametersListUtil
import nl.hannahsten.texifyidea.run.latex.LatexDistributionType
import nl.hannahsten.texifyidea.run.latex.LatexRunConfiguration
import nl.hannahsten.texifyidea.settings.sdk.DockerSdk
import nl.hannahsten.texifyidea.settings.sdk.DockerSdkAdditionalData
import nl.hannahsten.texifyidea.settings.sdk.LatexSdkUtil
import nl.hannahsten.texifyidea.util.LatexmkRcFileFinder
import nl.hannahsten.texifyidea.util.files.hasTectonicTomlFile
import nl.hannahsten.texifyidea.util.runCommand
import java.util.*
/**
* @author Hannah Schellekens, Sten Wessel
*/
@Suppress("DuplicatedCode")
enum class LatexCompiler(private val displayName: String, val executableName: String) {
PDFLATEX("pdfLaTeX", "pdflatex") {
override fun createCommand(
runConfig: LatexRunConfiguration,
auxilPath: String?,
outputPath: String?,
moduleRoot: VirtualFile?,
moduleRoots: Array<VirtualFile>
): MutableList<String> {
// For now only support custom executable for TeX Live
// At least avoids prepending a full path to a supposed TeX Live executable when in fact it will be prepended by a docker command
val executable = LatexSdkUtil.getExecutableName(executableName, runConfig.project, runConfig.getLatexDistributionType())
val command = mutableListOf(runConfig.compilerPath ?: executable)
command.add("-file-line-error")
command.add("-interaction=nonstopmode")
command.add("-synctex=1")
command.add("-output-format=${runConfig.outputFormat.name.lowercase(Locale.getDefault())}")
command.add("-output-directory=$outputPath")
// -aux-directory only exists on MiKTeX
if (auxilPath != null && runConfig.getLatexDistributionType().isMiktex(project = runConfig.project)) {
command.add("-aux-directory=$auxilPath")
}
// Prepend root paths to the input search path
if (runConfig.getLatexDistributionType().isMiktex(runConfig.project)) {
moduleRoots.forEach {
command.add("-include-directory=${it.path}")
}
}
return command
}
},
LUALATEX("LuaLaTeX", "lualatex") {
override fun createCommand(
runConfig: LatexRunConfiguration,
auxilPath: String?,
outputPath: String?,
moduleRoot: VirtualFile?,
moduleRoots: Array<VirtualFile>
): MutableList<String> {
val command = mutableListOf(
runConfig.compilerPath ?: LatexSdkUtil.getExecutableName(
executableName,
runConfig.project,
runConfig.getLatexDistributionType()
)
)
// Some commands are the same as for pdflatex
command.add("-file-line-error")
command.add("-interaction=nonstopmode")
command.add("-synctex=1")
command.add("-output-format=${runConfig.outputFormat.name.lowercase(Locale.getDefault())}")
command.add("-output-directory=$outputPath")
// Note that lualatex has no -aux-directory
return command
}
},
LATEXMK("Latexmk", "latexmk") {
override val includesBibtex = true
override val includesMakeindex = true
override val handlesNumberOfCompiles = true
override val outputFormats = arrayOf(Format.DEFAULT, Format.PDF, Format.DVI)
override fun createCommand(
runConfig: LatexRunConfiguration,
auxilPath: String?,
outputPath: String?,
moduleRoot: VirtualFile?,
moduleRoots: Array<VirtualFile>
): MutableList<String> {
val command = mutableListOf(
runConfig.compilerPath ?: LatexSdkUtil.getExecutableName(
executableName,
runConfig.project,
runConfig.getLatexDistributionType()
)
)
val isLatexmkRcFilePresent = LatexmkRcFileFinder.isLatexmkRcFilePresent(runConfig)
// If it is present, assume that it will handle everything (command line options would overwrite latexmkrc options)
if (!isLatexmkRcFilePresent) {
// Adding the -pdf flag makes latexmk run with pdflatex, which is definitely preferred over running with just latex
command.add("-pdf")
command.add("-file-line-error")
command.add("-interaction=nonstopmode")
command.add("-synctex=1")
}
if (runConfig.outputFormat != Format.DEFAULT) {
command.add("-output-format=${runConfig.outputFormat.name.lowercase(Locale.getDefault())}")
}
command.add("-output-directory=$outputPath")
if (auxilPath != null && runConfig.getLatexDistributionType().isMiktex(runConfig.project)) {
command.add("-aux-directory=$auxilPath")
}
// -include-directory does not work with latexmk
return command
}
},
XELATEX("XeLaTeX", "xelatex") {
override val outputFormats = arrayOf(Format.PDF, Format.XDV)
override fun createCommand(
runConfig: LatexRunConfiguration,
auxilPath: String?,
outputPath: String?,
moduleRoot: VirtualFile?,
moduleRoots: Array<VirtualFile>
): MutableList<String> {
val command = mutableListOf(
runConfig.compilerPath ?: LatexSdkUtil.getExecutableName(
executableName,
runConfig.project,
runConfig.getLatexDistributionType()
)
)
// As usual, available command line options can be viewed with xelatex --help
// On TeX Live, installing collection-xetex should be sufficient to get xelatex
command.add("-file-line-error")
command.add("-interaction=nonstopmode")
command.add("-synctex=1")
if (runConfig.outputFormat == Format.XDV) {
command.add("-no-pdf")
}
command.add("-output-directory=$outputPath")
if (auxilPath != null && runConfig.getLatexDistributionType().isMiktex(runConfig.project)) {
command.add("-aux-directory=$auxilPath")
}
// Prepend root paths to the input search path
if (runConfig.getLatexDistributionType().isMiktex(runConfig.project)) {
moduleRoots.forEach {
command.add("-include-directory=${it.path}")
}
}
return command
}
},
TEXLIVEONFLY("Texliveonfly", "texliveonfly") {
override val outputFormats = arrayOf(Format.PDF)
override fun createCommand(
runConfig: LatexRunConfiguration,
auxilPath: String?,
outputPath: String?,
moduleRoot: VirtualFile?,
moduleRoots: Array<VirtualFile>
): MutableList<String> {
val command = mutableListOf(
runConfig.compilerPath ?: LatexSdkUtil.getExecutableName(
executableName,
runConfig.project,
runConfig.getLatexDistributionType()
)
)
// texliveonfly is a Python script which calls other compilers (by default pdflatex), main feature is downloading packages automatically
// commands can be passed to those compilers with the arguments flag, however apparently IntelliJ cannot handle quotes so we cannot pass multiple arguments to pdflatex.
// Fortunately, -synctex=1 and -interaction=nonstopmode are on by default in texliveonfly
// Since adding one will work without any quotes, we choose the output directory.
if (outputPath != null) {
command.add("--arguments=--output-directory=$outputPath")
}
return command
}
},
TECTONIC("Tectonic", "tectonic") {
override val includesBibtex = true
override val handlesNumberOfCompiles = true
override val outputFormats = arrayOf(Format.PDF, Format.HTML, Format.XDV, Format.AUX)
override fun createCommand(
runConfig: LatexRunConfiguration,
auxilPath: String?,
outputPath: String?,
moduleRoot: VirtualFile?,
moduleRoots: Array<VirtualFile>
): MutableList<String> {
val command = mutableListOf(runConfig.compilerPath ?: executableName)
// The available command line arguments can be found at https://github.com/tectonic-typesetting/tectonic/blob/d7a8497c90deb08b5e5792a11d6e8b082f53bbb7/src/bin/tectonic.rs#L158
// The V2 CLI uses a toml file and should not have arguments
if (runConfig.mainFile?.hasTectonicTomlFile() != true) {
command.add("--synctex")
command.add("--outfmt=${runConfig.outputFormat.name.lowercase(Locale.getDefault())}")
if (outputPath != null) {
command.add("--outdir=$outputPath")
}
}
else {
command.add("-X")
command.add("build")
}
return command
}
},
ARARA("Arara", "arara") {
override val includesBibtex = true
override val handlesNumberOfCompiles = true
override val outputFormats = arrayOf(Format.PDF)
override fun createCommand(
runConfig: LatexRunConfiguration,
auxilPath: String?,
outputPath: String?,
moduleRoot: VirtualFile?,
moduleRoots: Array<VirtualFile>
): MutableList<String> {
// Arara handles everything as configured by magic comments in the file.
// We cannot use --verbose because it relies on user input
return mutableListOf(runConfig.compilerPath ?: executableName)
}
},
;
/**
* Convert Windows paths to WSL paths.
*/
private fun String.toPath(runConfig: LatexRunConfiguration): String =
if (runConfig.getLatexDistributionType() == LatexDistributionType.WSL_TEXLIVE) {
runCommand("wsl", "wslpath", "-a", this) ?: this
}
else this
/**
* Get the execution command for the latex compiler.
*
* @param runConfig
* The run configuration object to get the command for.
* @param project
* The current project.
*/
fun getCommand(runConfig: LatexRunConfiguration, project: Project): List<String>? {
val rootManager = ProjectRootManager.getInstance(project)
val mainFile = runConfig.mainFile ?: return null
// Getting the content root is an expensive operation (See WorkspaceFileIndexDataImpl#ensureIsUpToDate), and since it probably won't change often we reuse a cached value
val moduleRoot = runConfig.outputPath.contentRoot
// For now we disable module roots with Docker
// Could be improved by mounting them to the right directory
val moduleRoots = if (runConfig.getLatexDistributionType().isDocker()) {
emptyArray()
}
else {
rootManager.contentSourceRoots
}
val outputPath = if (runConfig.getLatexDistributionType() == LatexDistributionType.DOCKER_MIKTEX) {
// If we used /miktex/work/out, an out directory would appear in the src folder on the host system
"/miktex/out"
}
else if (runConfig.getLatexDistributionType() == LatexDistributionType.DOCKER_TEXLIVE) {
"/out"
}
else {
runConfig.outputPath.getAndCreatePath()?.path?.toPath(runConfig)
}
val auxilPath = if (runConfig.getLatexDistributionType() == LatexDistributionType.DOCKER_MIKTEX) {
"/miktex/auxil"
}
else if (runConfig.getLatexDistributionType() == LatexDistributionType.DOCKER_TEXLIVE) {
null
}
else {
runConfig.auxilPath.getAndCreatePath()?.path?.toPath(runConfig)
}
val command = createCommand(
runConfig,
auxilPath,
outputPath,
moduleRoot,
moduleRoots
)
if (runConfig.getLatexDistributionType() == LatexDistributionType.WSL_TEXLIVE) {
var wslCommand = GeneralCommandLine(command).commandLineString
// Custom compiler arguments specified by the user
runConfig.compilerArguments?.let { arguments ->
ParametersListUtil.parse(arguments)
.forEach { wslCommand += " $it" }
}
wslCommand += " ${mainFile.path.toPath(runConfig)}"
return mutableListOf("bash", "-ic", wslCommand)
}
if (runConfig.getLatexDistributionType().isDocker()) {
createDockerCommand(runConfig, auxilPath, outputPath, mainFile, command)
}
// Custom compiler arguments specified by the user
runConfig.compilerArguments?.let { arguments ->
ParametersListUtil.parse(arguments)
.forEach { command.add(it) }
}
// Run some code before running the document
if (runConfig.beforeRunCommand?.isNotBlank() == true) {
if (runConfig.compiler == LATEXMK) {
// latexmk has its own flag to do this
command.add("-usepretex=" + runConfig.beforeRunCommand)
command.add(mainFile.name)
}
else {
// pdflatex, lualatex and xelatex support this by being able to run on a given string as if it was in a file
command.add(runConfig.beforeRunCommand + " \\input{${mainFile.name}}")
}
}
else if (runConfig.compiler != TECTONIC || runConfig.mainFile?.hasTectonicTomlFile() != true) {
command.add(mainFile.name)
}
return command
}
@Suppress("SameParameterValue")
private fun createDockerCommand(runConfig: LatexRunConfiguration, dockerAuxilDir: String?, dockerOutputDir: String?, mainFile: VirtualFile, command: MutableList<String>) {
val isMiktex = runConfig.getLatexDistributionType() == LatexDistributionType.DOCKER_MIKTEX
if (isMiktex) {
// See https://hub.docker.com/r/miktex/miktex
"docker volume create --name miktex".runCommand()
}
// Find the sdk corresponding to the type the user has selected in the run config
val sdk = ProjectJdkTable.getInstance().allJdks.firstOrNull { it.sdkType is DockerSdk }
val parameterList = mutableListOf(
if (sdk == null) "docker" else (sdk.sdkType as DockerSdk).getExecutableName("docker", sdk.homePath!!),
"run",
"--rm",
)
parameterList += if (isMiktex) {
listOf(
"-v",
"miktex:/miktex/.miktex",
"-v",
"${mainFile.parent.path}:/miktex/work"
)
}
else {
listOf(
"-v",
"${mainFile.parent.path}:/workdir"
)
}
if (dockerOutputDir != null) {
// Avoid mounting the mainfile parent also to /miktex/work/out,
// because there may be a good reason to make the output directory the same as the source directory
val outPath = runConfig.outputPath.getAndCreatePath()
if (outPath?.path != null && outPath != mainFile.parent) {
parameterList.addAll(listOf("-v", "${outPath.path}:$dockerOutputDir"))
}
}
if (dockerAuxilDir != null) {
val auxilPath = runConfig.auxilPath.getAndCreatePath()
if (auxilPath?.path != null && auxilPath != mainFile.parent) {
parameterList.addAll(listOf("-v", "${auxilPath.path}:$dockerAuxilDir"))
}
}
val sdkImage = (sdk?.sdkAdditionalData as? DockerSdkAdditionalData)?.imageName
val default = if (isMiktex) "miktex/miktex:latest" else "texlive/texlive:latest"
parameterList.add(sdkImage ?: default)
command.addAll(0, parameterList)
}
/**
* Create the command to execute to use the compiler.
*
* @param runConfig LaTeX run configuration which initiated the action of creating this command.
* @param moduleRoot Module root.
* @param moduleRoots List of source roots.
*
* @return The command to be executed.
*/
protected open fun createCommand(
runConfig: LatexRunConfiguration,
auxilPath: String?,
outputPath: String?,
moduleRoot: VirtualFile?,
moduleRoots: Array<VirtualFile>
): MutableList<String> = error("Not implemented for $this")
/**
* Whether the compiler includes running bibtex/biber.
*/
open val includesBibtex = false
/**
* Whether the compiler includes running index programs.
*/
open val includesMakeindex = false
/**
* Whether the compiler automatically determines the number of compiles needed.
*/
open val handlesNumberOfCompiles = false
/**
* List of output formats supported by this compiler.
*/
open val outputFormats: Array<Format> = arrayOf(Format.PDF, Format.DVI)
override fun toString() = this.displayName
companion object {
fun byExecutableName(exe: String): LatexCompiler {
return entries.firstOrNull {
it.executableName.equals(exe, true)
} ?: PDFLATEX
}
}
/**
* @author Hannah Schellekens
*/
enum class Format {
DEFAULT, // Means: don't overwite the default, e.g. a default from the latexmkrc, i.e. don't add any command line parameters
PDF,
DVI,
HTML,
XDV,
AUX;
companion object {
fun byNameIgnoreCase(name: String?): Format {
return entries.firstOrNull {
it.name.equals(name, ignoreCase = true)
} ?: PDF
}
}
}
}