TransWikia.com

How to remove top margin above tableofcontents

TeX - LaTeX Asked by Dietmar Aumann on April 19, 2021

My contents page would fit on one side, if LateX wouldn’t force an automatic whitespace block on top of the table of contents.

Is there any way to remove this block and have tableofcontents start at the top of the page?

3 Answers

In the standard document classes (like book and report), tableofcontents is set as a chapter*:

newcommandtableofcontents{%
    if@twocolumn
      @restonecoltrueonecolumn
    else
      @restonecolfalse
    fi
    chapter*{contentsname
        @mkboth{%
           MakeUppercasecontentsname}{MakeUppercasecontentsname}}%
    @starttoc{toc}%
    if@restonecoltwocolumnfi
    }

So, it would be possible to temporarily modify the chapter heading macro to not insert as much vertical space. Here's a look at the chapter* heading macro @makeschapterhead:

def@makeschapterhead#1{%
  vspace*{50p@}%
  {parindent z@ raggedright
    normalfont
    interlinepenalty@M
    Huge bfseries  #1parnobreak
    vskip 40p@
  }}

Note the insertion of vertical space (vspace*{50p@}) before setting the heading. So, we can temporarily redefine this macro to not insert the vertical space:

enter image description here

documentclass{book}
usepackage{showframe}% http://ctan.org/pkg/showframe
begin{document}

begingroup
makeatletter
% Redefine the chapter* header macro to remove vertical space
def@makeschapterhead#1{%
  %vspace*{50p@}% Remove the vertical space
  {parindent z@ raggedright
    normalfont
    interlinepenalty@M
    Huge bfseries  #1parnobreak
    vskip 40p@
  }}
makeatother

tableofcontents
endgroup
chapter{A chapter}section{A section}subsection{A subsection}
chapter{A chapter}section{A section}subsection{A subsection}
chapter{A chapter}section{A section}subsection{A subsection}
chapter{A chapter}section{A section}subsection{A subsection}
chapter{A chapter}section{A section}subsection{A subsection}
chapter{A chapter}section{A section}subsection{A subsection}
end{document}

The grouping of the redefinition makes it local. Therefore, all modifications are restored after endgroup.

Since the chapter* header macro only uses vspace*{..} to insert the gap between the text block and chapter header, you could also redefine vspace to gobble the two arguments (* and {50p@}):

documentclass{book}
usepackage{showframe}% http://ctan.org/pkg/showframe
begin{document}
begingroup
renewcommand{vspace}[2]{}% Gobble 2 arguments after vspace
tableofcontents
endgroup
chapter{A chapter}section{A section}subsection{A subsection}
chapter{A chapter}section{A section}subsection{A subsection}
chapter{A chapter}section{A section}subsection{A subsection}
chapter{A chapter}section{A section}subsection{A subsection}
chapter{A chapter}section{A section}subsection{A subsection}
chapter{A chapter}section{A section}subsection{A subsection}
end{document}

The showframe package highlights the text block boundary (in addition to other elements) and is therefore only used in this example to showcase the vertical alignment of the table of contents. It is not needed in your final document.


Perhaps a cleaner approach would be to use etoolbox to patch @makeschapterhead. This would also allow you to separate document structure from content (making what resides in the document environment only relate to the content). Add the following to your document preamble:

usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
makeatletter
letoldtableofcontentstableofcontents
renewcommand{tableofcontents}{begingroup%
  patchcmd{@makeschapterhead}% <cmd>
    {vspace*{50p@}}% <search>
    {}% <replace>
    {}{}% <success><failure>
  oldtableofcontents%
  endgroup%
}
makeatother

The above does a local search-and-replace within @makeschapterhead (replacing vspace*{50p@} with nothing, before calling the regular table of contents. The redefinition (search-and-replace) is localized within the scope of the group.

Correct answer by Werner on April 19, 2021

Here's a tocloft way that has not been mentioned so far:

tocloft provides the cftbeforeXtitleskip and cftafterXtitleskip lengths, where X stands for toc, lof or lot, depending on the specific needs.

The advantage of using tocloft is that it hides the specific class settings, however, don't use tocloft with a KOMA class -- that's not recommended.

The values in the given example are just arbitrary!

documentclass{book}

usepackage{tocloft}
usepackage{blindtext}

setlength{cftbeforetoctitleskip}{0pt}
setlength{cftaftertoctitleskip}{0pt}

begin{document}
tableofcontents
blinddocument[2]

end{document}

Answered by user31729 on April 19, 2021

I use this:

renewcommand{contentsname}{vspace{-1cm} hfillbfseriesLARGE TABLE OF CONTENTS hfill vspace{0.2cm}}

The values inside the 1st/2nd vspace{} control the vertical spaces before/after 'TABLE OF CONTENTS'.

Remember to put the command inside begin{document} ... end{document}.

Answered by DianChao Lin on April 19, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP