Created
April 3, 2023 07:28
-
-
Save mpizenberg/c6ed7bc3992ee5dfed55edce508080bb to your computer and use it in GitHub Desktop.
Example for code blocks in Typst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #set par(justify: true) | |
| *Goal*: being able to add line numbers, which are correct even in case of long lines that need wrapping. | |
| *Strategy*: duplicate the code block, once for getting the line numbers correct, and the other for syntax highlighting. The idea is to split lines and prefix each line with its line number such that line wrapping should be respected. | |
| #show raw.where(block: true): it => { set par(justify: false); grid( | |
| columns: (100%, 100%), | |
| column-gutter: -100%, | |
| block(width: 100%, inset: 1em, for i, line in it.text.split("\n") { | |
| box(width: 0pt, align(right, str(i + 1) + h(2em))) | |
| hide(line) | |
| linebreak() | |
| }), | |
| block(radius: 1em, fill: luma(246), width: 100%, inset: 1em, it), | |
| )} | |
| #set text(size: 12pt) | |
| #let code = read("config-pascal.json") | |
| #raw(code, block:true, lang: "json") | |
| ```haskell | |
| Bool = True | False | |
| ``` |
This variant gave me the look I was looking for:
#show raw.where(block: true): it => {
set text(0.9em)
set par(justify: false)
let i = 0
grid(
columns: (100%, 100%),
column-gutter: -100%,
block(width: 100%, for line in it.text.split("\n") {
i += 1
box(width: 0pt, align(right, text(size: 0.9em, fill: luma(127), str(i) + h(1.5em))))
hide(line)
linebreak()
}),
it,
)
}when used like this:
#figure(
block(stroke: 0.5pt, width: 100%, inset: 5pt)[
#set align(left)
code here...
])This belongs to a standard library. Is there anything like it in the "universe"?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Typst syntax may have changed a little since then, so I came up with this slight variation on your original.