Search code examples
latexpython-sphinxxelatextoctree

How to achieve a grouping with part or chapter in latexpdf to represent the :caption: option in a toctree?


I have a Sphinx documentation project:

$ tree
.
├── _build
├── conf.py
├── index.rst
├── Makefile
└── src
    ├── part1.rst
    └── part2.rst
$

with this content:

$  cat src/part1.rst 
Rome
-----
Elit provident eum amet in illum Asperiores fugiat voluptatum iste amet nisi!

Milan
-----
- a
- b

(doc) luis@spinoza:~/data/sphinx_toc$ cat src/part2.rst 
Paris
------

Dolor ipsa qui beatae officia nesciunt? Similique quia corporis quaerat eligendi itaque?

Lyon
-----
- a 
- b

$

I'm using :caption to group things on HTML left menu

$ cat index.rst 
Documentation
==============

.. toctree::
   :maxdepth: 2
   :caption: France

   ./src/part1.rst

.. toctree::
   :maxdepth: 2
   :caption: Italy

   ./src/part2.rst
$

On latexpdf I get 4 sections at the same level

$ grep "section{" _build/latex/test_toc.tex 
\section{Rome}
\section{Milan}
\section{Paris}
\section{Lyon}

I'm looking for a way to keep the same I have on HTML but to group with part or chapter countries Italy and France:

Note: I'm using howto and xelatex:

$ grep latex conf.py 
latex_documents = [('index', 'test_toc.tex', 'Test TOC', 'me', 'howto')]
$

Solution

  • I found this, kind of, solution, not very elegant.

    $ cat index.rst 
    Documentation
    ==============
    
    .. only:: latex
    
      .. raw:: latex
    
         \part{France}
    
    .. toctree::
       :maxdepth: 2
       :caption: France
    
       ./src/part1.rst
    
    .. only:: latex
    
      .. raw:: latex
    
        \part{Italy}
    
    .. toctree::
       :maxdepth: 2
       :caption: Italy
    
       ./src/part2.rst
    $
    

    In my real document I have 22 captions that will add 22x4_lines, not very compact.

    I hope someone will have a better idea.

    $ grep -E 'section{|part{' _build/latex/test_toc.tex 
    \part{France}
    \section{Rome}
    \section{Milan}
    \part{Italy}
    \section{Paris}
    \section{Lyon}
    $