@@ -69,16 +69,34 @@ export async function generatePdf(mkOptions: LatexmkOptions): Promise<string> {
6969 ) ;
7070 const initialCompileNeedsRerun = needsRecompilation ( response . log ) ;
7171
72- // Generate the index information, if needed
73- const indexCreated = await makeIndexIntermediates (
74- workingDir ,
75- stem ,
76- pkgMgr ,
77- texLive ,
78- mkOptions . engine . indexEngine ,
79- mkOptions . engine . indexEngineOpts ,
80- mkOptions . quiet ,
81- ) ;
72+ const indexIntermediateFile = indexIntermediate ( workingDir , stem ) ;
73+ let indexCreated = false ;
74+ if ( indexIntermediateFile ) {
75+ // When building large and complex indexes, it
76+ // may be required to run the PDF engine again prior to building
77+ // the index (or page numbers may be incorrect).
78+ // See: https://github.com/rstudio/bookdown/issues/1274
79+ info ( " Re-compiling document for index" ) ;
80+ await runPdfEngine (
81+ mkOptions . input ,
82+ mkOptions . engine ,
83+ texLive ,
84+ mkOptions . outputDir ,
85+ mkOptions . texInputDirs ,
86+ pkgMgr ,
87+ mkOptions . quiet ,
88+ ) ;
89+
90+ // Generate the index information, if needed
91+ indexCreated = await makeIndexIntermediates (
92+ indexIntermediateFile ,
93+ pkgMgr ,
94+ texLive ,
95+ mkOptions . engine . indexEngine ,
96+ mkOptions . engine . indexEngineOpts ,
97+ mkOptions . quiet ,
98+ ) ;
99+ }
82100
83101 // Generate the bibliography intermediaries
84102 const bibliographyCreated = await makeBibliographyIntermediates (
@@ -237,18 +255,25 @@ function displayError(title: string, log: string, result: ProcessResult) {
237255 }
238256}
239257
258+ function indexIntermediate ( dir : string , stem : string ) {
259+ const indexFile = join ( dir , `${ stem } .idx` ) ;
260+ if ( existsSync ( indexFile ) ) {
261+ return indexFile ;
262+ } else {
263+ return undefined ;
264+ }
265+ }
266+
240267async function makeIndexIntermediates (
241- dir : string ,
242- stem : string ,
268+ indexFile : string ,
243269 pkgMgr : PackageManager ,
244270 texLive : TexLiveContext ,
245271 engine ?: string ,
246272 args ?: string [ ] ,
247273 quiet ?: boolean ,
248274) {
249275 // If there is an idx file, we need to run makeindex to create the index data
250- const indexFile = join ( dir , `${ stem } .idx` ) ;
251- if ( existsSync ( indexFile ) ) {
276+ if ( indexFile ) {
252277 if ( ! quiet ) {
253278 info ( "making index" , kLatexHeaderMessageOptions ) ;
254279 }
0 commit comments