0

I’m using titlesec to define a custom subsubsection heading, by adapting the answer to this question.

I would like \mysubsubheading to coexist with the existing \subsubsection, as an alternative style. But now when I use \subsubheading, the subsubsection numbering no longer appears as part of the formatting:

enter image description here

How do I define a new subsubsection style which doesn’t interfere with the existing one? I don’t need the two subsubsection styles to be usable within the same subsection simultaneously; rather, within a given subsection, I want to be able to choose betweeen using \subsubsection and \mysubsubsection.

\documentclass{article}
\usepackage{fontspec}
\usepackage{titlesec}

\newcounter{mysubsubsection}
\titleclass{\mysubsubsection}{straight}[\subsection]
\titleformat{\mysubsubsection}
  {\normalfont\bfseries}{WP \themysubsubsection:~}{0em}{}
\titlespacing*{\mysubsubsection}{0pt}{*3.25}{*1.5}
\counterwithin{subsection}{mysubsubsection}
\renewcommand{\themysubsubsection}{\arabic{mysubsubsection}}

\begin{document}
\subsection{subsection}
\subsubsection{subsubsection}
\mysubsubsection{My subsubsection}
\end{document}
Roly
  • 4,221
  • 4
  • 26
  • 56
  • 1
    Do you require the counters of subsubsection and mysubsubsection be distinct? – Bernard Jan 05 '20 at 10:59
  • I think they can probably share a counter, since I don’t need them to cohabit the same subsection (and in the event that they did, it would be reasonable for them to share). – Roly Jan 05 '20 at 11:01

2 Answers2

1

Here is a solution using a boolean (prefix) in \titleformat. Note, however, that the prefix is not incorporated in the table of contents (I think it could be with titletoc. Not tested, though).

\documentclass{article}
\usepackage{fontspec}
\usepackage[toctitles]{titlesec}
\usepackage{etoolbox}

\newbool{prefix}

\titleformat{\subsubsection}
  {\normalfont\bfseries}{\ifbool{prefix}{WP~\thesubsubsection\,:}{\thesubsubsection}}{0.5em}{}
\titlespacing*{\mysubsubsection}{0pt}{*3.25}{*1.5}

\newcommand{\mysubsubsection}[2][]{\booltrue{prefix}\subsubsection[#1]{#2}\boolfalse{prefix}}

\begin{document}

\tableofcontents
\subsection{Subsection}
\subsubsection{A normal subsubsection}
Some text.
\mysubsubsection{My subsubsection}
Some more text.
\subsubsection{Subsubsection}
Some text again.
\mysubsubsection{Another subsubsection}
Still some more text.

\end{document} 

enter image description here

Bernard
  • 271,350
1

I'd just put the styles into macros which define them and then use those macros to toggle the style of the \subsubsection and won't use a custom \mysubsubsection macro:

\documentclass{article}
\usepackage{titlesec}

\newcommand*\subsubsectionstyleA
  {%
    \titleformat\subsubsection
      {\normalfont\bfseries}
      {WP \thesubsubsection:~}
      {0pt}
      {}%
    \titlespacing*\subsubsection{0pt}{*3.25}{*1.5}%
    \renewcommand*\thesubsubsection{\arabic{subsubsection}}%
  }
\newcommand*\subsubsectionstyleB
  {%
    \titleformat\subsubsection
      {\normalfont\normalsize\bfseries}
      {\thesubsubsection}
      {1em}
      {}%
    \titlespacing*\subsubsection{0pt}{*3.25}{*1.5}%
    \renewcommand*\thesubsubsection{\thesubsection.\arabic{subsubsection}}%
  }


\begin{document}
\section{section}
\subsection{subsection}
\subsubsectionstyleA
\subsubsection{subsubsection}
\subsubsection{subsubsection}

\subsection{subsection}
\subsubsectionstyleB
\subsubsection{subsubsection}
\subsubsection{subsubsection}
\end{document}

enter image description here

This way the subsubsection counts of the ones using styleA (your custom one) would just be displayed as 1, 2, etc. in the ToC. If you want the WP to also be included in the ToC, you could use the similar:

\documentclass{article}
\usepackage{titlesec}

\newcommand*\subsubsectionstyleA
  {%
    \titleformat\subsubsection
      {\normalfont\bfseries}
      {\thesubsubsection:~}
      {0pt}
      {}%
    \titlespacing*\subsubsection{0pt}{*3.25}{*1.5}%
    \renewcommand*\thesubsubsection{WP~\arabic{subsubsection}}%
  }
\newcommand*\subsubsectionstyleB
  {%
    \titleformat\subsubsection
      {\normalfont\normalsize\bfseries}
      {\thesubsubsection}
      {1em}
      {}%
    \titlespacing*\subsubsection{0pt}{*3.25}{*1.5}%
    \renewcommand*\thesubsubsection{\thesubsection.\arabic{subsubsection}}%
  }


\begin{document}
\tableofcontents
\section{section}
\subsection{subsection}
\subsubsectionstyleA
\subsubsection{subsubsection}
\subsubsection{subsubsection}

\subsection{subsection}
\subsubsectionstyleB
\subsubsection{subsubsection}
\subsubsection{subsubsection}
\end{document}

enter image description here

Skillmon
  • 60,462
  • Thanks, and also to @Bernard for the useful answers. I prefer this one because it generalises more easily to more than two styles. I also need this to work with section numbers placed (right-justified) in the left margin; I’ll ask a separate question for that, since I’m not sure how to do that with titlesec. – Roly Jan 05 '20 at 12:58