Skip to content

Commit b13870b

Browse files
authored
Merge pull request #66 from lambda-feedback/d40-remove-image-figure-text
D62 Center images
2 parents b6b70c0 + 5d28dfd commit b13870b

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

index.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,23 @@ export const handler = async function (
5959
markdown: string,
6060
implicitFigures: boolean = false
6161
) => {
62-
// pandoc source format
63-
// If implicitFigures, enable the implicit_figures extension and add image captions
64-
const fromString = implicitFigures ? "markdown+implicit_figures" : "markdown-implicit_figures";
62+
// Use standard Pandoc style via format name `markdown`
63+
// (see https://pandoc.org/chunkedhtml-demo/3.1-general-options.html)
64+
//
65+
// The "+implicit_figures" extension wraps images in figures and uses the image's alt text as the caption.
66+
// NOTE: The "Figure X." prefix is removed in the LaTeX template via:
67+
// ```tex
68+
// % Remove "Figure X." prefix from captions
69+
// \usepackage{caption}
70+
// \captionsetup[figure]{labelformat=empty}
71+
// ```
72+
//
73+
// If `implicitFigures`, enable the `implicit_figures`, disable otherwise...
74+
const formatName = implicitFigures ? "markdown+implicit_figures" : "markdown-implicit_figures";
6575

6676
try {
6777
await pdcTs.Execute({
68-
from: fromString,
78+
from: formatName,
6979
to: "latex", // pandoc output format
7080
pandocArgs,
7181
spawnOpts: { argv0: "+RTS -M512M -RTS" },
@@ -82,7 +92,7 @@ export const handler = async function (
8292
}
8393

8494
const TeXoutput = await pdcTs.Execute({
85-
from: fromString,
95+
from: formatName,
8696
to: "latex", // pandoc output format
8797
pandocArgs,
8898
outputToFile: false, // Controls whether the output will be returned as a string or written to a file

src/template.latex

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,28 @@ $endif$
220220
\usepackage{float}
221221
\floatplacement{figure}{H}
222222

223+
% Centre images
224+
\usepackage{etoolbox} % For toggle functionality
225+
226+
\newtoggle{infigure} % Create toggle to track if we're inside a figure environment
227+
\togglefalse{infigure} % Initialize as false
228+
229+
\pretocmd{\figure}{\toggletrue{infigure}}{}{} % Set toggle when entering/exiting figure environments
230+
\apptocmd{\endfigure}{\togglefalse{infigure}}{}{}
231+
232+
\let\oldincludegraphics\includegraphics % Save the original includegraphics command
233+
234+
\renewcommand{\includegraphics}[2][]{ % Redefine includegraphics to check if it's already in a figure
235+
\iftoggle{infigure}{%
236+
\oldincludegraphics[#1]{#2}%
237+
}{%
238+
\begin{figure}
239+
\centering
240+
\oldincludegraphics[#1]{#2}
241+
\end{figure}%
242+
}%
243+
}
244+
223245
% Remove "Figure X." prefix from captions
224246
\usepackage{caption}
225247
\captionsetup[figure]{labelformat=empty}

0 commit comments

Comments
 (0)