Skip to content

Commit 095e1fd

Browse files
committed
build: run pdflatex to convergence for the English mirror PDFs (fixes missing ToC)
The English compendium-en.pdf had no table of contents. runPdfLatexCmd ran pdflatex once; a ToC needs >=2 passes (pass 1 writes .toc, pass 2 typesets it, a 3rd may be needed as inserting the ToC shifts \pageref page numbers). The Swedish tasks get away with one pass because compendium/ and slides/ keep .aux/.toc across builds, but the English mirror dirs are regenerated fresh every run, so the single pass wrote the .toc and never read it. Fix: runPdfLatexCmd gains maxPasses and loops until LaTeX stops asking to "Rerun" (a tiny latexmk), capped — so it does the minimum passes to converge, not a guessed 2 or 3. Swedish tasks keep the default cap 1 (unchanged); the English tasks (pdfCompendiumEn, pdfCompendium1/2En, pdfSlidesEn) pass cap 4. Verified on a fresh-dir rebuild: the ToC now renders.
1 parent 283ba80 commit 095e1fd

1 file changed

Lines changed: 20 additions & 9 deletions

File tree

build.sbt

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,18 +126,29 @@ def showTail(fileName: String, n: Int = 40): Unit = {
126126
println(lines.takeRight(n).mkString("\n"))
127127
}
128128

129-
def runPdfLatexCmd(texFile: File, workDir: File, stdOutSuffix: String = "-console.log"): Unit = {
130-
println(s" ******* Compiling $texFile to pdf *******")
129+
def runPdfLatexCmd(texFile: File, workDir: File, stdOutSuffix: String = "-console.log", maxPasses: Int = 1): Unit = {
131130
val cmd = scala.sys.process.Process(
132131
Seq("pdflatex","-halt-on-error", texFile.getName),
133132
workDir
134133
)
135134
val cmdOutputFile = workDir / texFile.getName.replace(".tex", stdOutSuffix)
136135
// val bibtexCmd = Process(Seq("bibtex", texFile.getName.replace(".tex", ".aux")), workDir)
137136

138-
// run pdflatex command TWICE in sequence to generate toc from .aux etc:
139-
//val exitValue = cmd.#>(cmdOutputFile).#&&(cmd).#>(cmdOutputFile).run.exitValue
140-
val exitValue = cmd.#>(cmdOutputFile).run.exitValue
137+
// Run pdflatex until the .toc / cross-refs converge, i.e. until LaTeX stops asking to rerun — capped at
138+
// maxPasses (like a tiny latexmk; avoids guessing 2 vs 3). The Swedish tasks default to 1 because their working
139+
// dir (compendium/, slides/) keeps .aux/.toc across builds; the English mirror dirs are regenerated fresh every
140+
// run, so pass 1 writes the .toc but never reads it (no ToC), pass 2 typesets it (which shifts pages), and a
141+
// 3rd pass may be needed for the ToC/\pageref page numbers to settle. En tasks pass a cap of 4.
142+
println(s" ******* Compiling $texFile to pdf (up to $maxPasses pass(es)) *******")
143+
var exitValue = 0; var pass = 0; var rerun = true
144+
while pass < math.max(1, maxPasses) && exitValue == 0 && rerun do
145+
exitValue = cmd.#>(cmdOutputFile).run.exitValue
146+
pass += 1
147+
rerun = exitValue == 0 && {
148+
val log = scala.util.Try(IO.read(cmdOutputFile)).getOrElse("")
149+
log.contains("Rerun to get") || log.contains("Label(s) may have changed")
150+
}
151+
println(s" ($pass pdflatex pass(es) run)")
141152
if (exitValue != 0) {
142153
println("*** ############ ERROR LOG STARTS HERE ############### ***")
143154
//Process(Seq("cat", cmdOutputFile.getName), workDir).run
@@ -225,7 +236,7 @@ def reportSwedishPct(autotranslateCp: String, pdf: File): Unit =
225236
lazy val pdfCompendiumEn = taskKey[Unit]("Compile the generated English mirror compendium-en/compendium-en.tex")
226237
pdfCompendiumEn := {
227238
val cp = (autotranslateProject / Compile / fullClasspath).value.files.map(_.getPath).mkString(java.io.File.pathSeparator)
228-
runPdfLatexCmd(texFile = file("compendium-en.tex"), workDir = file("compendium-en"))
239+
runPdfLatexCmd(texFile = file("compendium-en.tex"), workDir = file("compendium-en"), maxPasses = 4)
229240
reportSwedishPct(cp, file("compendium-en/compendium-en.pdf"))
230241
}
231242

@@ -247,14 +258,14 @@ pdfCompendium2 := {
247258
lazy val pdfCompendium1En = taskKey[Unit]("Compile the generated English mirror compendium-en/compendium1-en.tex")
248259
pdfCompendium1En := {
249260
val cp = (autotranslateProject / Compile / fullClasspath).value.files.map(_.getPath).mkString(java.io.File.pathSeparator)
250-
runPdfLatexCmd(texFile = file("compendium1-en.tex"), workDir = file("compendium-en"))
261+
runPdfLatexCmd(texFile = file("compendium1-en.tex"), workDir = file("compendium-en"), maxPasses = 4)
251262
reportSwedishPct(cp, file("compendium-en/compendium1-en.pdf"))
252263
}
253264

254265
lazy val pdfCompendium2En = taskKey[Unit]("Compile the generated English mirror compendium-en/compendium2-en.tex")
255266
pdfCompendium2En := {
256267
val cp = (autotranslateProject / Compile / fullClasspath).value.files.map(_.getPath).mkString(java.io.File.pathSeparator)
257-
runPdfLatexCmd(texFile = file("compendium2-en.tex"), workDir = file("compendium-en"))
268+
runPdfLatexCmd(texFile = file("compendium2-en.tex"), workDir = file("compendium-en"), maxPasses = 4)
258269
reportSwedishPct(cp, file("compendium-en/compendium2-en.pdf"))
259270
}
260271

@@ -296,7 +307,7 @@ pdfSlidesEn := {
296307
val name = if (f.takeRight(4) == ".tex") f.dropRight(4) + "-en.tex" else f + "-en.tex"
297308
val texFile = file(name)
298309
println(s"runPdfLatexCmd($texFile, $workDir)")
299-
runPdfLatexCmd(texFile, workDir)
310+
runPdfLatexCmd(texFile, workDir, maxPasses = 4)
300311
reportSwedishPct(cp, new File(workDir, name.dropRight(4) + ".pdf"))
301312
}
302313
}

0 commit comments

Comments
 (0)