Search code examples
latexpython-sphinxrestructuredtextmyst

Sphinx: Hide `{contents}` in LaTeX build


index.rst

======================================
MyTitle
======================================
.. raw:: latex

   \section*{MyTitle}

Some text

.. toctree::
      :hidden:
      :titlesonly:
      :caption: Setup

   setup_macos

setup_macos.md

```{contents} On this page:
```

# macOS setup

In this tutorial...

The question is about the {contents} tag in setup_macos.md.

This causes a "local" TOC to be generated. This is desirable in the HTML docs, but I do not want it in the latexpdf docs.

How can I put a local TOC in HTML pages, but hide it from LaTeX output?


Solution

  • Nest the contents directive in an only directive. Use backticks as described here: https://myst-parser.readthedocs.io/en/latest/syntax/roles-and-directives.html#nesting-directives.

    ````{only} html
    ```{contents} On this page:
    ```
    ````