djhoese
djhoese

Reputation: 3667

Remove whitespace at top of sphinx pdflatex table of contents

I'm building my python package documentation as HTML and as a latex PDF. The default latex pdf generated (manual class) has a large amount of white space at the top of the table of contents around the text "CONTENTS". I'm not super familiar with latex so when I've look at the generated .tex file I don't see anything that tells me how to remove the whitespace.

I've searched around and couldn't find a latex solution that worked. I also tried setting the :caption: on the toctree to an empty string, but that actually removes the entire TOC and all of my content.

Can anyone help me with this?

whitespace example

Upvotes: 2

Views: 780

Answers (1)

user4184837
user4184837

Reputation:

The default behaviour of Sphinx for English language is to use Bjarne option to LaTeX package fncychap for chapter headings. But it also loads package titlesec for generally speaking title headings. It does not make a special chapter definition with titlesec, which simply gather the fncychap definition and wraps it in its own hooks. Anyway, making the story short we find

\ttl@save@mkschap #1->\vspace *{50\p@ }{\parindent \z@ \raggedright \normalfont \interlinepenalty \@M \DOTIS {#1} \vskip 40\p@ }

in a log trace and this is the fncychap definition of \@makeschapterhead as preserved by titlesec in its own macro \ttl@save@mkschap.

fncychap is loaded before sphinx.sty, there is no hook,

edit: in fact the 'fncychap' key whose default value is '\\usepackage[Bjarne]{fncychap}' could serve to add some code to redefine the fncychap setting for un-numbered chapter titles. It is not that different from the approach with 'preamble' key below, except that one would not have needed knowing about titlesec intervention in all this.

but since recent Sphinx 1.5 you can use your own Jinja template for latex content. From the look of your contents which is small, I think you have an older version of Sphinx thus I will go for the LaTeX hacking variant something like this:

latex_elements = {
    # The paper size ('letterpaper' or 'a4paper').
    #
    # 'papersize': 'letterpaper',

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

    # Additional stuff for the LaTeX preamble.
    #
    'preamble': r"""
\makeatletter
\def\ttl@save@mkschap #1{\vspace *{10\p@ }{\parindent \z@ \raggedright
    \color{blue}%
    \normalfont \interlinepenalty \@M \DOTIS {#1} \vskip 10\p@ }}
\makeatother
""",

    # Latex figure (float) alignment
    #
    # 'figure_align': 'htbp',
}

I have added a \color{blue} in there for demonstration purposes only, and modified the \vspace and \vskip commands which is what you need.

The image shows however that there is some extra source of vertical space between Contents and the TOC contents (it remains even with \vskip 0\p@ but one can do \vskip -40\p@ ...), but I think you are after the top space above Contents and already using only \vspace*{10pt} reduced it a lot (not visible in screenshot below).

enter image description here

Upvotes: 1

Related Questions