Skip to content

Allow code blocks to customize their font size. #224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions filter/divide-code-blocks.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
-- draw horizontal rules above and below code blocks to separate them nicely

code_classes =
{
["normal"] = {
["font"] = "\\small",
},
["small"] = {
["font"] = "\\scriptsize",
},
["tiny"] = {
["font"] = "\\tiny",
},
}

function CodeBlock(block)
local class = block.classes[1]
local class_spec = code_classes[string.lower(class or "")]
if not class_spec then
class_spec = code_classes["normal"]
end
font = class_spec["font"]
return {
pandoc.RawInline('latex', [[
\vskip 3pt%
pandoc.RawInline('latex', string.format([[
\vskip 3pt
\begin{customcodeblock}
\textbf{\textit{\textcolor{codeblock-header}{\small \BeginDemarcated{Code}}}}
]]),
%s
]], font)),
block,
pandoc.RawInline('latex', [[
\textbf{\textit{\textcolor{codeblock-header}{\small \EndDemarcated{Code}}}}
Expand Down
43 changes: 43 additions & 0 deletions guide.tcg
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,49 @@ The result looks like this:
int i = 42;
```

You can customize the font size, in case code runs off the edge of the page and can't be word-wrapped:

````md
``` {.small}
{
"Foo": {
"Bar": {
"Baz":
"/some/very/long/string/that/can't/be/word/wrapped/e.g./because/it's/encoded/in/json",
}
}
}
```
````

The result looks like this:

``` {.small}
{
"Foo": {
"Bar": {
"Baz":
"/some/very/long/string/that/can't/be/word/wrapped/e.g./because/it's/encoded/in/json",
}
}
}
```

You can use `.normal`, `.small`, or `.tiny` (illustrated below):

``` {.tiny}
{
"Foo": {
"Bar": {
"Baz":
"/some/very/long/string/that/can't/be/word/wrapped/e.g./because/it's/encoded/in/json",
}
}
}
```

The default case is `.normal`.

## Footnotes {#sec:footnotes}

```md
Expand Down
9 changes: 0 additions & 9 deletions template/tcg.tex
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,6 @@
% }
% \makeatother

% Make all verbatim environments use \small font.
\usepackage{etoolbox}
\makeatletter
\patchcmd{\@verbatim}
{\verbatim@font}
{\verbatim@font\small}
{}{}
\makeatother

% Left-justify the document.
\usepackage[document]{ragged2e}
\RaggedRight{}
Expand Down
Loading