0

I am quite new to LaTeX and am trying to write my own notes using it, but I am having some issues with formatting.

Shown below is a minimal working example of my code:

\documentclass{article}
\usepackage[left = 2.54 cm, right = 2.54 cm, top = 2.54 cm, bottom = 2.54 cm]{geometry}
\usepackage{titling}
\usepackage{array}
\usepackage{multirow}
\usepackage{amsmath}
\usepackage{amssymb}


\setlength{\tabcolsep}{24 pt}
\renewcommand{\arraystretch}{2}

\begin{document}

\tableofcontents

\newpage

\section{Discrete Uniform Distribution \\}

\begin{tabular}{>{\bfseries}l l}
Abbreviation & DiscUnif$(n)$ \\
Type & Discrete \\
Rationale & Equally likely outcomes \\
Sample Space & $[1, n]\ \forall\ n \in \mathbb{Z^+}$ \\
Probability Mass Function & $f(x) = \frac 1 n\ \forall\ x \in \mathbb{Z^+}$ \\
\multirow{2}{*}{Moments} & $E(X) = \frac {n + 1} 2$ \\
& $Var(X) = \frac {n^2 - 1} {12}$ \\
\end{tabular}

\end{document}

However, my Table of Contents comes out like this:

Table of Contents Formatting Error

As can be seen in the image above, my "Discrete Uniform Distribution" section does indeed start on page 2, but I would like the page number to be on the same line as the name of the corresponding section. The other sections that I have in my document which follow all have the same page numbering issue i.e. the related page number for the section appears on the next line.

What is the problem with my code here?

Also, as mentioned, I am very new to LaTeX (only been using it for about a week), so if I have any "bad coding", please feel free to suggest how I may improve :)

7
  • you are forcing a line break with \\ which should never appear at the end of any paragraph and certainly not in a section heading. Apr 18, 2021 at 9:54
  • please always post examples in a form that people can run them and see the issue. You have removed \begin{document} and \end{document} here which forces people to notice that and guess where you intend them to be put back. Apr 18, 2021 at 9:56
  • the spacing after section headings is a document level setting you could copy the line from article.cls and increase it or use a package such as secsty that lets you adjust several parameters for headings Apr 18, 2021 at 9:57
  • \\ does not add any vertical space it is forcing your heading to be a two-line paragraph (and presumably generating a warning that the line is empty, underful) Apr 18, 2021 at 9:59
  • @DavidCarlisle Apologies for not indicating where my document begins and ends. I have edited my post! Also, why do you say that \\ does not add any vertical space? I mean, it adds an empty line, which is technically vertical space right?
    – Ethan Mark
    Apr 18, 2021 at 10:02

1 Answer 1

1

It is always wrong to have \\ at the end of a paragraph (and forcing a line break at the send of a section heading is particularly weird, as you see from your output)

TeX does warn about both the table of contents and the main heading

Underfull \hbox (badness 10000) in paragraph at lines 1--1
Underfull \hbox (badness 10000) in paragraph at lines 19--19

note 10000 is the maximum badness.

as you have double spaced the table, the spacing above may look a little tight so you could correct with \bigskip for example.

\documentclass{article}
\usepackage[left = 2.54 cm, right = 2.54 cm, top = 2.54 cm, bottom = 2.54 cm]{geometry}
\usepackage{titling}
\usepackage{array}
\usepackage{multirow}
\usepackage{amsmath}
\usepackage{amssymb}


\setlength{\tabcolsep}{24 pt}
\renewcommand{\arraystretch}{2}

\begin{document}

\tableofcontents

\newpage

\section{Discrete Uniform Distribution}

\begin{flushleft}
\bigskip
\begin{tabular}{@{}>{\bfseries}l l}
Abbreviation & DiscUnif$(n)$ \\
Type & Discrete \\
Rationale & Equally likely outcomes \\
Sample Space & $[1, n]\ \forall\ n \in \mathbb{Z^+}$ \\
Probability Mass Function & $f(x) = \frac 1 n\ \forall\ x \in \mathbb{Z^+}$ \\
\multirow{2}{*}{Moments} & $E(X) = \frac {n + 1} 2$ \\
& $Var(X) = \frac {n^2 - 1} {12}$ \\
\end{tabular}  
\end{flushleft}


\end{document}

If you want to ajust the spacing after all sections not just this one, you can copy the lines from article

\makeatletter
\renewcommand\section{\@startsection {section}{1}{\z@}%
                                   {-3.5ex \@plus -1ex \@minus -.2ex}%
                                   {2.3ex \@plus.2ex}%<<<
                                   {\normalfont\Large\bfseries}}
\makeatother

and adjust the value from 3.5ex to whatever size you want eg 12pt for the default baseline for the 10pt font. (negate the values to suppress indentation)

Normally do this in teh preamble but here I do it mid document to show the difference, and making it 24pt (twice baseline) to be more visible as there is not much difference between 2.3ex and one baselineskip.

enter image description here

\documentclass{article}

\begin{document}

\section{Discrete Uniform Distribution}

text text text text text text text text text 
text text text text text text text text text 
text text text text text text text text text 
text text text text text text text text text 
text text text text text text text text text 

\section{Something else}

text text text text text text text text text 
text text text text text text text text text 
text text text text text text text text text 
text text text text text text text text text 
text text text text text text text text text 


\makeatletter
\renewcommand\section{\@startsection {section}{1}{\z@}%
                                   {-3.5ex \@plus -1ex \@minus -.2ex}%
                                   {24pt \@plus 2pt}% {2.3ex \@plus.2ex}
                                   {\normalfont\Large\bfseries}}
\makeatother




\section{Discrete Uniform Distribution}

text text text text text text text text text 
text text text text text text text text text 
text text text text text text text text text 
text text text text text text text text text 
text text text text text text text text text 

\section{Something else}

text text text text text text text text text 
text text text text text text text text text 
text text text text text text text text text 
text text text text text text text text text 
text text text text text text text text text 

\end{document}
13
  • Thank you for putting your comments into an answer! I have two questions. Firstly, what is the point of using begin{flushleft} and end{flushleft}? I tried it and noticed it made no difference to the output. Secondly, I noticed you added an @{} after begin{tabular}. Why is this needed and what does it do?
    – Ethan Mark
    Apr 18, 2021 at 10:28
  • @EthanMark flushleft (or center or whatever) is good style around non-loating tables as adjusts the vertical spacing, here actually unseen as it only affects the space below as the space above controlled by the heading. @{} removes the tabcolsep from the left so it is not indented . I also added some notes about general section head spacing Apr 18, 2021 at 10:31
  • the plus and minus components of a TeX dimen are how much the space can stretch and shrink (what the latex book calls a rubber length, or texbook calls glue) @EthanMark Apr 18, 2021 at 10:36
  • @EthanMark I marked the wrong argument, that's the space before, hang on... Apr 18, 2021 at 10:59
  • @EthanMark done Apr 18, 2021 at 11:03

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.