7

In the titlesec package, one may use the \ifthesection (or \ifthechapter,...) command to apply some customization commands only when a \section has been defined. However, it seems that using \sectionmark (for instance to define a short running title) breaks this feature. Here is a MWE:

\documentclass{book}

\usepackage[pagestyles,outermarks]{titlesec}
\usepackage{lipsum}

\newpagestyle{main}{ 
\headrule
\sethead[\normalsize \thepage][][\scshape \chaptertitlename~\thechapter. \chaptertitle]
{\ifthesection{\thesection.~\sectiontitle}{}}{}{\normalsize \thepage}
}

\begin{document}

\tableofcontents

\pagestyle{main}

\chapter{First Chapter}
\lipsum
\lipsum

\chapter{Second Chapter}

\section{First Section}
\lipsum
\lipsum

\section{Second Section with a very long title that I do not want to show in the running heads but which is fine for the table of contents}
\sectionmark{Second Section}

\lipsum

\end{document}

In this example, the odd-side headers for chapter 1 (a chapter with no sections) only shows the page number, as expected (in particular the \ifthesection command prevents the dot from showing up):

enter image description here

In chapter 2, we have a first section for which everything works fine:

enter image description here

But things get more complicated when it comes to the second section, which has a very long name. Let us imagine that we want the full name in the table of contents, and a short running head in the headers. The command \sectionmark{Second Section} normally works, but it does not when \ifthesection is used in the headers style definition. In the present form, we get:

enter image description here

I can think of three possibilities to solve this problem:

  • One solution would be to remove the \ifthesection command in the headers style definition, but then a dot would appear in the headers for chapters with no sections. Then another way to test if this dot (or any other character) should be printed would be needed.
  • Using the optional argument for the \section command: \section[Second Section]{Second Section with a very long title that I do not want to show in the running heads but which is fine for the table of contents}. Then a hack would be needed to modify the table of contents so that the full name appears.
  • Playing around with marks to replace the \sectionmark{Second Section} with an appropriate command which does not break the \ifthesection command.

It seems to me that it would be preferable to choose the third solution, the other two looking more like work-arounds. However, I do not know much about the marks system. Does anybody know how to do this?

0

2 Answers 2

6

This is wanted: an explicit \sectionmark command makes \ifthesection false. You can trick titlesec into ignoring this setting by defining a macro similar to \sectionmark that doesn't perform that setting:

\makeatletter
\newcommand{\sectionmarkhead}[1]{%
  \gdef\sectiontitle{#1}%
  \let\ttl@marks\ttl@tm@section
  \ttl@markboth {section}}
\makeatother

If you use \sectionmarkhead{Second section} you'll get the desired result.

8
  • It works indeed. Is there a reason why it is preferable that \sectionmark sets \ifthesection to false by default?
    – Corentin
    Oct 10, 2012 at 9:26
  • @Corentin I really don't know why titlesec does that way: the main usage of explicit \sectionmark commands is exactly for setting a different header without affecting the table of contents entry.
    – egreg
    Oct 10, 2012 at 9:28
  • 1
    @egreg. Actually, \sectionmark doesn't set a different header, but rather emits another mark. If you want the botmark, that's fine, but if you want the firstmark, it does nothing (or ever worse, you'll have two different headers) because that in \section takes precedence (in the current page). \sectionmark it's intented for "numberless" sections, not for numbered ones. Oct 10, 2012 at 16:40
  • @egreg I can't make your macro work with \section*command: if I use the pagestyle main defined in the OP MWE along with your macro and then write a document like \begin{document} \chapter{First Chapter}\lipsum[1-10]\section*{First Section with a very long title}\sectionmarkhead{First Section}\lipsum[1]\end{document} I get a blank header instead of the desired result. Is there a workaround? Thanks in advance
    – petobens
    Apr 3, 2013 at 20:47
  • @petobens Why do you think it should work with \section*?
    – egreg
    Apr 3, 2013 at 20:50
5

If you always want the full name in the TOC (which is solution 2), use the package option toctitles.

0

You must log in to answer this question.

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