29

Is there a way to set the color of single words (or characters) in sphinx? I'm pretty sure there should be some markup tag, like HTML's font tag.

Adam Matan
  • 128,757
  • 147
  • 397
  • 562

5 Answers5

16

On my Sphinx-powered website, I use a combination of:

  • A restructuredText file containing roles definitions, one for each color - see .special.rst (BitBucket link)
  • A CSS file containing color rules for each role - see the first lines of hacks.css (BitBucket link)

Then, in every rST file where I need colors, I first import .special.rst at the top, either manually:

.. include:: .special.rst

Or with the rst_epilog configuration variable in Sphinx's conf.py file:

rst_epilog = "\n.. include:: .special.rst\n"

And then each role can be used easily in pure rST syntax:

This is :red:`red !` And :blue:`this part is blue`.

More details are given on this page (in French, sorry).

It works perfectly well for html output (and html-like), but not for PDF. Refer to the first answer above for producing a PDF with colors.

Næreen
  • 1,126
  • 1
  • 12
  • 23
  • 1
    I like this solution! – umbe1987 Sep 29 '17 at 10:21
  • 2
    Thanks for this solution. It works but do you know how to solve `WARNING: Problems with "include" directive path:`? I don't know how to specify that `.special.rst` to be at project root directory in `conf.py`. I tried `/` and `~/` but neither works. – YOUNG Jan 23 '18 at 19:02
  • 1
    I don't know, in my case the `conf.py` file and all the `rst` files are in the same folder, so there is no need to specify anything. – Næreen Jan 24 '18 at 15:47
  • 1
    After one year (see my first comment ;)) this solution is still great, although I have noticed (at least from my side), that after the first succesful build with `make html`, the following attempts to build with the same command always end up with (e.g. for red color) `WARNING: Unknown interpreted text role "red".` warning. I have to do `make clean html` and then `make html` again. Other than this, everything works just fine. – umbe1987 Jan 02 '19 at 14:27
  • Amazing solution. As @umbe1987 said, compile errors occur when rebuilding the HTML. I managed to fix it using `rst_prolog` instead of `rst_epilog`. The difference is that `rst_prolog` includes the file at the beginning, while `rst_epilog` includes it at the end ([See `rst_prolog` in Sphinx docs](http://www.sphinx-doc.org/en/master/usage/configuration.html#confval-rst_prolog)). – Turgut Sarıçam Aug 31 '19 at 13:39
13

Sphinx already supports colors with the s5defs.txt standard definition file intended for inclusion (but is missing the CSS file):

  1. Create/append this text to the value of rst_epilog sphinx configuration, in your docs/conf.py file:

     rst_prolog = """
     .. include:: <s5defs.txt>
    
     """
    
  2. Follow Sphinx's instructions to add a css with the colors (e.g. adopt the hack.css from @Næreen's answer):

  • Place your css file into e.g. _static/css/s4defs-roles.css;

  • append it's path into shtml_css_files sphinx configuration:

        html_css_files = ['css/s4defs-roles.css']
    

You may then use:

Some :red:`colored text` at last!

TIP: Read this SO if you also want the styling to appear in Latex output.

ankostis
  • 8,579
  • 3
  • 47
  • 61
  • 1
    An outright easy to follow solution for something which should not have been such a big deal in the first place. **Thank you.** One problem I faced though - i.e. with the `.. default-role::` in `rst-prolog` in `conf.py` file, my page layout was going haywire. Initially I made major changes in the `.css` file. But in the end I realized that `.. default-role::` was the real culprit. Removing it made everything fall in place. **Including the font color!!** – shaan Aug 08 '20 at 18:32
  • Thank @shaan! Fixed. – ankostis Aug 09 '20 at 22:39
13

If you want to do this without being tied to html, try applying a different style than normal body text to your word.

In this example adapted from the rst2pdf manual, I apply the existing rubric style which is red in the backend that I am using:

Before red.

.. role:: rubric

I like color :rubric:`rubric`.

After red.

The actual look of the word will depend on how the style you choose is defined in the stylesheet that you use when generating your document. If you want blue text, make a blue text style and derive it from the normal text style. The stylsheet is backend-specific and you may be using the default. To print the default for rst2pdf.py, do this (from the rst2pdf manual):

rst2pdf --print-stylesheet

Continuing the example for a rst2pdf stylesheet, add this to your stylesheet to have a blue text style:

bluetext:
  parent: bodytext
  textColor: blue

In the document you can reference this style to get a blue word. Note this bit is generic, and should make blue text if you define a blue style in your html or whatever backend's stylesheet.

Before blue.

.. role:: bluetext

I like color :bluetext:`blue`.

After blue.

The generated pdf has the coloured words: enter image description here

ahcox
  • 9,349
  • 5
  • 33
  • 38
  • Actually RsT already supports color-roles by including the `` "standard" definition file (see [my answer](https://stackoverflow.com/a/60991308/548792), below). – ankostis Apr 23 '20 at 15:01
7

This works, but leaves the HTML in a separate paragraph.

.. raw:: html

    <font color="blue">Blue word,</font>

And a word without color

If anyone has a better answer, I will accept it.

Adam Matan
  • 128,757
  • 147
  • 397
  • 562
4

Just a quick note because I landed here looking for something similar for html.

This works on Sphinx v2.0.1 for me. This uses the concept reported by @adam-matan but doesn't cause any formatting issues (i.e. the paragraph problem).

reference: reStructuredText Directives

.. role:: raw-html(raw)
   :format: html

:raw-html:`<font color="blue">Blue word,</font>` And a word without color