2

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 Answers1

2

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}

Bernard
  • 271,350
  • 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? – Addison Klinke Jul 27 '20 at 02:32
  • There are 26 letters in the alphabet. If you have more than 26 subsections then LaTeX runs out of counters for the subsections. – Peter Wilson Jul 27 '20 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 '20 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 – Addison Klinke Jul 27 '20 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 '20 at 23:03
  • @Bernard Perfect! One last thing - I edited my question to show how I'm trying to format spacing around the \tcolorbox section titles. When I apply your solution, I get an extra blank line above each section title. I tried adding additional 0pt parameters into your \titleformat function to achieve the same formatting I had before, but didn't have any luck – Addison Klinke Jul 28 '20 at 14:02
  • @AddisonKlinke: Could you please post a small complete code that we can work with instantly? – Bernard Jul 28 '20 at 14:08
  • @Bernard Yes, sorry for not including that earlier. I updated the MWE now – Addison Klinke Jul 28 '20 at 15:55
  • Thank you your addition. The problem seems to be linked with ‘other contents’. Formatting a section is not designed to add freely text to the title itself. What kind of thing is it supposed to be? – Bernard Jul 28 '20 at 16:11
  • The section title still has a blank line even when I remove the "Other content here..." line. Is that what you meant? – Addison Klinke Jul 28 '20 at 22:11
  • Yes, but to try finding a solution, I have to understand how it is semantically linked to the title. A normal \section has for sole argument its title. – Bernard Jul 28 '20 at 22:34
  • Ah it is not meant to be an additional argument to \section. I'm writing a guidebook, so the section title is the name of the area, and then "other content" would be details such as trail mileage, shade conditions, foliage, etc. Does that help? – Addison Klinke Jul 28 '20 at 22:48
  • I'll try to see tomorrow if I can find a solution (it's getting late here), but as I see it, it means, more or less, the beginning of the section body should be in the \tcolorbox. – Bernard Jul 28 '20 at 23:01
  • Any luck? The section body should start after the darker-colored header of the \tcolorbox, inside the light-colored portion – Addison Klinke Aug 03 '20 at 23:54