I'm putting together a template for college assignments. In order to systematize the writing, I've reformatted the section/subsection titles in the document so they read "Problem #number" and " Answer #number" respectively, without the need for manual entry.
My problem here is that the TOC doesn't put a title for each section and subsection, as there is no argument passed in the command. See this example:
\documentclass{article}
\usepackage{titlesec}
\titleformat{\section}{\large \bfseries}{Problem \arabic{section}}{2em}{}
\titleformat{\subsection}{\small \bfseries}{Answer \arabic{section}}{1em}{}
\begin{document}
\tableofcontents
\section{}
\subsection{}
\end{document}
Produces:
I understand that I can manually add a title for TOC doing:
\section[title]{}
And that I could define a new command that could do this for every section (and for every subsection), like this
\documentclass{article}
\usepackage{titlesec}
\titleformat{\section}{\large \bfseries}{Problem \arabic{section}}{2em}{}
\newcommand{\problem}[1]{\section[Problem \thesection]{#1}}
\begin{document}
\tableofcontents
\problem{}
\end{document}
That produces
But this shows the section number to the left of the title.
Is there a better way to make all titles in the TOC automatically display as in the document?
titletoc
package, and a\titlecontents
command which eases customisation of the table of contents.