TransWikia.com

Section and subsection TOC title formatting

TeX - LaTeX Asked by eneguev on May 18, 2021

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:

Output of MWE

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

output of MWE2

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?

2 Answers

I was going to suggest using the exercise package, but discovered a bug in the TOC formatting (which I fixed).

One can reformat the header easily enough, if you don't like centering.

documentclass{article}
usepackage{exercise}
ListOfExerciseInToc
ExerciseLevelInToc{section}
defExerciseName{Problem}
defAnswerName{Answer}

makeatletter
renewcommand{@@@ExeEnv}{%
    pagebreak[1]vskipExerciseSkipBefore
    @QuestionLevel1
    refstepExecounter
    begingroup@getExerciseInfoExerciseHeader
    addcontentsline{ext@exercise}{toc@exercise}{ExerciseName
        theExercise expandafter{stringitshape ExerciseTitle}hspace{.66em}}% added string
    endgroupAtBeginExercise}
    
renewcommand{@@@ExeCmd}{%
    ifnum@QuestionLevel=0
      advance @QuestionLevel by 1
      begin{list}{@getExerciseInfoExerciseListHeader}%
{partopsepExepartopsep labelsepExelabelsep itemsep Exesep%
parsepExeparsep topsepExetopsep labelwidthExelabelwidth%
leftmarginExeleftmargin rightmarginExerightmargin}
    else
      termineliste{1}@EndExeBox
    fi
    @selectExercise
    global@Answerfalse@BeginExeBoxrefstepExecounter%
    addcontentsline{ext@exercise}{toc@exercise}{ExerciseName
        theExercise expandafter{stringitshape expandafterExerciseTitle}hspace{.66em}}% added string
    itemignorespacesAtBeginExercise
}
makeatother

begin{document}
tableofcontents
bigskiphrule

section{Normal section}

begin{Exercise}
  Problem text here.
end{Exercise}
begin{Answer}
  Answer text here.
end{Answer}

end{document}

Answered by John Kormylo on May 18, 2021

Requires a small modification of the entry of the TOC from section to suppress the section number.

b

documentclass{article}
usepackage{titlesec}

titleformat{section}{large bfseries}{Problem thesection}{2em}{}

newcommand{problem}[1]{%
    section[Problem thesection]{#1}
}

%% from https://tex.stackexchange.com/a/218675/161015
makeatletter
letlatexl@sectionl@section
defl@section#1#2{begingroupletnumberline@gobblelatexl@section{#1}{#2}endgroup}
makeatother

begin{document}
    tableofcontents

    problem{One}
    
    problem{Two}
    
    problem{Three}
    
end{document}

Answered by Simon Dispa on May 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