1 year ago

#349870

test-img

Muhammad Mohsin Khan

How to show certain stub files created using autosummary in sphinx as subheadings in the TOC?

I am using autosummary in sphinx to create stub files and document them through the index.rst file. I have a module folder and have some Python files in it. I wanted these Python files to be created as a subheading under the module heading in the generated pdf.

This is my directory structure:

optimate
├── doc
|   ├── source
|   |   ├── index.rst
|   |   ├── conf.py
|   |   └── ...
|   └── Makefile
├── optimate
|   ├── core
|   │   ├── __init__.py
|   |   └── optimizer.py
|   └── ...
└── ...

Following is my conf.py:

# -- Path setup --------------------------------------------------------------
import os
import sys
sys.path.insert(0, os.path.abspath('../..'))
sys.path.insert(0, os.path.abspath('../../optimate'))
sys.path.insert(0, os.path.abspath('../../optimate/core'))


# -- Project information -----------------------------------------------------
import optimate
import re

project = 'optimate'
copyright = '2022, Muhammad Mohsin Khan'
author = 'Muhammad Mohsin Khan'

version = re.sub(r'(\d+\.\d+)\.\d+(.*)', r'\1\2', optimate.__version__)
version = re.sub(r'(\.dev\d+).*?$', r'\1', version)
# The full version, including alpha/beta/rc tags.
release = optimate.__version__


# -- General configuration ---------------------------------------------------
extensions = ['sphinx.ext.autodoc',
    'sphinx.ext.autosummary',
    'sphinx.ext.autosectionlabel',
    'sphinx.ext.intersphinx',
    'sphinx.ext.imgmath',
    'sphinx.ext.viewcode',
    'sphinx.ext.napoleon',
    'sphinxcontrib.bibtex',
    'recommonmark'
]

templates_path = ['_templates']

source_suffix = ['.rst', '.md']
# source_suffix = '.rst'

# The root toctree document.
root_doc = 'index'

language = None

exclude_patterns = []

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'

# If true, `todo` and `todoList` produce output, else they produce nothing.
todo_include_todos = False

exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']


# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages.  See the documentation for
# a list of builtin themes.
#
html_theme = 'alabaster'

html_static_path = ['_static']

# This is required for the alabaster theme
# refs: http://alabaster.readthedocs.io/en/latest/installation.html#sidebars
html_sidebars = {
    '**': [
        'localtoc.html',
        'sourcelink.html',
        'relations.html',  # needs 'show_related': True theme option to display
        'searchbox.html'
    ]
}


# -- Options for HTMLHelp output ------------------------------------------

# Output file base name for HTML help builder.
htmlhelp_basename = 'optimatedoc'


# -- Options for LaTeX output ---------------------------------------------
numfig = True
latex_elements = {
    # The paper size ('letterpaper' or 'a4paper').
    #
    'papersize': 'a4paper',

    # The font size ('10pt', '11pt' or '12pt').
    #
    'pointsize': '11pt',

    # Additional stuff for the LaTeX preamble.
    #
    # 'preamble': '',

    # Latex figure (float) alignment
    #
    'figure_align': 'h',

    # Leaves no blank page after every section
    #
    'extraclassoptions': 'openany,oneside'
}

latex_documents = [
    (root_doc, 'index.tex', 'Optimate Documentation',
     'Muhammad Mohsin Khan', 'manual'),
]


# -- Options for manual page output ---------------------------------------

man_pages = [
    (root_doc, 'optimate', 'Optimate Documentation',
     [author], 1)
]


# -- Options for Texinfo output -------------------------------------------

texinfo_documents = [
    (root_doc, 'optimate', 'Optimate Documentation',
     author, 'Optimate', 'One line description of project.',
     'Miscellaneous'),
]
    
    
# Autosummary
autosummary_generate = True

# Example configuration for intersphinx: refer to the Python standard library.
intersphinx_mapping = {'https://docs.python.org/': None}

and following is my index.rst:

Optimate - Automatic parameter optimizer for different material models
======================================================================

.. image:: /images/optimate_logo.jpg
   :align: center
   
About
=====

.. autosummary::
   :toctree: modules

   optimate
   
Modules
=======

.. autosummary::
   :toctree: modules

   optimate.core
   
.. autosummary::
   :toctree: modules/submodules
   
   optimate.core.optimizer


Indices and tables
==================

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`

What I get in the pdf file as Table of Contents (TOC), when I run make latexpdf in the doc/ folder, is:

1 About                                                                    2
1.1 optimate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .   2

2 Modules                                                            
2.1 optimate.core . . . . . . . . . . . . . . . . . . . . . . . . . . . .  3
2.2 optimate.core.optimizer . . . . . . . . . . . . . . . . . . . . . . .  3

What I want as Table of Contents (TOC) in the pdf is:

1 About                                                                    2
1.1 optimate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .   2

2 Modules                                                                        
2.1 optimate.core . . . . . . . . . . . . . . . . . . . . . . . . . . . .  3
    2.1.1 optimate.core.optimizer . . . . . . . . . . . . . . . . . . . .  3

I tried changing the index.rst as:

Modules
=======

.. autosummary::
   :toctree: modules

   optimate.core

Submodules
----------   
.. autosummary::
   :toctree: modules/submodules
   
   optimate.core.optimizer

I also made a custom template with # as an underline and passed it to the autosummary as:

Modules
=======

.. autosummary::
   :toctree: modules

   optimate.core
   
.. autosummary::
   :toctree: modules/submodules
   :template: mytemplate.rst
   
   optimate.core.optimizer

which creates optimate.core.optimizer.rst stub file heading with # as underline but it doesn't seem to work and I still get the above-undesired result. Does anybody know a solution to this? Or propose an alternative way to do this?

python

pdf

latex

python-sphinx

restructuredtext

0 Answers

Your Answer

Accepted video resources