matteo
matteo

Reputation: 4873

create a role (font color) in sphinx that works with `make latexpdf`

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!

Upvotes: 2

Views: 2945

Answers (2)

Kyle Stemen
Kyle Stemen

Reputation: 76

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.

Upvotes: 2

aflp91
aflp91

Reputation: 673

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

Upvotes: 3

Related Questions