3

I am writing a solutions manual for the problems at the end of chapters in a published book. For this I need unnumbered sections listed in the TOC, plus I need the title of each unnumbered section to be exactly Problem xx.yy where xx is \thechapter and yy is \thesection.

Using the book document class, I managed to do it with this:

\section*{Problem~\thesection}\addcontentsline{toc}{section}{Problem~\thesection}\addtocounter{section}{1}

My question is, how can I define a new sectioning command or redefine \section or \section* so I don't have to rewrite all that for each Problem. I have 100+ Problems!

Here is a MWE

\documentclass[11pt,letterpaper]{book}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\begin{document}
\tableofcontents
\chapter{Introductory Problems}\addtocounter{section}{1}
\section*{Problem~\thesection}\addcontentsline{toc}{section} {Problem~\thesection}\addtocounter{section}{1}
Here is bla bla bla the solution to bla bla bla. 
\end{document}

But may be there is a way to suppress the printing of section numbers in the TOC so that I can use \section{} instead of \section*{}, so there would be no need for the \addtocounter, etc. It would be a cleaner solution. But I have no idea how to suppress the printing of section numbers in the TOC ???

  • Can you try writing a macro that does the job? – Ethan Bolker Sep 25 '14 at 0:16
  • Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. It is a bit difficult to answer this in the abstract since this stuff depends a lot on the document class. Also, do you want to redefine \section* or do you want a new command \psection or whatever? You say either but do you need regular \section* commands? Do you need regular \section commands? – cfr Sep 25 '14 at 0:30
3

Just allocate an own counter for problems and bury in a macro the repetitive tasks

\documentclass[11pt,letterpaper]{book}

\newcommand\problem{%
  \refstepcounter{problem}%
  \section*{Problem \theproblem}%
  \addcontentsline{toc}{section}{Problem \theproblem}%
}
\newcounter{problem}[chapter]
\renewcommand{\theproblem}{\thechapter.\arabic{problem}}

\begin{document}
\tableofcontents

\chapter{Introductory Problems}

\problem
Here is bla bla bla the solution to bla bla bla. 

\problem
Here is bla bla bla the solution to bla bla bla. 

\chapter{Harder Problems}

\problem
Here is bla bla bla the solution to bla bla bla. 

\problem
Here is bla bla bla the solution to bla bla bla. 

\end{document}

enter image description here

1

You just need to redefine how the section counter \thecounter is printed :

\renewcommand\thesection{Problem \arabic{chapter}.\arabic{section}}

If you only do this, however, then the "Problem 1.1" labels collide with the section names in the table of contents. If you do not want the sections displayed in the table of contents then use

\setcounter{tocdepth}{0}

If you do want them then one way to fix the colliding problem is to use the tocloft package:

\documentclass{book}
\usepackage{tocloft}
\setcounter{tocdepth}{2}
\renewcommand\thesection{Problem \arabic{chapter}.\arabic{section}}
\settowidth\cftsecnumwidth{Problem 8.88}
\begin{document}
\tableofcontents
  \chapter{First chapter}
  \section{First problem, chapter one}
  \section{Second problem, chapter one}

  \chapter{Second chapter}
  \section{First problem, chapter two}
  \section{Second problem, chapter two}
\end{document}

Using tocloft the variable \cftsecnumwidth controls the amount of space left for typesetting the section "numbers".

Here is the output:

enter image description here

enter image description here

  • This will affect all sections in the document – egreg Sep 25 '14 at 8:33
  • @egreg Yes, that's my intention. As I read the question, the OP is writing a separate solutions manual for a textbook. As the solutions manual is a different document all sections should be numbered this way. If I have misinterpreted and this is not the case then something different is needed. – Andrew Sep 25 '14 at 8:38
1

You can do that with titlesec/titletoc. Here is a code; I define a problem command, with an optional argument (the problem title, it it has one. You also can have unnumbered sections:

 \documentclass[11pt,letterpaper]{book}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage[explicit]{titlesec}
\usepackage{titletoc}
\usepackage{lipsum}
\titleformat{name=\section,numberless}[hang]{\large\bfseries}{}{0pt}{\addcontentsline{toc}{section}{#1}}

\titleformat{\section}[hang]{\large\bfseries}{Problem~\thesection}{1em}{}
\newcommand\problem[1][]{\section{#1}}

\titlecontents{section}[1.5em]{\smallskip}%
          {Problem~\thecontentslabel~}%numbered
          {}%numberless\
          {\hfill\quad\contentspage}[\smallskip]%

\begin{document}

\tableofcontents

\chapter{Introductory Problems}%

\problem
\lipsum[1-3]
\problem
\lipsum[4-6]
Here is bla bla bla the solution to bla bla bla.

\problem[(the marriage lemma)]
Here is bla bla bla the solution to bla bla bla.


\section*{A numberless section}%
\lipsum[7-9]

\end{document} 

enter image description here

  • Actually it is titleformat{name=\section,numberless}[hang]{\large\bfseries}{label}{0pt}{Problem~}[\addcontentsline{toc}{section}{\sectiontitle}] but I do not know what to put for {label}. If I put {} it does not work. – Echeban Sep 25 '14 at 1:41
  • @Echeban: I know that, but I preferred to avoid the optional argument for someone who probably doesn't know very well titlesec. Do you think there can be some artefact with the way I did it? – Bernard Sep 25 '14 at 1:44

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.