I'm new here, so I hope I'm adhering to any community rules.

I am trying to create a table of contents for some notes I'd like to record on LaTex. Here is my code:

    \documentclass[12pt]{article}

\usepackage[margin=0.90in]{geometry}
\usepackage{amsmath,amsthm,amssymb,scrextend}
\usepackage{fancyhdr}
\usepackage{titlesec}

\makeatletter
\if@openright\cleardoublepage\else\clearpage\fi
\thispagestyle{plain}%
\global\@topnum\z@
\@afterindentfalse
\secdef\@subsection\@ssubsection}
\@startsection {section}{1}{\z@}%
{-3.5ex \@plus -1ex \@minus -.2ex}%
{2.3ex \@plus.2ex}%
{\normalfont\Large\bfseries}}
\makeatother

\begin{document}

% --------------------------------------------------------------
%                         Start here
% --------------------------------------------------------------

\lfoot{}
\cfoot{Page \thepage \hspace{1pt} of 16}
\rfoot{}

% \maketitle
\title{\textbf{\textit{Physics in Medicine -- Revision Notes}}}
\date{}

\begin{titlepage}
\vspace{100 mm}
\maketitle
\doublespacing
\tableofcontents
\end{titlepage}
\vfill

\newpage

\singlespacing
\section*{Section 1: X-ray imaging}
\vspace{2mm}
\newline \noindent \textbf{What are x-rays?}

\vspace{3mm}

\noindent TEXT

\vspace{10mm}

\newline \noindent \textbf{Production of x-rays}
\vspace{8mm}
TEXT

\end{document}


But the result is shown on the picture - horrible big spaces between the content lines. Don't know how to fix this so advice is appreciated :)

• Welcome to TeX.SE! Why do you manually add the contentslines? When you specify sections and subsections the table of contents can be created automatically. Nov 16, 2018 at 13:08
• I haven't. I've added the code I am using in the body of the text. I am already use the \tableofcontents on the front page. I'm trying to link with the above commands, but the result is as shown :( Nov 16, 2018 at 13:09
• It would be better to provide a full (but small) example document, starting with \documentclass and ending with \end{document}, that shows this output. With suuch a document it is easier for the community to reproduce your issue and to suggest solutions. Nov 16, 2018 at 13:12
• You can edit your question with the edit button below the question text, and you can format code within the question but inserting 4 spaces before the code lines, or by selecting the code and pressing the code formatting button (that looks like {}) Nov 16, 2018 at 13:13
• only now it does not compile, because you have taken out too many packages - please try to make it compilable (but still minimal). Nov 16, 2018 at 13:24

The extra space is caused by the redefinition of \section and \subsection. This code contains the line \addtocontents{toc}{\protect\addvspace{20\p@}} (and 5\p@), which adds 20pt and 5pt of vertical space to the table of contents on each use of the \section and subsection commands, respectively.

To remove the extra space, you can remove this part of the redefinition (see MWE below). You can also remove the entire redefinition since it is not necessary, unless you require the additional modifications of the (sub)section title behavior of course.

Some additional space is introduced by \doublespacing (from the setspace package), this can be removed to make the table of contents more compact.

MWE:

\documentclass[12pt]{article}
\usepackage{setspace}

\makeatletter
\renewcommand\subsection{%
\if@openright\cleardoublepage\else\clearpage\fi
\thispagestyle{plain}%
\global\@topnum\z@
\@afterindentfalse
\secdef\@subsection\@ssubsection}
\renewcommand\section{%
\@startsection {section}{1}{\z@}%
{-3.5ex \@plus -1ex \@minus -.2ex}%
{2.3ex \@plus.2ex}%
{\normalfont\Large\bfseries}}
\makeatother

\begin{document}
\title{\textbf{\textit{Physics in Medicine -- Revision Notes}}}
\date{}

\begin{titlepage}
\maketitle
\doublespacing
\tableofcontents
\end{titlepage}