2

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
\renewcommand\subsection{\addtocontents{toc}{\protect\addvspace{5\p@}}%
  \if@openright\cleardoublepage\else\clearpage\fi
  \thispagestyle{plain}%
  \global\@topnum\z@
  \@afterindentfalse
  \secdef\@subsection\@ssubsection}
\renewcommand\section{\addtocontents{toc}{\protect\addvspace{20\p@}}%
  \@startsection {section}{1}{\z@}%
  {-3.5ex \@plus -1ex \@minus -.2ex}%
  {2.3ex \@plus.2ex}%
  {\normalfont\Large\bfseries}}
\makeatother


    \begin{document}

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

\lhead{Physics in Medicine}
\chead{}
\rhead{Revision Notes}

\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
\addcontentsline{toc}{section}{Section 1: X-ray imaging}
\addcontentsline{toc}{subsection}{What are x-rays?}
\section*{Section 1: X-ray imaging}
\vspace{2mm} 
\newline \noindent \textbf{What are x-rays?}

\vspace{3mm} 

\noindent TEXT

\vspace{10mm}
\addcontentsline{toc}{subsection}{Production of x-rays}

\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 :)

enter image description here

11
  • 1
    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.
    – Marijn
    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.
    – Marijn
    Nov 16, 2018 at 13:12
  • 1
    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 {})
    – Marijn
    Nov 16, 2018 at 13:13
  • 1
    only now it does not compile, because you have taken out too many packages - please try to make it compilable (but still minimal).
    – Marijn
    Nov 16, 2018 at 13:24

1 Answer 1

2

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{\addtocontents{toc}{\protect\addvspace{5\p@}}%
\renewcommand\subsection{%
  \if@openright\cleardoublepage\else\clearpage\fi
  \thispagestyle{plain}%
  \global\@topnum\z@
  \@afterindentfalse
  \secdef\@subsection\@ssubsection}
%\renewcommand\section{\addtocontents{toc}{\protect\addvspace{20\p@}}%
\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}

\addcontentsline{toc}{section}{Section 1: X-ray imaging}
\addcontentsline{toc}{subsection}{What are x-rays?}
\section*{Section 1: X-ray imaging}

\addcontentsline{toc}{subsection}{Production of x-rays}

\end{document}

Result:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.