3

I have two tasks I'm trying to accomplish, both of which I found independent solutions for on other threads. However, when the solutions are used in combination they conflict so I'm looking for an approach that is compatible with both

Task 1: use alpha labels for subsections (i.e. A, B, C, etc). This can be accomplished with \renewcommand\thesubsection{\Alph{subsection}} as suggested in @GonzaloMedina's answer

Task 2: automatically add \section* entries to the TOC. This answer by @egreg suggests sticking with plain \section and using \setcounter{secnumdepth}{0} to remove the numbering in the TOC

If I were able to successfullly combine these two tasks (which I haven't), my desired rendering would look like the following

Desired TOC Desired chapter content

However, using @egreg's solution for task 2 causes the A, B, C subsection labels to disappear. I tried another suggestion from the same thread by @MikeRenfro to add \renewcommand{\thesection}{}, but as the author mentions, it leaves unnecessary indents which are not aesthetically pleasing. On a separate thread, @Vser suggests \addcontentsline{toc}{section}{\nameref{sec:intro}}, but in my testing this left a line of pure ... without auto-populating the section name. The manual work to enter each name without the help of \nameref would be too time-consuming and difficult to maintain.

EDIT 7/28/20: In my actual document, I'm using \tcolorbox to help visually define the section headers. For instance, the following MWE along with it's rendering

\documentclass[a4paper,10pt,twocolumn]{book}
\usepackage{cuted}
\usepackage{tcolorbox}
\usepackage[explicit]{titlesec}

\setcounter{tocdepth}{1}
\titlespacing*{\section}{0pt}{0pt}{0pt}
\titleformat{name=\section, numberless}{\setcounter{subsection}{0}\normalfont\Large\bfseries}{}{0pt}{}[\addcontentsline{toc}{section}{#1}#1]
\renewcommand\thesubsection{\Alph{subsection}} % Accomplishes task 1

\begin{document}
    
    \tableofcontents
    \chapter{Chapter}

    \begin{strip}
        \begin{tcolorbox}[title=\section*{An unnumbered section}]
            Other content here..
        \end{tcolorbox}
    \end{strip}
    \subsection{First Subsection}   
    \subsection{Second Subsection}  
    
\end{document} 

tcolor box section header

Originally, I had included \titlespacing*{\section}{0pt}{0pt}{0pt} in the preamble to ensure there was no extra space above, below, or left of the section title text. However, the current solution by @Bernard adds an extra blank row above the section header as seen in the rendering

1 Answer 1

3

You can get what you want with \titleformat, the numberless key and the explicit option from titlesec:

\documentclass{book}
\usepackage[explicit]{titlesec,}

\setcounter{tocdepth}{1}
\titlespacing*{\section}{0pt}{0pt}{0pt}
\titleformat{name=\section, numberless}{\setcounter{subsection}{0}\normalfont\Large\bfseries}{}{0pt}{}[\addcontentsline{toc}{section}{#1}#1]
\renewcommand\thesubsection{\Alph{subsection}} % Accomplishes task 1

\begin{document}

    \tableofcontents
    \chapter{Chapter}
    \section{A numbered section}
        \subsection{Subsection}
    \section*{An unnumbered section}

\end{document} 
15
  • This works nicely on the MWE, however in my actual document I get a bunch of Counter too large. \subsection{The Subsection Title} - any thoughts? Jul 27, 2020 at 2:32
  • There are 26 letters in the alphabet. If you have more than 26 subsections then LaTeX runs out of counters for the subsections. Jul 27, 2020 at 16:30
  • If you have more than 26 sunsections, a solution would use the \alphalph package and redefine \thesubsection as \AlphAlph{subsection}. This way, subsections would be labelled as A, B, … , Z, AA, AB, …
    – Bernard
    Jul 27, 2020 at 16:48
  • @PeterWilson @Bernard Thank you for the pointers. However, I am not exceeding 26 subsections within any given section. The issue appears to be the subsection counter is not reset after each new \section* so section1 has A, B, C and then section2 starts D, E, F. Eventually I run out of letters because there are more than 26 subsections across all sections of the book Jul 27, 2020 at 21:48
  • 1
    @AddisonKlinke: I've modified \titleformat for unnumbered section to reste the subsection counter. Please see if it's OK now.
    – Bernard
    Jul 27, 2020 at 23:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .