Skip to content

Commit 92f2e22

Browse files
authored
Allow table font sizes to be customized. (#279)
1 parent 22d2a0b commit 92f2e22

3 files changed

Lines changed: 79 additions & 17 deletions

File tree

filter/divide-code-blocks.lua

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
11
-- draw horizontal rules above and below code blocks to separate them nicely
22

3-
code_classes =
3+
fontsize_classes =
44
{
5-
["normal"] = {
6-
["font"] = "\\small",
7-
},
8-
["small"] = {
9-
["font"] = "\\scriptsize",
10-
},
11-
["tiny"] = {
12-
["font"] = "\\tiny",
13-
},
5+
["normal"] = "\\small",
6+
["small"] = "\\scriptsize",
7+
["tiny"] = "\\tiny",
148
}
159

1610
function CodeBlock(block)
17-
local class_spec = code_classes["normal"]
11+
local fontsize = fontsize_classes["normal"]
1812
for _, class in ipairs(block.classes) do
19-
local maybe_spec = code_classes[string.lower(class)]
20-
if maybe_spec then
21-
class_spec = maybe_spec
13+
local maybe_fontsize = fontsize_classes[string.lower(class)]
14+
if maybe_fontsize then
15+
fontsize = maybe_fontsize
2216
break
2317
end
2418
end
@@ -30,11 +24,10 @@ function CodeBlock(block)
3024

3125
block.text = block.text:gsub("\r", "") -- Remove carriage-returns.
3226

33-
font = class_spec["font"]
3427
return {
3528
pandoc.RawInline('latex', string.format([[
3629
\BeginCodeBlock{%s}
37-
]], font)),
30+
]], fontsize)),
3831
block,
3932
pandoc.RawInline('latex', [[
4033
\EndCodeBlock

filter/tabularx.lua

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
-- Use xltabular's xltabular environment instead of longtable to write LaTeX tables.
22
-- Run this filter after pandoc-crossref.
33

4+
fontsize_classes =
5+
{
6+
["normal"] = "\\small",
7+
["small"] = "\\scriptsize",
8+
["tiny"] = "\\tiny",
9+
}
10+
411
function Length(element)
512
local n = 0
613
for key, value in pairs(element) do
@@ -234,6 +241,15 @@ end
234241
-- which gives us the option to draw the full grid of the table.
235242
function Table(tbl)
236243
if FORMAT =='latex' then
244+
local fontsize = fontsize_classes["normal"]
245+
for _, class in ipairs(tbl.classes) do
246+
local maybe_fontsize = fontsize_classes[string.lower(class)]
247+
if maybe_fontsize then
248+
fontsize = maybe_fontsize
249+
break
250+
end
251+
end
252+
237253
tbl.colspecs = NormalizeColumns(tbl.colspecs)
238254
local latex_code = ''
239255

@@ -275,6 +291,9 @@ function Table(tbl)
275291
latex_code = latex_code .. '\\addtocounter{table}{-1}\n'
276292
end
277293

294+
-- Set the font size.
295+
latex_code = latex_code .. '\\begingroup' .. fontsize
296+
278297
--
279298
-- Begin the xltabular environment
280299
--
@@ -370,7 +389,7 @@ function Table(tbl)
370389
-- End the tabular environment
371390
--
372391

373-
latex_code = latex_code .. '\\end{xltabular}\n'
392+
latex_code = latex_code .. '\\end{xltabular}\\endgroup\n'
374393

375394
-- Return a raw LaTeX blob with our encoded table.
376395
return pandoc.RawBlock('tex', latex_code)

guide.tcg

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,6 +1238,56 @@ Each type of Markdown table provides different support for alignment. See the
12381238
[Pandoc table documentation](https://pandoc.org/chunkedhtml-demo/8.9-tables.html)
12391239
for more details.
12401240

1241+
#### Font size
1242+
1243+
The font size of tables can be customized via the `.small` or `.tiny` classes.
1244+
1245+
```md
1246+
Table: Font size demonstration (small) {#tbl:fontsize-demonstration .small}
1247+
1248+
+-------+-------+-----------------+
1249+
| Fruit | Color | Description |
1250+
+=======+=======+=================+
1251+
| Apple | Red | Useful for pie. |
1252+
+-------+-------+-----------------+
1253+
| Pear | Green | Useful for pie? |
1254+
+-------+-------+-----------------+
1255+
1256+
Here is how to customize the font size of a table that has no caption:
1257+
1258+
: {.tiny}
1259+
1260+
+--------+--------+---------------------+
1261+
| Fruit | Color | Description |
1262+
+========+========+=====================+
1263+
| Banana | Yellow | Useful for pie. |
1264+
+--------+--------+---------------------+
1265+
| Tomato | Red | Not useful for pie. |
1266+
+--------+--------+---------------------+
1267+
```
1268+
1269+
These tables render like so:
1270+
1271+
Table: Font size demonstration (small) {#tbl:fontsize-demonstration .small}
1272+
1273+
+-------+-------+-----------------+
1274+
| Fruit | Color | Description |
1275+
+=======+=======+=================+
1276+
| Apple | Red | Useful for pie. |
1277+
+-------+-------+-----------------+
1278+
| Pear | Green | Useful for pie? |
1279+
+-------+-------+-----------------+
1280+
1281+
: {.tiny}
1282+
1283+
+--------+--------+---------------------+
1284+
| Fruit | Color | Description |
1285+
+========+========+=====================+
1286+
| Banana | Yellow | Useful for pie. |
1287+
+--------+--------+---------------------+
1288+
| Tomato | Red | Not useful for pie. |
1289+
+--------+--------+---------------------+
1290+
12411291
## HTML Tables {#sec:html-tables}
12421292

12431293
A rowspan/colspan table like @tbl:fruits-grid can be implemented in HTML like

0 commit comments

Comments
 (0)