Skip to content

Instantly share code, notes, and snippets.

@LindsayBradford
Last active February 26, 2019 14:39
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save LindsayBradford/11274456 to your computer and use it in GitHub Desktop.
Save LindsayBradford/11274456 to your computer and use it in GitHub Desktop.
Gradle script for LaTeX pdf generation.
/*
* Usage:
* all LaTeX source files go in $rawDirectory
* base document to 'cook' should match content of $latexFile
* build script delivers resulting PDF file to $cookedDirectory
*/
defaultTasks 'full'
ext { documentBase = 'myBaseLaTeXFileName' }
project.ext {
latexFile = documentBase + '.tex'
pdfFile = documentBase + '.pdf'
rawDirectory = 'raw'
cookedDirectory = 'cooked'
}
task cook(type: Exec) {
doFirst {
println "Cooking $project.latexFile in /$project.rawDirectory."
}
outputs.upToDateWhen{false}
workingDir project.rawDirectory
standardOutput = new ByteArrayOutputStream() // stops output to STDOUT
2.times {
commandLine 'pdflatex', project.documentBase
}
outputs.file file("$project.rawDirectory/$project.pdfFile")
doLast {
println "Cooked $project.latexFile to $project.pdfFile."
}
}
task deliver(type: Copy) {
outputs.upToDateWhen{false}
from cook
into project.cookedDirectory
doLast {
println "Delivered $project.pdfFile to /$project.cookedDirectory."
}
}
task flush(type: Delete) {
dependsOn deliver
outputs.upToDateWhen{false}
delete {
fileTree(dir: project.rawDirectory, excludes: ['**/*.tex', '**/*.cls'])
}
doLast {
println "Flushed /$project.rawDirectory of temporary $documentBase files."
}
}
task full {
dependsOn flush
}
@themathmagician
Copy link

themathmagician commented Sep 12, 2017

Thank you, very helpful. As a comment, you can avoid the 2.times by running:
commandLine 'latexmk', '-pdf', project.documentBase
(it will automatically run the typesetting the required amount of times for generation of tables, references etc).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment