skip to Main Content

I would like to retain the syntax highlighting for code written in code blocks in the Quarto document (.qmd) after rendering a PDF. Please let me know if this is possible.

Code in .qmd file

Code After Rendering

Colors not retained after rendering

2

Answers


  1. Chosen as BEST ANSWER

    There is a difference between the syntax highlighting theme in your IDE (e.g., VS code) and the syntax highlighting that is used when rendering a PDF. Details can be found here: Quarto Synax Highlighting

    You are able to choose from a list of supported themes (arrow, pygments, tango, espresso, zenburn, kate, monochrome, breezedark, haddock, atom-one, ayu, breeze, dracula, github, gruvbox, monokai, nord, oblivion, printing, radical, solarized, and vim), using the following command at the top of the document:

    ---
    title: "title"
    author: First Last
    format:
      pdf:
        highlight-style: atom-one
    ---
    

    You are also able to customize your own syntax highlighting style using the command (pandoc's needs to be installed) and setting the highlight-style to the newly created theme.

    quarto pandoc --print-highlight-style pygments > mytheme.theme

    ---
    title: "title"
    author: First Last
    format:
      pdf:
        highlight-style: mytheme.theme
    ---
    

    You are then able to customize your theme file (mytheme.theme).

    My goal was to try to get the color scheme of the code chunks in rendered PDF identical to the selected VS code theme (in my case Dark+). The best option I found was to use the 'Developer: Generate Color Theme From Current Settings' command (i.e., workbench.action.generateColorTheme) to generate a document of the current color theme. After several rounds or trial and error, I was able to create a theme similar to the VS Code Dark+ theme (see code below).

    Information about the style attributes in the .theme file can be found in this GitHub Repo

    mytheme.theme file

    {
    "text-color": "#9CDCFE",
    "background-color": "#1e1e1e",
    "line-number-color": "#aaaaaa",
    "line-number-background-color": null,
    "text-styles": {
        "Alert": {
            "text-color": "#ff0000",
            "background-color": null,
            "bold": true,
            "italic": false,
            "underline": false
        },
        "Annotation": {
            "text-color": "#6A9955",
            "background-color": null,
            "bold": true,
            "italic": true,
            "underline": false
        },
        "Attribute": {
            "text-color": "#9CDCFE",
            "background-color": null,
            "bold": false,
            "italic": false,
            "underline": false
        },
        "BaseN": {
            "text-color": "#B5CEA8",
            "background-color": null,
            "bold": false,
            "italic": false,
            "underline": false
        },
        "BuiltIn": {
            "text-color": "#C586C0",
            "background-color": null,
            "bold": false,
            "italic": false,
            "underline": false
        },
        "Char": {
            "text-color": "#CE9178",
            "background-color": null,
            "bold": false,
            "italic": false,
            "underline": false
        },
        "Comment": {
            "text-color": "#6A9955",
            "background-color": null,
            "bold": false,
            "italic": true,
            "underline": false
        },
        "CommentVar": {
            "text-color": "#6A9955",
            "background-color": null,
            "bold": true,
            "italic": true,
            "underline": false
        },
        "Constant": {
            "text-color": "#880000",
            "background-color": null,
            "bold": false,
            "italic": false,
            "underline": false
        },
        "ControlFlow": {
            "text-color": "#C586C0",
            "background-color": null,
            "bold": true,
            "italic": false,
            "underline": false
        },
        "DataType": {
            "text-color": "#9CDCFE",
            "background-color": null,
            "bold": false,
            "italic": false,
            "underline": false
        },
        "DecVal": {
            "text-color": "#B5CEA8",
            "background-color": null,
            "bold": false,
            "italic": false,
            "underline": false
        },
        "Documentation": {
            "text-color": "#ba2121",
            "background-color": null,
            "bold": false,
            "italic": true,
            "underline": false
        },
        "Error": {
            "text-color": "#F44747",
            "background-color": null,
            "bold": true,
            "italic": false,
            "underline": false
        },
        "Extension": {
            "text-color": null,
            "background-color": null,
            "bold": false,
            "italic": false,
            "underline": false
        },
        "Float": {
            "text-color": "#B5CEA8",
            "background-color": null,
            "bold": false,
            "italic": false,
            "underline": false
        },
        "Function": {
            "text-color": "#DCDCAA",
            "background-color": null,
            "bold": false,
            "italic": false,
            "underline": false
        },
        "Import": {
            "text-color": "#C586C0",
            "background-color": null,
            "bold": true,
            "italic": false,
            "underline": false
        },
        "Information": {
            "text-color": "#6A9955",
            "background-color": null,
            "bold": true,
            "italic": true,
            "underline": false
        },
        "Keyword": {
            "text-color": "#569CD6",
            "background-color": null,
            "bold": true,
            "italic": false,
            "underline": false
        },
        "Operator": {
            "text-color": "#D4D4D4",
            "background-color": null,
            "bold": false,
            "italic": false,
            "underline": false
        },
        "Other": {
            "text-color": "#C586C0",
            "background-color": null,
            "bold": false,
            "italic": false,
            "underline": false
        },
        "Preprocessor": {
            "text-color": "#bc7a00",
            "background-color": null,
            "bold": false,
            "italic": false,
            "underline": false
        },
        "RegionMarker": {
            "text-color": "#8c00ff",
            "background-color": null,
            "bold": true,
            "italic": true,
            "underline": false
        },
        "SpecialChar": {
            "text-color": "#CE9178",
            "background-color": null,
            "bold": false,
            "italic": false,
            "underline": false
        },
        "SpecialString": {
            "text-color": "#bb6688",
            "background-color": null,
            "bold": false,
            "italic": false,
            "underline": false
        },
        "String": {
            "text-color": "#CE9178",
            "background-color": null,
            "bold": false,
            "italic": false,
            "underline": false
        },
        "Variable": {
            "text-color": "#19177c",
            "background-color": null,
            "bold": false,
            "italic": false,
            "underline": false
        },
        "VerbatimString": {
            "text-color": "#CE9178",
            "background-color": null,
            "bold": false,
            "italic": false,
            "underline": false
        },
        "Warning": {
            "text-color": "#6A9955",
            "background-color": null,
            "bold": true,
            "italic": true,
            "underline": false
        }
    }
    

    }


  2. The syntax highlighting in the editor and during output generation are two completely separate entities. It probably is possible somehow, but would require manual translation of the color theme into a pandoc highlighting theme.

    See, for example, Pandoc: What are the available syntax highlighters?.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search