1

I am using the titlesec package with the toctitles option, which modifies the optional argument of sectioning commands so that this optional argument is used only in headings; the non-optional argument is then used in both the main text and the table of contents. Not using the toctitles option would lead to the optional argument to also be used in the table of contents.

I need to have different arguments for each of the three targets: main text, headings, and table of contents. Is this possible? The reason for this is that I need different line breaks in the main text and in the table of contents, and I need to be able to specify different text for the headings. In case there are line-breaking commands that are context-aware (toc vs. main text), those would provide an alternative solution.

Kubo
  • 368
equaeghe
  • 5,976
  • 4
  • 30
  • 36
  • 2
    By any chance, are you using a KOMA-class? This functionality is built in there. – Johannes_B Dec 18 '13 at 14:08
  • @Johannes_B: No, and this is a big project, effectively too big to switch to KOMA. – equaeghe Dec 18 '13 at 14:11
  • 1
    Can you prepare a minimal working example+ This means showing us the documentclass, and everything to understand the topic. – Johannes_B Dec 18 '13 at 14:12
  • Related: How to set \rightmark after \section command? (suggestion there implements \section[<toc entry>]{<title>}[<header entry>] without titlesec) – Werner May 03 '14 at 05:12
  • @Johannes_B: Can you point to the functionality of KOMA-Script regarding the question above? I have the same question, and are using scrbook. I´ve been reading the manual, but are not sure what to look for. – Kubo Sep 18 '17 at 12:43
  • 1
    @Kubo Search for optiontoheadandtoc in the manual. The very specific thing you are looking for is an example on page 96 (at least 96 in my version of the manual). – Johannes_B Sep 18 '17 at 15:31
  • @Johannes_B: Wow, hit the nail on the head. Here is a solution for KOMA: \chapter[head={A normal chapter heading ...}, tocentry={A normal chapter-heading, which is a little bit to long \newline but can be broken here for the ToC}]{A normal chapter-heading, which is a little bit to long, but nothing is done to it here.} – Kubo Sep 18 '17 at 18:05

1 Answers1

1

We can insert an additional optional argument that allows for separately specifying a ToC/header/title of a \section.

The original definition of \ttl@straight@i (called by a sectional unit under the straight - default - class) looks like this:

\def\ttl@straight@i#1[#2]#3{%
  \def\@currentlabelname{#2}% for nameref
  \gdef\ttl@savemark{\csname#1mark\endcsname{#3}}%
  \let\ttl@savewrite\@empty
  \def\ttl@savetitle{#3}%
  \gdef\thetitle{\csname the#1\endcsname}%
  \if@noskipsec \leavevmode \fi
  \par
  \ttl@labelling{#1}{#2}%
  \ttl@startargs\ttl@straight@ii{#1}{#3}}

It takes 3 arguments and based on the way it is processed by titlesec, all of them are mandatory (but may be empty). We can insert a truly optional argument inbetween using some help from xparse:

\makeatletter
\RenewDocumentCommand{\ttl@straight@i}{m R[]{} o m}{%
  \def\@currentlabelname{#2}% for nameref
  \gdef\ttl@savemark{\csname#1mark\endcsname{#4}}%
  \let\ttl@savewrite\@empty
  \IfNoValueTF{#3}
    {\def\ttl@savetitle{#4}}% Optional #3 argument NOT supplied
    {\def\ttl@savetitle{#3}}% Optional #3 argument supplied
  \gdef\thetitle{\csname the#1\endcsname}%
  \if@noskipsec \leavevmode \fi
  \par
  \ttl@labelling{#1}{#2}%
  \ttl@startargs\ttl@straight@ii{#1}{#4}}
\makeatother

The argument R[]{} is a required argument delimited by [..] with an empty {} default; O{} would work just as well here. However, the inclusion of o now allows us to use either of the following:

  • \section[<header>][<ToC>]{<title>}

  • \section[<header>]{<ToC-and-title>}

  • \section{<header-ToC-and-title>}

We only have to condition on what to store in \ttl@savetitle based on whether or not [<ToC>] was supplied.

Here is a complete, minimal example:

enter image description here

\documentclass{article}
\usepackage[paper=a6paper]{geometry}% Just for this example
\usepackage[pagestyles,toctitles]{titlesec}
\usepackage{xparse}

\newpagestyle{main}{ 
  \headrule% Include a rule
  \sethead{\thesection.~\sectiontitle}% Left header
          {}                          % Center header
          {\normalsize \thepage}      % Right header
}
\pagestyle{main}

\makeatletter
\RenewDocumentCommand{\ttl@straight@i}{m R[]{} o m}{%
  \def\@currentlabelname{#2}% for nameref
  \gdef\ttl@savemark{\csname#1mark\endcsname{#4}}%
  \let\ttl@savewrite\@empty
  \IfNoValueTF{#3}
    {\def\ttl@savetitle{#4}}% Optional #3 argument NOT supplied
    {\def\ttl@savetitle{#3}}% Optional #3 argument supplied
  \gdef\thetitle{\csname the#1\endcsname}%
  \if@noskipsec \leavevmode \fi
  \par
  \ttl@labelling{#1}{#2}%
  \ttl@startargs\ttl@straight@ii{#1}{#4}}

\makeatother
\begin{document}

\tableofcontents

\section[fIrst sEctIOn][FiRST SeCTioN]{First section}
\clearpage
\section[sEcOnd sEctIOn]{Second section}
\clearpage
\section{Third section}

\end{document}
Werner
  • 603,163
  • What would I need to change to get \section[<ToC>][<header>]{<title>} and \section[<ToC>]{<header-and-title>}? – equaeghe May 09 '14 at 09:36
  • To answer the question in my own comment above: first exchange #2 and #3 and then replace all occurrences of #3 by \IfNoValueTF{#3}{#4}{#3}. To make things explicit, after this exchange, #1 corresponds to the section level, #2 to the optional <ToC>, #3 to the optional <header>, and #4 to the mandatory <title>, which is also used for <header> if the second optional argument is omitted or for <ToC> <header> if both optional arguments are omitted. – equaeghe May 14 '14 at 12:57