The Pygments Style Class

This is what I am using for this site to highlight source code via pygments. This custom class is active when using the dark theme (via pygments_dark_style in the conf.py). Sphinx allows to customize the style depending on the CSS media query, so you can have different syntax highlighters based on whether the dark or light theme is active. For the light theme, I use the default highlight definitions, but it’s quite easy to customize them as well (see below)

How to use such a custom style?

  • Your style class must inherit from pygments.style.Style.

  • Copy this file to your sphinx site’s source directory (this is the directory that contains conf.py.)

  • Modify whatever color attribute you want.

  • In your conf.py make the path available for import like so:

1import os
2import sys
3sys.path.append(os.path.dirname(__file__))
  • In your conf.py use it with pygments_dark_style = "pyg_dark.Pyg_Dark". For a light theme use pygments_style instead.

Here is the full class defining the style:

Sphinx syntax hl class (reference)
 1from pygments.style import Style
 2from pygments.token import (
 3    Keyword,
 4    Name,
 5    Comment,
 6    Error,
 7    Number,
 8    Operator,
 9    Generic,
10    Whitespace,
11    Punctuation,
12    Literal,
13    )
14
15
16class Pyg_Dark(Style):
17    background_color = "#15151a"  # class: '.highlight'
18    line_number_color = "#707090"
19    default_style = ""
20
21    styles = {
22        Error: "#a61717 bg:#e3d2d2 border:#FF0000",  # class: 'err'
23        # Error:                     "#a61717 bg:#e3d2d2 border:1px solid #FF0000", # class: 'err'
24        Comment: "italic #6a737d",  # class: 'c'
25        Comment.Preproc: "noitalic #007020",  # class: 'cp'
26        Comment.PreprocFile: "italic #6a737d",  # class: 'cpf'
27        Comment.Hashbang: "italic #6a737d",  # class: 'ch'
28        Comment.Multiline: "italic #6a737d",  # class: 'cm'
29        Comment.Single: "italic #6a737d",  # class: 'c1'
30        Comment.Special: "bold italic #999999 bg:#fff0f0",  # class: 'cs'
31        Keyword: "bold #b16286",  # class: 'k'
32        Keyword.Constant: "bold #b16286",  # class: 'kc'
33        Keyword.Declaration: "bold #b16286",  # class: 'kd'
34        Keyword.Namespace: "bold #007020",  # class: 'kn'
35        Keyword.Pseudo: "#007020",  # class: 'kp'
36        Keyword.Reserved: "bold #772050",  # class: 'kr'
37        Keyword.Type: "#772088",  # class: 'kt'
38        Operator: "bold #805010",  # class: 'o'
39        Operator.Word: "bold #805010",  # class: 'ow'
40        Punctuation: "bold #4040aa",  # class: 'p'
41        Name: "#999999",  # class: 'n'
42        Name.Attribute: "#008080",  # class: 'na'
43        Name.Builtin: "#0086b3",  # class: 'nb'
44        Name.Class: "#445588",  # class: 'nc'
45        Name.Constant: "#008080",  # class: 'no'
46        Name.Decorator: "bold #555555",  # class: 'nd'
47        Name.Entity: "bold #800080",  # class: 'ni'
48        Name.Exception: "bold #990000",  # class: 'ne'
49        Name.Function: "#206688",  # class: 'nf'
50        Name.Label: "bold #002070",  # class: 'nl'
51        Name.Namespace: "bold #708422",  # class: 'nn'
52        Name.Tag: "bold #22863a",  # class: 'nt'
53        Name.Variable: "#999999",  # class: 'nv'
54        Name.Builtin.Pseudo: "#999999",  # class: 'bp'
55        Name.Function.Magic: "#06287e",  # class: 'fm'
56        Name.Variable.Class: "#008080",  # class: 'vc'
57        Name.Variable.Global: "#008080",  # class: 'vg'
58        Name.Variable.Instance: "#008080",  # class: 'vi'
59        Name.Variable.Magic: "#bb60d5",  # class: 'vm'
60        Number: "#208050",  # class: 'm'
61        Literal: "#032f62",  # class: 'l'
62        Literal.String: "#88872a",  # class: 's'
63        Literal.Number.Bin: "#40804f",  # class: 'mb'
64        Literal.Number.Float: "#40804f",  # class: 'mf'
65        Literal.Number.Hex: "#40804f",  # class: 'mh'
66        Literal.Number.Integer: "#40804f",  # class: 'mi'
67        Literal.Number.Oct: "#40804f",  # class: 'mo'
68        Literal.String.Affix: "#88872a",  # class: 'sa'
69        Literal.String.Backtick: "#88872a",  # class: 'sb'
70        Literal.String.Char: "#88872a",  # class: 'sc'
71        Literal.String.Delimiter: "#88872a",  # class: 'dl'
72        Literal.String.Doc: "italic #88872a",  # class: 'sd'
73        Literal.String.Double: "#88872a",  # class: 's2'
74        Literal.String.Escape: "bold #88872a",  # class: 'se'
75        Literal.String.Heredoc: "#88872a",  # class: 'sh'
76        Literal.String.Interpol: "italic #88872a",  # class: 'si'
77        Literal.String.Other: "#88872a",  # class: 'sx'
78        Literal.String.Regex: "#009926",  # class: 'sr'
79        Literal.String.Single: "#88872a",  # class: 's1'
80        Literal.String.Symbol: "#990073",  # class: 'ss'
81        Literal.Number.Integer.Long: "#009999",  # class: 'il'
82        Generic.Deleted: "#A00000 bg:#ffdddd",  # class: 'gd'
83        Generic.Emph: "italic",  # class: 'ge'
84        Generic.Error: "#aa0000",  # class: 'gr'
85        Generic.Heading: "bold #000080",  # class: 'gh'
86        Generic.Inserted: "#00A000 bg:#ddffdd",  # class: 'gi'
87        Generic.Output: "#333333",  # class: 'go'
88        Generic.Prompt: "bold #c65d09",  # class: 'gp'
89        Generic.Strong: "bold",  # class: 'gs'
90        Generic.Subheading: "bold #800080",  # class: 'gu'
91        Generic.Traceback: "#0040D0",  # class: 'gt'
92        Whitespace: "#bbbbbb",  # class: 'w'
93    }

Tags: site sphinx