I have a table of contents that is 1 page+1 line long. I would like to squeeze it into one page only.

I know I could add a line to the page (although how?) or to reduce the font size or the space between lines but I would prefer a more general solution. Something I will be able to use also in the future if, say, I will have a situation of 1page+2lines.

Below is what I get now:

## EDIT

The question was put on hold. I am not searching for any of the solutions in the suggested link, what I am searching for is a dynamical way of fixing ToC in one page. The accepted solution in the link reduces the vertical space between chapter ToC entries from 1.0em to 0.8em. And it works for one line. I am searching for a general way of adjusting ToC in one page, that will work without me hard coding the vertical space; that is, have TeX calculate the necessary space to shrink by so that the contents are flush with the top and bottom margins.

-
Have you tried \enlargethispage{} for the ToC? –  Kurt Aug 14 '13 at 14:48
So your "dynamic solution" should even work when you have two (or more) full pages of ToC that you want to shrink into one? The ToC (most front matter) should be something you want to worry about at the end of a document project, and therefore might be something to deal with manually anyway. –  Werner Aug 15 '13 at 12:39
If you have two pages plus one line it could fix it to just two pages, that would be smart! It is more and more sounding like a cpp project for the complexity and the logic behind it. –  leoredi Aug 15 '13 at 15:48
@leoredi I've added an automated solution in my answer... –  karlkoeller Aug 16 '13 at 6:36

## Manual method

There are many methods to do such things...

Anyway, in your case I think using the spacing environment provided by the setspace package is the best solution.

MWE:

\documentclass{article}
\usepackage{setspace}

\begin{document}

\begin{spacing}{0.85}
\tableofcontents
\end{spacing}

\clearpage

\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text

\end{document}


Result (the whole TOC in the same page):

You can adjust the value 0.85 to your needs and further adjust it when you add more stuff in the TOC.

## Automated method

This is my attempt to obtain an automated solution.

We will make use of the packages calc and fp for calculations.

First, we calculate the height of \tableofcontents through the command \totalheightof (calc package):

\newlength{\mylength}
\setlength{\mylength}{\totalheightof{\parbox{\linewidth}{%
{\expandafter\def\csname @starttoc\endcsname#1{\InputIfFileExists{\jobname.#1}{}{}}\tableofcontents}%
}}}


The line

{\expandafter\def\csname @starttoc\endcsname#1{\InputIfFileExists{\jobname.#1}


is needed otherwise the ToC won't be printed (see this answer of David Carlisle).

Now we make some calculations to obtain the right ratio to be passed to the spacing environment (\makeatletter ... \makeatother is needed):

\FPdiv\mycalca{\strip@pt\textheight}{\strip@pt\mylength}
\FPsub\mycalcb{1}{\mycalca}
\FPmul\mycalcc{\mycalcb}{1.73}
\FPsub\mycalcd{1}{\mycalcc}


At this point we check if the height of the ToC is greater then the height of the text in the page: if yes, then the ratio is the one we've just calculated, 1 otherwise:

\ifnum\mylength>\textheight
\let\myratio\mycalcd
\else
\FPset\myratio{1}
\fi


This is a complete MWE:

\documentclass{article}
\usepackage{setspace}
\usepackage{calc}
\usepackage{fp}

\makeatletter

\newlength{\mylength}
\setlength{\mylength}{\totalheightof{\parbox{\linewidth}{%
{\expandafter\def\csname @starttoc\endcsname#1{\InputIfFileExists{\jobname.#1}{}{}}\tableofcontents}%
}}}

\FPdiv\mycalca{\strip@pt\textheight}{\strip@pt\mylength}
\FPsub\mycalcb{1}{\mycalca}
\FPmul\mycalcc{\mycalcb}{1.73}
\FPsub\mycalcd{1}{\mycalcc}

\ifnum\mylength>\textheight
\let\myratio\mycalcd
\else
\FPset\myratio{1}
\fi

\makeatother

\begin{document}

\begin{spacing}{\myratio}
\tableofcontents
\end{spacing}

\clearpage

\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text
\section{test}
Some Text

\end{document}


Note that the algorithm is able to insert up to 7 lines that exceed the height of the text. It is not possible to go over...

-
A decent working solution, but I'd agree with OP that this isn't really in 'the spirit of TeX.' For this, you have to manually fiddle with the spacing value to get it to 'look right.' That is, it turns your paper into a drawing canvas rather than, well, a paper. –  Sean Allred Aug 15 '13 at 12:48
@SeanAllred, but on the other hand, fine tuning the TOC could be considered to fall into the category of hand-fettling that may be done in the last stages of document production (along with any tweaks to awkward hyphenation, for example). –  Chris H Aug 15 '13 at 13:30
@ChrisH With a system like TeX, I don't see such hand-fettling to ever be necessary on the document level. –  Sean Allred Aug 15 '13 at 20:19
@SeanAllred You can always provide a better answer... –  karlkoeller Aug 15 '13 at 20:22
@karlkoeller I would love to know one. :-) I'm not saying this answer doesn't fix the problem at hand, but I concerned that it isn't addressing the spirit of the question. This is a good answer for those who want a quick fix. –  Sean Allred Aug 15 '13 at 20:23

By adding some shrinking glue to the baseline skip the following will give you up 44 lines of the contents on one page. The standard is 24, the example is with 30, but uncomment the last line to see the 44.

\documentclass{article}

\begin{document}

{\setlength{\baselineskip}{\dimexpr\baselineskip minus 10pt}
\tableofcontents}

\newpage
\newcommand{\tsec}{\section{Test}Some text.\par}
\newcommand{\tsecs}{\tsec\tsec\tsec\tsec\tsec}
\newcommand{\tsess}{\tsecs\tsecs\tsecs}

\tsess\tsess
%Uncomment to get 44 sections
%\tsecs\tsecs\tsec\tsec\tsec\tsec
\end{document}


I am working under your assumption that titles are short. If you have more than 44 sections, then the contents spills over several pages.

-
 \section[short title]{This is a rather long and descriptive title, which may well wrap onto multiple lines (of course it may not, but that's up to you)}