sphinx,restructuredtext:为单个单词设置颜色

Ada*_*tan 18 html restructuredtext python-sphinx

有没有办法在sphinx中设置单个单词(或字符)的颜色?我很确定应该有一些标记标记,比如HTML font标记.

ahc*_*cox 11

如果你想在不与html绑定的情况下这样做,请尝试将不同于正常正文的样式应用到单词中.

在这个改编自rst2pdf手册的例子中,我应用了我正在使用的后端红色的现有量规样式:

Before red.

.. role:: rubric

I like color :rubric:`rubric`.

After red.
Run Code Online (Sandbox Code Playgroud)

该单词的实际外观取决于您在生成文档时使用的样式表中如何定义所选样式.如果您想要蓝色文本,请创建蓝色文本样式并从普通文本样式派生.样式表是特定于后端的,您可能正在使用默认值.要打印rst2pdf.py的默认值,请执行此操作(从rst2pdf手册):

rst2pdf --print-stylesheet
Run Code Online (Sandbox Code Playgroud)

继续rst2pdf样式表的示例,将其添加到样式表以使其具有蓝色文本样式:

bluetext:
  parent: bodytext
  textColor: blue
Run Code Online (Sandbox Code Playgroud)

在文档中,您可以引用此样式以获得蓝色字.请注意,这个位是通用的,如果在html或后端的样式表中定义蓝色样式,则应该生成蓝色文本.

Before blue.

.. role:: bluetext

I like color :bluetext:`blue`.

After blue.
Run Code Online (Sandbox Code Playgroud)

生成的pdf具有彩色字: 在此输入图像描述


Nær*_*een 11

在我的Sphinx驱动的网站上,我使用了以下组合:

  • 包含角色定义的restructuredText文件,每种颜色一个 - 请参阅.special.rst(BitBucket链接)
  • 包含每个角色的颜色规则的CSS文件 - 请参阅hacks.css的第一行(BitBucket链接)

然后,在我需要颜色的每个rST文件中,我首先.special.rst在顶部导入,手动:

.. include:: .special.rst
Run Code Online (Sandbox Code Playgroud)

或者使用rst_epilogSphinx conf.py文件中的配置变量:

rst_epilog = "\n.. include:: .special.rst\n"
Run Code Online (Sandbox Code Playgroud)

然后,每个角色都可以在纯rST语法中轻松使用:

This is :red:`red !` And :blue:`this part is blue`.
Run Code Online (Sandbox Code Playgroud)

此页面上提供了更多详细信息 (法语,抱歉).

它非常适合html输出(和html一样),但不适用于PDF.请参阅上面第一个答案,以生成带颜色的PDF.

  • 感谢这个解决方案。它有效,但您知道如何解决“警告:“include”指令路径的问题:”?我不知道如何指定 `.special.rst` 位于 `conf.py` 的项目根目录中。我尝试了“/”和“~/”,但都不起作用。 (2认同)

ank*_*tis 7

Sphinx已经支持用于包含标准定义文件的颜色(但缺少 CSS 文件):s5defs.txt

  1. rst_epilog 在您的docs/conf.py文件中创建/将此文本附加到sphinx 配置的值:

     rst_prolog = """
     .. include:: <s5defs.txt>
    
     """
    
    Run Code Online (Sandbox Code Playgroud)
  2. 按照Sphinx 的说明添加带有颜色的 css(例如采用hack.css来自@Næreen 的回答):

然后您可以使用:

Some :red:`colored text` at last!
Run Code Online (Sandbox Code Playgroud)

提示:如果您还希望样式出现在Latex输出中,请 阅读此 SO


Ada*_*tan 6

这有效,但将HTML留在单独的段落中.

.. raw:: html

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

And a word without color
Run Code Online (Sandbox Code Playgroud)

如果有人有更好的答案,我会接受.