2

I'm used to write Rest documents while I never used LaTex.

What I would like to do is create some font color roles that I can add inline the text (e.g. :red:this text is red) that work both in html and in latexpdf compilation.

I've found a similar question here, but I cannot reproduce it.

I think that the magic will be done changing the conf.py file, but I didn't find out how.

Moreover, during the latexpdf compilation, in the _build/latex directory, there is a sphinx.sty file that contains a lot of things for the customization of the final pdf file.

If I want to change some parameter of the final pdf file, have I to edit this file, put it somewhere and tell sphinx to take this style file?

Sorry for all these stuff, but I'm a little bit confused..

Thanks!

Community
  • 1
  • 1
matteo
  • 4,683
  • 9
  • 41
  • 77

2 Answers2

2

Just add:

  1. a 'source/_templates/layout.html' file with

    {% extends "!layout.html" %}
    
    {% block extrahead %}
    <link rel="stylesheet" type="text/css" 
         href="{{ pathto('_static/custom.css', 1) }}" /> 
    
    {% endblock %}
    
  2. a 'source/_static/custom.css' file with

    @import url("default.css");
    
    .red {
      color: red;
    }
    

You can now use a :red: role in your rst files. Don't forget the back tick and the clean target after html or css editions:

A :red:`red`text

$> make clean html

The global tree:

test-sphinx
├── Makefile
├── build
└── source
    ├── _static
    │   └── custom.css
    ├── _templates
    │   └── layout.html
    ├── conf.py
    └── index.rst

An index.rst example:

TS test!
========

.. role:: red

This is :red:`just` a test …

Hope this help,

Antoine

aflp91
  • 641
  • 5
  • 12
  • Hi Antoine.. I tried what you suggested but it didn't worked. I just pasted the code in a file (`custom.css`) in the `_static` folder and called the role in the rst files with :red:`text`. Am I missing something? – matteo Aug 16 '15 at 20:28
  • I'm very sorry Matteo: I should have read my notepad with my notes … I edited my previous answer and tested it this time ! – aflp91 Aug 16 '15 at 22:25
  • Hi Antoine, thanks for you effort but it didn't work anyway. I copied and paste your code in the folders you said, but in the final draft I cannot see a red text.. By the way, once we get it to work, this role works also for pdf outputs (build with latexpdf)? Thanks again! – matteo Aug 17 '15 at 07:20
  • Bad … I've just tried again and it works there. But, about latexpdf, it's another story and I don't have the answer right now. – aflp91 Aug 17 '15 at 07:47
  • yes, very bad.. I tried again and again, but nothing changed... Have I to edit also the `conf.py` file? – matteo Aug 17 '15 at 07:57
  • I didn't. This is how I tested : used 'sphinx-quickstart' with almost all defaults (just put source and build separated) ; added files custom.css and layout.html; edited index.rst; make and that's all … Just made from scratch right now with success. – aflp91 Aug 17 '15 at 08:06
  • For latexpdf, what about the post http://stackoverflow.com/questions/13530489/adding-background-color-of-notes-to-sphinx-generated-pdf-files?rq=1 – aflp91 Aug 17 '15 at 08:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/87144/discussion-between-aflp91-and-matteo). – aflp91 Aug 17 '15 at 08:16
  • Thanks! I used `{% extends "!layout.html" %}{% set css_files = css_files + ["_static/override.css"] %}` as my layout.html file instead, which might be the "newer" way to do it. And I did not need `@import url("default.css");` in my CSS nor did I need the `clean` option in `make html`. Thank you works great! – mareoraft Apr 09 '16 at 21:01
  • In case this could help: do not leave a space between the ":red:" and the "`just`". Otherwise, it won't work. – Kyriakos Mar 03 '22 at 16:49
0

The answer by aflp91 works for setting the color using css for the html output, but it does not change the color in the latex output.

Aflp91's answer actually generates a custom role in the tex file, which can be overwritten to change the color. All that was missing was the code to actually customize the custom role in LaTex. To do so, add the following in conf.py:

latex_elements = {
    'passoptionstopackages': r'\PassOptionsToPackage{svgnames}{xcolor}',
    'preamble': r'''
\newcommand{\DUrolered}[1]{{\color{red} #1}}
''',
}

The DUrole syntax is described in the docutils documentation.