Skip to content

Commit 3f1ec42

Browse files
author
JJ Allaire
committed
support for bottom placement of pdf table captions
1 parent dbccdb4 commit 3f1ec42

3 files changed

Lines changed: 48 additions & 7 deletions

File tree

‎src/config/constants.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,8 @@ export const kFigResponsive = "fig-responsive";
336336
export const kCapLoc = "cap-location";
337337
export const kFigCapLoc = "fig-cap-location";
338338
export const kTblCapLoc = "tbl-cap-location";
339+
export const kCapTop = "top";
340+
export const kCapBottom = "bottom";
339341

340342
// https://pandoc.org/MANUAL.html#default-files
341343
// note: we are keeping some things out of 'defaults' b/ca

‎src/format/html/format-html-shared.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { join } from "path/mod.ts";
88
import { outputVariable, sassLayer, sassVariable } from "../../core/sass.ts";
99
import {
1010
kCapLoc,
11+
kCapTop,
1112
kCodeOverflow,
1213
kLinkExternalIcon,
1314
kTblCapLoc,
@@ -183,7 +184,7 @@ export const quartoDefaults = (format: Format) => {
183184
sassVariable(
184185
kTblCapLoc,
185186
format.metadata[kTblCapLoc] ||
186-
format.metadata[kCapLoc] || "top",
187+
format.metadata[kCapLoc] || kCapTop,
187188
),
188189
),
189190
);

‎src/format/pdf/format-pdf.ts‎

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import { mergeConfigs } from "../../core/config.ts";
1111
import { texSafeFilename } from "../../core/tex.ts";
1212

1313
import {
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+
231243
function 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(/^\\caption.*?\\tabularnewline$/)) {
363+
caption = line;
364+
return undefined;
365+
} else if (line.match(/^\\end{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(/^\\begin{longtable}/);
375+
}
376+
377+
return line;
378+
};
379+
};
380+
343381
const indexAndSuppressPandocBibliography = (
344382
renderedCites: Record<string, string[]>,
345383
) => {

0 commit comments

Comments
 (0)