@@ -11,7 +11,9 @@ import { mergeConfigs } from "../../core/config.ts";
1111import { texSafeFilename } from "../../core/tex.ts" ;
1212
1313import {
14+ kCapBottom ,
1415 kCapLoc ,
16+ kCapTop ,
1517 kCitationLocation ,
1618 kCiteMethod ,
1719 kClassOption ,
@@ -117,14 +119,11 @@ function createPdfFormat(autoShiftHeadings = true, koma = true): Format {
117119 if ( koma ) {
118120 // determine caption options
119121 const captionOptions = [ ] ;
120- const tblCaploc = format . metadata [ kTblCapLoc ] ||
121- format . metadata [ kCapLoc ] || "top" ;
122+ const tblCaploc = tblCapLocation ( format ) ;
122123 captionOptions . push (
123- tblCaploc === "top" ? "tableheading" : "tablesignature" ,
124+ tblCaploc === kCapTop ? "tableheading" : "tablesignature" ,
124125 ) ;
125- const figCaploc = format . metadata [ kFigCapLoc ] ||
126- format . metadata [ kCapLoc ] || "bottom" ;
127- if ( figCaploc === "top" ) {
126+ if ( figCapLocation ( format ) === kCapTop ) {
128127 captionOptions . push ( "figureheading" ) ;
129128 }
130129
@@ -213,6 +212,11 @@ function pdfLatexPostProcessor(flags: PandocFlags, format: Format) {
213212 }
214213 }
215214
215+ // Move longtable captions below if requested
216+ if ( tblCapLocation ( format ) === kCapBottom ) {
217+ lineProcessors . push ( longtableBottomCaptionProcessor ( ) ) ;
218+ }
219+
216220 // If enabled, switch to sidenote footnotes
217221 if ( marginRefs ( flags , format ) ) {
218222 // Replace notes with side notes
@@ -228,6 +232,14 @@ function pdfLatexPostProcessor(flags: PandocFlags, format: Format) {
228232 } ;
229233}
230234
235+ function tblCapLocation ( format : Format ) {
236+ return format . metadata [ kTblCapLoc ] || format . metadata [ kCapLoc ] || kCapTop ;
237+ }
238+
239+ function figCapLocation ( format : Format ) {
240+ return format . metadata [ kFigCapLoc ] || format . metadata [ kCapLoc ] || kCapBottom ;
241+ }
242+
231243function marginRefs ( flags : PandocFlags , format : Format ) {
232244 return format . pandoc [ kReferenceLocation ] === "margin" ||
233245 flags [ kReferenceLocation ] === "margin" ;
@@ -340,6 +352,32 @@ const sideNoteLineProcessor = () => {
340352 } ;
341353} ;
342354
355+ const longtableBottomCaptionProcessor = ( ) => {
356+ let scanning = false ;
357+ let caption : string | undefined ;
358+
359+ return ( line : string ) : string | undefined => {
360+ if ( scanning ) {
361+ // look for a caption line
362+ if ( line . match ( / ^ \\ c a p t i o n .* ?\\ t a b u l a r n e w l i n e $ / ) ) {
363+ caption = line ;
364+ return undefined ;
365+ } else if ( line . match ( / ^ \\ e n d { longtable} $ / ) ) {
366+ scanning = false ;
367+ if ( caption ) {
368+ line = caption + "\n" + line ;
369+ caption = undefined ;
370+ return line ;
371+ }
372+ }
373+ } else {
374+ scanning = ! ! line . match ( / ^ \\ b e g i n { longtable} / ) ;
375+ }
376+
377+ return line ;
378+ } ;
379+ } ;
380+
343381const indexAndSuppressPandocBibliography = (
344382 renderedCites : Record < string , string [ ] > ,
345383) => {
0 commit comments