diff --git a/filter/divide-code-blocks.lua b/filter/divide-code-blocks.lua index 9152d45..32b7aeb 100644 --- a/filter/divide-code-blocks.lua +++ b/filter/divide-code-blocks.lua @@ -1,24 +1,18 @@ -- draw horizontal rules above and below code blocks to separate them nicely -code_classes = +fontsize_classes = { - ["normal"] = { - ["font"] = "\\small", - }, - ["small"] = { - ["font"] = "\\scriptsize", - }, - ["tiny"] = { - ["font"] = "\\tiny", - }, + ["normal"] = "\\small", + ["small"] = "\\scriptsize", + ["tiny"] = "\\tiny", } function CodeBlock(block) - local class_spec = code_classes["normal"] + local fontsize = fontsize_classes["normal"] for _, class in ipairs(block.classes) do - local maybe_spec = code_classes[string.lower(class)] - if maybe_spec then - class_spec = maybe_spec + local maybe_fontsize = fontsize_classes[string.lower(class)] + if maybe_fontsize then + fontsize = maybe_fontsize break end end @@ -30,11 +24,10 @@ function CodeBlock(block) block.text = block.text:gsub("\r", "") -- Remove carriage-returns. - font = class_spec["font"] return { pandoc.RawInline('latex', string.format([[ \BeginCodeBlock{%s} - ]], font)), + ]], fontsize)), block, pandoc.RawInline('latex', [[ \EndCodeBlock diff --git a/filter/tabularx.lua b/filter/tabularx.lua index bca373b..bd37196 100644 --- a/filter/tabularx.lua +++ b/filter/tabularx.lua @@ -1,6 +1,13 @@ -- Use xltabular's xltabular environment instead of longtable to write LaTeX tables. -- Run this filter after pandoc-crossref. +fontsize_classes = +{ + ["normal"] = "\\small", + ["small"] = "\\scriptsize", + ["tiny"] = "\\tiny", +} + function Length(element) local n = 0 for key, value in pairs(element) do @@ -234,6 +241,15 @@ end -- which gives us the option to draw the full grid of the table. function Table(tbl) if FORMAT =='latex' then + local fontsize = fontsize_classes["normal"] + for _, class in ipairs(tbl.classes) do + local maybe_fontsize = fontsize_classes[string.lower(class)] + if maybe_fontsize then + fontsize = maybe_fontsize + break + end + end + tbl.colspecs = NormalizeColumns(tbl.colspecs) local latex_code = '' @@ -275,6 +291,9 @@ function Table(tbl) latex_code = latex_code .. '\\addtocounter{table}{-1}\n' end + -- Set the font size. + latex_code = latex_code .. '\\begingroup' .. fontsize + -- -- Begin the xltabular environment -- @@ -370,7 +389,7 @@ function Table(tbl) -- End the tabular environment -- - latex_code = latex_code .. '\\end{xltabular}\n' + latex_code = latex_code .. '\\end{xltabular}\\endgroup\n' -- Return a raw LaTeX blob with our encoded table. return pandoc.RawBlock('tex', latex_code) diff --git a/guide.tcg b/guide.tcg index b8b66be..c21ae9a 100644 --- a/guide.tcg +++ b/guide.tcg @@ -1238,6 +1238,56 @@ Each type of Markdown table provides different support for alignment. See the [Pandoc table documentation](https://pandoc.org/chunkedhtml-demo/8.9-tables.html) for more details. +#### Font size + +The font size of tables can be customized via the `.small` or `.tiny` classes. + +```md +Table: Font size demonstration (small) {#tbl:fontsize-demonstration .small} + ++-------+-------+-----------------+ +| Fruit | Color | Description | ++=======+=======+=================+ +| Apple | Red | Useful for pie. | ++-------+-------+-----------------+ +| Pear | Green | Useful for pie? | ++-------+-------+-----------------+ + +Here is how to customize the font size of a table that has no caption: + +: {.tiny} + ++--------+--------+---------------------+ +| Fruit | Color | Description | ++========+========+=====================+ +| Banana | Yellow | Useful for pie. | ++--------+--------+---------------------+ +| Tomato | Red | Not useful for pie. | ++--------+--------+---------------------+ +``` + +These tables render like so: + +Table: Font size demonstration (small) {#tbl:fontsize-demonstration .small} + ++-------+-------+-----------------+ +| Fruit | Color | Description | ++=======+=======+=================+ +| Apple | Red | Useful for pie. | ++-------+-------+-----------------+ +| Pear | Green | Useful for pie? | ++-------+-------+-----------------+ + +: {.tiny} + ++--------+--------+---------------------+ +| Fruit | Color | Description | ++========+========+=====================+ +| Banana | Yellow | Useful for pie. | ++--------+--------+---------------------+ +| Tomato | Red | Not useful for pie. | ++--------+--------+---------------------+ + ## HTML Tables {#sec:html-tables} A rowspan/colspan table like @tbl:fruits-grid can be implemented in HTML like