【问题标题】:How to expand all the subsections on the sidebar toctree in Sphinx?如何在 Sphinx 中展开侧边栏目录树上的所有小节?
【发布时间】:2013-01-06 19:05:27
【问题描述】:

我想知道是否有办法扩展 index.rst 文件中包含的标题下的所有小节?

例如,它是这样的:

Section 1
Section 2
Section 3

这就是我想要的样子:

Section 1
  Subsection 1.1
  Subsection 1.2
  Subsection 1.3
Section 2
  Subsection 2.1
  Subsection 2.2
  Subsection 2.3
Section 3
  Subsection 3.1
  Subsection 3.2
  Subsection 3.3

如果我点击第 1 部分,它会显示下面的内容,但如果我点击第 2 部分,第 1 部分的内容将被隐藏,仅显示 2 部分。我希望每次在索引页面上都扩展所有 2 个部分。我试过添加toctreemaxdepth,没有任何效果。

【问题讨论】:

标签: python python-sphinx sidebar sections toctree


【解决方案1】:

好吧,我在尝试阅读 sphinx 源代码时丢失了大约 340 万个神经元(它是由一群鲁莽鲁莽的浣熊写的吗?!这么多抽象级别)。

所以:

  • 制作自己的 sphinx 主题(使用 3rd 方主题作为基础,非常简单。我为此使用“可读”主题)
  • 在你有theme.conf的目录下,添加一个“fulltoc.html”模板,包含一行:

fulltoc.html:

{{ toctree(collapse=False) }}

(嘿,注意到“折叠”参数了吗?)

  • 在 sphinx conf.py 中,修改 html_sidebars 选项以添加您的模板;并声明您的主题

conf.py:

html_theme_path = [customized_readable_theme.get_html_theme_path()]
html_theme = 'customized_readable'
html_sidebars = {'**': ['fulltoc.html', 'relations.html', 'sourcelink.html', 'searchbox.html']}
  • 重建文档

【讨论】:

    【解决方案2】:

    如果您使用的是 sphinx_rtd_theme,您可以通过更改文件 layout.html 中定义的“toctree maxdepth”值来更改 html 页面中侧边栏菜单的最大深度。该文件通常位于目录source/_themes/sphinx_rtd_theme 中。有几种解决方案:

    • 最简单、最快的解决方案:Show deeper toctree in sidebar

    • 您使用的是旧版本的主题。然后,您可以在以下行中设置一个新的“最大深度”值(例如 3):

      {% set toctree = toctree(maxdepth=3, collapse=False, includehidden=True) %}
      
    • 您使用的是最新版本的主题。那么你可能在文件 layout.html 中有这些行:

      {% set global_toc = toctree(maxdepth=theme_navigation_depth|int,
                                  collapse=theme_collapse_navigation|tobool,
                                  includehidden=theme_includehidden|tobool,
                                  titles_only=theme_titles_only|tobool) %}
      

      在这种情况下,您可以在 theme.conf 中定义“theme_navigation_depth”:

      [options]
      theme_navigation_depth = 3
      

    更改完成后重新编译...不要忘记享受阳光!

    【讨论】:

      【解决方案3】:

      不幸的是,有一个关于此的公开错误: https://github.com/readthedocs/sphinx_rtd_theme/issues/455

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-21
        • 1970-01-01
        • 2019-05-13
        • 2013-09-28
        • 2019-09-04
        相关资源
        最近更新 更多