Skip to content

Commit 8045245

Browse files
committed
feat: allow deactivation of float environment in pdf for fenced code
this is a workaround to allow the correct display of fenced code blocks inside floats in pdf/LaTeX by removing the default treatment from pandoc.
1 parent b42543e commit 8045245

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

src/resources/filters/layout/meta.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ function layout_meta_inject_latex_packages()
3131
-- Both options could be undefined, true / false or set to a color value
3232
local useCodeBlockBorder = (adaptiveTextHighlighting and meta[kCodeBlockBorderLeft] == nil and meta[kCodeBlockBackground] == nil) or (meta[kCodeBlockBorderLeft] ~= nil and meta[kCodeBlockBorderLeft] ~= false)
3333
local useCodeBlockBg = meta[kCodeBlockBackground] ~= nil and meta[kCodeBlockBackground] ~= false
34+
local deactivateCodeBlockBg = meta[kCodeBlockBackground] ~= nil and meta[kCodeBlockBackground] == false
3435

3536
-- if we're going to display a border or background
3637
-- we need to inject color handling as well as the
@@ -101,6 +102,16 @@ function layout_meta_inject_latex_packages()
101102
end
102103

103104

105+
-- if we're NOT going to display a border or background we need to inject
106+
-- code to modify the pandoc behavior for fenced code blocks boxes.
107+
-- This can be used to especially allow code blocks in other boxes like
108+
-- callback
109+
if (not useCodeBlockBorder and deactivateCodeBlockBg) then
110+
metaInjectLatex(meta, function(inject)
111+
inject("\\ifdefined\\Shaded\\renewenvironment{Shaded}{}\\fi")
112+
end)
113+
end
114+
104115

105116
-- enable column layout (packages and adjust geometry)
106117
if (layoutState.hasColumns or marginReferences() or marginCitations()) and _quarto.format.isLatexOutput() then

src/resources/schema/document-code.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@
9393
long: |
9494
Specifies to apply a background color on code blocks. Provide a hex color to specify that the background color is
9595
enabled as well as the color of the background.
96+
97+
For PDF output, if you specify `false` and `code-block-border-left` is `false` or not set, the code block is not
98+
enclosed in a floating LaTeX environment. This is especially useful for long code blocks nested inside a callout block,
99+
as it allows correct page breaks to occur. Line numbering and annotations are not affected.
96100
- name: highlight-style
97101
tags:
98102
formats: [$html-all, docx, ms, $pdf-all]
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
---
2+
title: "foo"
3+
code-block-bg: false
4+
format: pdf
5+
---
6+
:::: {.callout-caution icon=false}
7+
## Self implementation
8+
9+
Implement the code yourself by filling out the missing sections:
10+
11+
```{python}
12+
#| code-fold: true
13+
#| eval: false
14+
#| code-summary: "Code fragment for implementation."
15+
#| fig-cap: "Test"
16+
import numpy as np
17+
import matplotlib.pyplot as plt
18+
%config InlineBackend.figure_formats = ["svg"]
19+
20+
frequ = 2 * np.pi * 50
21+
f = lambda t: 0.8 * np.sin(t * frequ)
22+
diod = lambda t: (np.exp(1.2 + t) - 1)
23+
24+
t = np.linspace(0, 2 * np.pi / frequ, 1024, endpoint=False)
25+
x = np.linspace(np.min(f(t)) * 1.3, np.max(f(t)) * 1.1, 1024)
26+
27+
ic = lambda t: diod(f(t))
28+
29+
k = np.arange(0, 16) * 1 / 16
30+
31+
# The provided figures where used to create the illustation
32+
if True:
33+
plt.figure()
34+
plt.plot(f(t), t)
35+
plt.plot([np.min(t),np.max(t)], [0,0], "gray")
36+
plt.axis("off")
37+
#plt.savefig("sin.svg", transparent=True)
38+
plt.figure()
39+
plt.plot(x, diod(x))
40+
z = np.array([0, np.min(f(t)), np.max(f(t))])
41+
plt.plot(z, diod(z), "bx")
42+
plt.axis("off")
43+
plt.xlim([-2,5])
44+
plt.gcf().patch.set_visible(False)
45+
#plt.savefig("diode.svg", transparent=True)
46+
47+
plt.figure()
48+
plt.plot(t, ic(t))
49+
plt.plot(2*np.pi*k/frequ, ic(2*np.pi*k/frequ), "ro")
50+
plt.axis("off")
51+
#plt.savefig("ic.svg", transparent=True, bbox_inches ="tight")
52+
53+
y = ic(k)
54+
yhat = (np.fft.fft(y))
55+
#Necessary correction factor for the FFT
56+
factor = 1 / 16
57+
yy = factor * yhat
58+
59+
ic_mean = np.mean(ic(np.linspace(0, 1/50, 2**20)))
60+
c0 = yy[0].real
61+
effective_value = np.linalg.norm(yy[1:])
62+
harmonic_distortion = np.linalg.norm(yy[3:-2])/np.linalg.norm(yy[1:])
63+
```
64+
::::
65+
66+
Some text that will not be displayed in the correct color.

0 commit comments

Comments
 (0)