Skip to content

LaTeX: longtable class forbids nested table but implicit longtable will allow it, and crash on PDF build #13646

@jfbu

Description

@jfbu

Describe the bug

The following source:

.. table:: Test table
   :widths: auto
   :class: longtable

   =====  =====
     A    not A
   =====  =====
   False  True
   True   +---+
          | h |
          +---+
   =====  =====

is intercepted on build by an error:

    sphinx.writers.latex.UnsupportedError: index:: longtable does not support nesting a table.

If one does not explicitly use the longtable class but trigger it via enough many rows, then no error is triggered by the latex builder. However the nesting of tabulary inside a longtable causes a crash when building the PDF:

! Extra alignment tab has been changed to \cr.
<template> \endtemplate 
                        
l.156 \end{tabulary}
                    
? X

How to Reproduce

Use above source

Environment Information

Platform:              darwin
Python version:        3.13.3 (v3.13.3:6280bb54784, Apr  8 2025, 10:47:54) [Clang 15.0.0 (clang-1500.3.9.4)])
Python implementation: CPython
Sphinx version:        8.3.0+/a5366394a
Docutils version:      0.21.2
Jinja2 version:        3.1.6
Pygments version:      2.19.1

Sphinx extensions

Additional context

It is definitely possible to nest a (simple) tabular inside longtable, and even a (simple) tabulary. For example this is legal LaTeX source:

\begin{longtable}{|c|c|}
\hline
  A&B\\
\hline
  C&D\\
\hline
  E&
    \begin{tabulary}{\linewidth}{|c|c|}
      A&B\\\hline
      C&D\\\hline
    \end{tabulary}
\\\hline
\end{longtable}

and produces (not quite correct, due to horizontal grid lines of sub-table merging with outer table) output.

In the Sphinx case of tabulary inside longtable via enough many rows to trigger it, the PDF though can not be built.

Following possibilities of treatment of this report:

  1. make sure all cases of a longtable do raise an error if a cell contains a table,
  2. force a nested table in a longtable to never use tabulary but only tabular which seems to work with no issue,
  3. understand why the Sphinx LaTeX mark-up for tabulary inside longtable causes a PDF build crash and fix that, so that longtable will only explicitly forbid nesting another longtable.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions