TransWikia.com

Table of contents when there are no chapters/sections

TeX - LaTeX Asked by Ali Baig on December 5, 2020

I am using "article" class to write a report. Instead of using conventional section and subsections, I have defined new commands which I want to use in table of contents.

newcommand{CentreHeading}[1]{begin{center} begin{large} MakeUppercase{textbf{#1}} end{large} end{center} vspace*{28pt} addcontentsline{toc}{section}{#1}}

Now, instead of using section command, I want to use CentreHeading command to define sections and their arguments to appear in the table of contents.

A minimal MWE is given below:

documentclass{article}
usepackage{titlecaps}

newcommand{CentreHeading}[1]{begin{center} begin{large} MakeUppercase{textbf{#1}} end{large} end{center} vspace*{28pt} addcontentsline{toc}{chapter}{#1}}

begin{document}
    tableofcontents
    newpage
    
    CentreHeading{First Section}
    Foo bar boo bar
    
    CentreHeading{Second Section}
    Again foo bar  boo bar
end{document}

One Answer

You are using the article class which knows nothing about chapters.

% tocentryprob.tex  SE 570050

documentclass{article}
usepackage{titlecaps}

newcommand{CentreHeading}[1]{begin{center} begin{large}
  MakeUppercase{textbf{#1}} end{large} end{center} vspace*{28pt}
  %%% addcontentsline{toc}{chapter}{#1}}  % not that but this
  addcontentsline{toc}{section}{#1}}

begin{document}
    tableofcontents
    newpage
    
    CentreHeading{First Section}
    Foo bar boo bar
    
    CentreHeading{Second Section}
    Again foo bar  boo bar
end{document}

In the addcontentsline change chapter to section.

Correct answer by Peter Wilson on December 5, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP