Skip to content
Advertisement

How to highlight custom keywords in Quarto docs code chunks?

I’m writing a brief Quarto document where I explain some SQL clauses/keywords. I’d like to have all of those clauses to be highlighted in my code chunks, but not all of them are supported by default syntax highlighting. Is there a way to add custom keywords to syntax highlighting settings?

For example, I wrote the following code chunks:

And i got the following outputs in my rendered document:

enter image description here enter image description here

I want both OFFSET and TOP to be highlighted in orange, like SELECT, FROM etc. How can I do that?

Advertisement

Answer

There are several SQL highlight variants, you can get a list of those with quarto pandoc --list-highlight-languages and switching from sql to sqlpostgresql will get you highlighted OFFSET.

It’s surprisingly easy to customize any of the existing highlights, https://pandoc.org/MANUAL.html#syntax-highlighting points you to KDE-style XML syntax definition files, https://github.com/KDE/syntax-highlighting/tree/master/data/syntax . Pick sql.xml (or any other ) as a base and add additional keywords:

I also changed the name attribute (<language name="SQL_CUSTOM" ... and saved it next to Quarto qmd as sql_custom.xml, having all files in a working directory, Quarto / Pandoc picked up updated definition file once it was added to syntax-definitions list.

Sample qmd:

And rendered result: enter image description here

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement