TransWikia.com

Centred "Contents" in TOC in twocolumn document with custom sections

TeX - LaTeX Asked on March 18, 2021

I want to adjust the TOC for my twocolumn document. I want to have the word “Contents” at the top and centre of the page, and the toc entries following in two columns afterwards.

I found this which shows me how to do this; but

1) it conflicts with my use of the titletoc and titlesec packages. This is because the TOC seems itself to be a section.

2) it is overly specific in formatting of the toc entries.
I would like to format the entries themselves with titletoc as follows

titlecontents{section} [0pt] { } {thecontentslabel.enspace} { } {leadershbox to 1em{hss.hss}hfill thecontentspage} []

How can I get my formatting of the sections and toc entries, as well as centering the word “Contents” in the TOC of my doc?

The result so far is this
Imgur

The MWE is:

documentclass[12pt,oneside,twocolumn]{article}
usepackage[a4paper, bottom=2cm, columnsep=2.0cm,top=2cm, left=1in, right=1cm ] {geometry}
usepackage{titlesec}
usepackage[]{titletoc}
titlespacing{section}{0pt}{4ex}{-0.5parskip}
titleformat{section}[hang]{raggedrightnormalfontlargebfseries}{thesection.}{1ex}{}[]

makeatletter
renewcommand*{tableofcontents}{%
  if@twocolumn
    @restonecolfalse
  else
    @restonecoltrue
  fi
  twocolumn[section*{centeringcontentsname}]%
  @mkboth{MakeUppercasecontentsname}%
          {MakeUppercasecontentsname}%
  thispagestyle{plain}%
  @starttoc{toc}%
  if@restonecol
    onecolumn
  else
    clearpage
  fi
}
makeatother
begin{document}
tableofcontents
newpage

section{Section 1}
section{Section 2}
section{Section 3}
section{Section 4}
section{Section 5}
section{Section 6}
section{Section 7}
section{Section 8}
section{Section 9}
section{Section 10}
section{Section 11}
section{Section 12}
section{Section 13}
section{Section 14}
section{Section 15}
section{Section 16}
section{Section 17}
section{Section 18}
section{Section 19}
section{Section 20}
section{Section 21}
section{Section 22}
section{Section 23}
section{Section 24}
section{Section 25}
section{Section 26}
section{Section 27}
section{Section 28}
section{Section 29}

end{document}

One Answer

Perhaps not as elegant as what you are trying, but:

Since the table of contents is only going to appear once, it's easy enough to remove the header and add a new one back in 'by hand'. Also, if you are mixing one-column and two-column elements, the multicol package is a little more flexible than setting the entire document to twocolumn.

EDIT: My original solution didn't work with titlesec, I think I was using the section* macro in a way that was incompatible with that package. This seems to work now, with a manually formatted toc heading.

documentclass{article}
usepackage{multicol}

usepackage{titlesec}
titlespacing{section}{0pt}{4ex}{-0.5parskip}
titleformat{section}[hang]{raggedrightnormalfontlargebfseries}{thesection.}{1ex}{}[]

usepackage[]{titletoc}
titlecontents{section} [0pt] { } {thecontentslabel.enspace} { } {leadershbox to 1em{hss.hss}hfill  thecontentspage} []

makeatletter
renewcommandtableofcontents{%
@starttoc{toc}%
}
makeatother
makeatletter
renewcommandtableofcontents{%
    @starttoc{toc}%
}
makeatother

begin{document}

begin{center}
Large
textbf{Contents}  
end{center}

begin{multicols}{2}

tableofcontents
newpage

section{Section 1}
section{Section 2}
section{Section 3}
section{Section 4}
section{Section 5}
section{Section 6}
section{Section 7}
section{Section 8}
section{Section 9}
section{Section 10}
section{Section 11}
section{Section 12}
section{Section 13}
section{Section 14}
section{Section 15}
section{Section 16}
section{Section 17}
section{Section 18}
section{Section 19}
section{Section 20}
section{Section 21}
section{Section 22}
section{Section 23}
section{Section 24}
section{Section 25}
section{Section 26}
section{Section 27}
section{Section 28}
section{Section 29}
end{multicols}


end{document}

LaTeX output

Answered by Tyler on March 18, 2021

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