Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sphinx, restructuredtext: set color for a single word

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.

like image 615
Adam Matan Avatar asked Sep 13 '10 17:09

Adam Matan


3 Answers

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.

like image 179
Næreen Avatar answered Oct 22 '22 20:10

Næreen


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

like image 12
ahcox Avatar answered Oct 22 '22 22:10

ahcox


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.

like image 12
ankostis Avatar answered Oct 22 '22 22:10

ankostis