Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

migrated from stackoverflow.com Jul 4 '12 at 1:10

This question came from our site for professional and enthusiast programmers.

    
hmm - could you provide a minimal working example, and explain what you have already tried, for example \vspace*{-5cm} or something else? –  epsilonhalbe Jul 2 '12 at 20:52
    
@epsilon'εⳆ2'halbe yes I tried \vspace*{-5cm} but it didn't work. It only gave me a blank page and the same design as the original (with the big margin) on the following page. –  Dietmar Aumann Jul 2 '12 at 20:59
    
please include such information, what you tried and it's effects in your question. Additionally an example would help - you can add text with \usepackage{lipsum}. –  epsilonhalbe Jul 2 '12 at 21:13
    
Welcome to TeX.sx! Your question was migrated here from Stack Overflow. Please register on this site, too, and make sure that both accounts are associated with each other (by using the same OpenID), otherwise you won't be able to comment on or accept answers or edit your question. –  Werner Jul 4 '12 at 1:21

1 Answer 1

up vote 8 down vote accepted

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

\newcommand\tableofcontents{%
    \if@twocolumn
      \@restonecoltrue\onecolumn
    \else
      \@restonecolfalse
    \fi
    \chapter*{\contentsname
        \@mkboth{%
           \MakeUppercase\contentsname}{\MakeUppercase\contentsname}}%
    \@starttoc{toc}%
    \if@restonecol\twocolumn\fi
    }

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*{50\p@}%
  {\parindent \z@ \raggedright
    \normalfont
    \interlinepenalty\@M
    \Huge \bfseries  #1\par\nobreak
    \vskip 40\p@
  }}

Note the insertion of vertical space (\vspace*{50\p@}) 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*{50\p@}% Remove the vertical space
  {\parindent \z@ \raggedright
    \normalfont
    \interlinepenalty\@M
    \Huge \bfseries  #1\par\nobreak
    \vskip 40\p@
  }}
\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 {50\p@}):

\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
\let\oldtableofcontents\tableofcontents
\renewcommand{\tableofcontents}{\begingroup%
  \patchcmd{\@makeschapterhead}% <cmd>
    {\vspace*{50\p@}}% <search>
    {}% <replace>
    {}{}% <success><failure>
  \oldtableofcontents%
  \endgroup%
}
\makeatother

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

share|improve this answer
    
Werner, I'm surprised this question, and your answer, hasn't got more attention, as I would imagine you helped a lot of people in this situation. I just Googled the issue, and found your solution, and without a tweak, it worked. –  Emanuel Berg Dec 22 '12 at 1:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.