【问题标题】:Inline index keyword highlighted内联索引关键字突出显示
【发布时间】:2026-02-03 01:55:02
【问题描述】:

我正在尝试编写 restructuredText 标记,同时在索引中创建一个条目并突出显示文本中的提及。我试过了

In this chapter, we introduce :index:`*Foo* <Foo>`, a
crucial concept in this example

索引条目创建好了,但是文本会

在本章中,我们将介绍 *Foo*,这是本示例中的关键概念

而不是

在本章中,我们将介绍 Foo,这是本示例中的关键概念

我能以某种方式正确地做到这一点吗?

【问题讨论】:

    标签: python-sphinx restructuredtext


    【解决方案1】:

    您可以为span.xref.std.std-term 添加样式属性吗?例如在你的source/_static/custom.cssput:

    @import url("default.css");
    
    span.xref.std.std-term {
      font-style: italic;
    }
    

    所有索引引用都将是斜体 ...

    【讨论】:

    • 这里生成的 HTML 好像是we introduce &lt;span class="target" id="index-0"&gt;&lt;/span&gt;Foo,所以这个词本身不能用 CSS 来寻址。它不适用于 PDF 输出。
    • 很抱歉我错了:我误读了index并认为glossary因此我的测试。但是为什么你不能用 CSS 来处理你的 span 元素呢?然而,PDF 输出确实是另一回事。
    • 因为span元素是空的,后面是我要高亮的字。
    【解决方案2】:

    到目前为止我发现的最好的解决方法是

    In this chapter, we introduce :index:`\ <Foo>`\ *Foo*, a
    crucial concept in this example
    

    【讨论】: