'Sphinx (documentation tool): set tab width in output

How do you set tab width in HTML output of Sphinx code snippets highlighted by Pygments? By default it is the annoying 8, but I want 4. Did not find a word about this setting in Sphinx conf.py.



Solution 1:[1]

Add something like this to your conf.py:

import re

def process_docstring(app, what, name, obj, options, lines):
    spaces_pat = re.compile(r"( {8})")
    ll = []
    for l in lines:
        ll.append(spaces_pat.sub("    ",l))
    lines[:] = ll

def setup(app):
    app.connect('autodoc-process-docstring', process_docstring)

See also Sphinx Docstring preprocessing documentation.

Solution 2:[2]

I asked the same question on sphinx-dev group and it turns out it's a problem with Docutils which is used by Sphinx. Docutils replace all tabs with 8 spaces and currently there is no way to change that value from Sphinx.

http://groups.google.com/group/sphinx-dev/browse_thread/thread/35b8071ffe9a8feb

The only feasible solution seems to be to follow the advice from S.Lott and John Paulett in comments to my question -- use spaces instead of tabs.

Solution 3:[3]

I've changes the font-size of the white-space in my custom.css This is a easy tweak and works very well. This will only change the code-block and nothing else. Which is what I wanted.

_static/custon.css

div.highlight pre .w {
    font-size: 50%;
}

Note: The solution of Docstring preprocessing was messing up my python code docstring which was causing error's while build.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 gieffe
Solution 2 jbasko
Solution 3 Philippe Forest