2

The following code is derived from egreg's answer in Centering an Arabic Section Number Above Justified Title Text

\documentclass{book}

\usepackage{titlesec} \titleformat{\section}[display] {\sffamily\Large\bfseries} {\filcenter\thesection} {1.25ex} {\justifycenter} \newcommand{\justifycenter}[1]{% \leftskip=0pt plus 1fil \rightskip=0pt plus -1fil \parfillskip=0pt plus 2fil #1% } \renewcommand{\thesection}{\arabic{section}}

\begin{document} \large \thispagestyle{empty}

\tableofcontents

\vspace*{25pt}

\section{Here is a Section Title on Which Centers the Number Above the Title Text and Justifies the Text as Well. However, I Do Not Want the Section Entry to be Entered into the TOC Automatically. How May I Turn It Off?} \addcontentsline{toc}{section}{1. } \end{document}

which produces the output:

enter image description here

As far as I can tell, titlesec is automatically producing a section entry in the TOC.

QUESTION: How may I turn this off, so that such entries are manually produced, with, say, \addcontentsline{toc}{section}{1. }? I compile the code with lualatex.

Thank you.

DDS
  • 8,816
  • 4
  • 9
  • 36
  • 1
    Do you really want to do this manually or is the problem that you don't want very long titles in the TOC? If it's the latter, you can use the optional argument of the sectioning commands to specify what goes into the TOC, e.g. \section[Short title]{Very long title}. – Alan Munn Jul 23 '22 at 14:46
  • @AlanMunn I would like to do this manually because I would like to avoid, when feasible, hyphenation in the title; so, if I add, say, a \break command in the title, this unfortunately, carries over to the TOC, which I don't want for aesthetic reasons. – DDS Jul 23 '22 at 16:36
  • @AlanMunn And, thank you very much for pointing out \section[Short title]{Very long title}---this seems to allow me to accomplish what I want. – DDS Jul 23 '22 at 16:41

1 Answers1

1

It's almost always a bad idea to do things manually. And the sectioning commands provide built-in methods for distinguishing what goes into the TOC from what goes into the section heading itself. Specifically, you can use the optional argument of a sectioning command to specify the form of the TOC entry.

Typically, this is used to shorten very long TOC entries, but it can be used whenever you need to distinguish the cases. In your case, you want to allow using \break inside the long section heading, but you don't want it in the TOC, so you can just add the same text without the \break into the optional argument of \section.

In your example I've added a (horribly placed) \break to show how this works.

\documentclass{book}

\usepackage{titlesec} \titleformat{\section}[display] {\sffamily\Large\bfseries} {\filcenter\thesection} {1.25ex} {\justifycenter} \newcommand{\justifycenter}[1]{% \leftskip=0pt plus 1fil \rightskip=0pt plus -1fil \parfillskip=0pt plus 2fil #1% } \renewcommand{\thesection}{\arabic{section}}

\begin{document} \large \thispagestyle{empty}

\tableofcontents

\vspace*{25pt}

\section[Here is a Section Title on Which Centers the Number Above the Title Text and Justifies the Text as Well. However, I Do Not Want the Section Entry to be Entered into the TOC Automatically. How May I Turn It Off?]{Here is a Section Title on Which Centers the Number Above the Title Text and\break Justifies the Text as Well. However, I Do Not Want the Section Entry to be Entered into the TOC Automatically. How May I Turn It Off?} \end{document}

output of code

Alan Munn
  • 218,180
  • 1
    Many thanks. I was hoping that you would convert your comment into an answer. – DDS Jul 23 '22 at 16:54