sphinx, restructuredtext: set color for a single word

Adam Matan picture Adam Matan · Sep 13, 2010 · Viewed 15k times · Source

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.

Answer

ahcox picture ahcox · Mar 17, 2012

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