3

Some of my section titles in my book xetex document with fancyhdr are too long for putting them into the running head. I use \sectionmark in order to set a shorter version for the running head. This works well (see last page of the MWE),
shortened sectiontitle
but not on the first page of the section (if this page happens to be an odd page, where section running heads are displayes, see page 3 of the MWE).
full sectiontitle

What can I do that the compiler respects the \sectionmark?

MWE:

\documentclass[10pt]{book}
\usepackage[nobottomtitles]{titlesec}
\usepackage{kantlipsum}
\usepackage{fancyhdr}
\makeatletter%hier definition schriftgrößen-makro
  \newcommand\chapsize{\@setfontsize\chapsize{13pt}{6}}
  \newcommand\secsize{\@setfontsize\secsize{11.5pt}{6}}
  \newcommand\pnsize{\@setfontsize\pnsize{10.5pt}{6}}
  \newcommand\appsize{\@setfontsize\appsize{9pt}{6}}
\makeatother

\titleformat{\chapter}[hang]
   {\chapsize\centering}{\thechapter.\ }{0pt}{}
\titleformat{\section}[hang] 
    {\secsize\centering}{\thesection\ }{0pt}{}
\titlespacing*{\chapter}{0pt}{0pt}{8mm}
\titlespacing*{\section}{0pt}{24pt}{4.233mm}

\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{#1}}
\fancyhf{}
\fancyfoot[LE,RO]{\pnsize{\thepage}}
\fancyhead[CO]{\fontsize{9.5}{10.5}\selectfont\scshape\MakeLowercase{\rightmark}}
\fancyhead[CE]{\fontsize{9.5}{10.5}\selectfont\scshape\MakeLowercase{\leftmark}}
\renewcommand{\headrulewidth}{0pt}


\begin{document}

\chapter{Chapter One}
\section{First Section}
\kant[1-7]
\section{Second section: Too much text for a running head}
\sectionmark{Second section}
\kant[8-15]
\end{document}
shevek
  • 451
  • 3
  • 14
  • You problem seesm to be related to the fact that \chapter contains a command \thispagestyle{plain}. Biside this, you should not use 'sectionmark this way, bu use the optional argument of\sectionlike in\section[Short enough]{Second section: Too much text for a running head}` – Jhor Sep 24 '19 at 14:42
  • Try \renewcommand{\sectionmark}[1]{\def\rightmark{#1}} – koleygr Sep 24 '19 at 14:46
  • @Jhor, the \chapter pagestyle affects the \section? Yet, even when I remove \chapter, the problem persists. And the optional argument for the section command is not an option for me, because what is written there will make its way into the TOC, which is not what I want. – shevek Sep 24 '19 at 14:58
  • @koleygr - your suggestion works! I just tried it with the whole, real, book and also there it worked. Can you set this as an answer? And what kind of magic does it? I have to admit that the fancyhdr documentation was way beyond my comprehension what rightmark is there for. – shevek Sep 24 '19 at 15:10
  • @shevek... Check the somehow automated way for your purposes... – koleygr Sep 24 '19 at 15:13
  • Why don't you just use the optional argument: \section[short text](long text}? – barbara beeton Sep 24 '19 at 17:38
  • @barbarabeeton the optional argument for the section command is not an option for me, because what is written there will make its way into the TOC, which is not what I want. – shevek Sep 24 '19 at 20:39
  • Thank you for the clarification. The other thing I'd try is \markright. I see that's used in this answer in the (re)definition of \sectionmark but apparently isn't working. The next thing I'd try is using \rightmark directly after \section. And if that works, I'd assume that fancyhdr delays its definitions until \AtBeginDocument; if true, then your redefinition of \sectionmark can be changed to use \AtBeginDocument. – barbara beeton Sep 24 '19 at 21:08
  • @barbarabeeton Thank you for that background info. Although I really don't understand what the difference between markright and rightmark in fancyhdr is: the redefinition of sectionmark IS working, see the comments to the answer of @koleygr. – shevek Sep 24 '19 at 21:36
  • \rightmark uses the content of what has been stored by specifying \markright. If \markright (or \markboth) hasn't been specified, \rightmark won't contain anything, so nothing will be printed.. (I hope this makes sense.) – barbara beeton Sep 24 '19 at 22:04

2 Answers2

2

Answer by simplifying the code preamble code (but making more difficult the user usage) after OP's request:

You could replace your \renewcommand{\sectionmark}[1]{\markright{#1}}

by \renewcommand{\sectionmark}[1]{\gdef\rightmark{#1}}

\documentclass[10pt]{book}
\usepackage[nobottomtitles]{titlesec}
\usepackage{kantlipsum}
\usepackage{fancyhdr}
\makeatletter%hier definition schriftgrößen-makro
  \newcommand\chapsize{\@setfontsize\chapsize{13pt}{6}}
  \newcommand\secsize{\@setfontsize\secsize{11.5pt}{6}}
  \newcommand\pnsize{\@setfontsize\pnsize{10.5pt}{6}}
  \newcommand\appsize{\@setfontsize\appsize{9pt}{6}}
\makeatother

\titleformat{\chapter}[hang]
   {\chapsize\centering}{\thechapter.\ }{0pt}{}
\titleformat{\section}[hang] 
    {\secsize\centering}{\thesection\ }{0pt}{}
\titlespacing*{\chapter}{0pt}{0pt}{8mm}
\titlespacing*{\section}{0pt}{24pt}{4.233mm}

\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\gdef\rightmark{#1}}
\fancyhf{}
\fancyfoot[LE,RO]{\pnsize{\thepage}}
\fancyhead[CO]{\fontsize{9.5}{10.5}\selectfont\scshape\MakeLowercase{\rightmark}}
\fancyhead[CE]{\fontsize{9.5}{10.5}\selectfont\scshape\MakeLowercase{\leftmark}}
\renewcommand{\headrulewidth}{0pt}


\begin{document}

\chapter{Chapter One}
\section{First Section}
\kant[1-7]
\section{Second section: Too much text for a running head}
\sectionmark{Second section}
\kant[8-15]
\end{document}

Old answer: Automating (adding in the definition of \section a \sectionmark command )

You could redefine section according to my post here to have an extra ability of using a "starred" section but with optional argument that would mean it will be a normal (non starred) section with changed \sectionmark .

See how it works below in your changed MWE:

\documentclass[10pt]{book}
\usepackage[nobottomtitles]{titlesec}
\usepackage{kantlipsum}
\usepackage{fancyhdr}
\makeatletter%hier definition schriftgrößen-makro
  \newcommand\chapsize{\@setfontsize\chapsize{13pt}{6}}
  \newcommand\secsize{\@setfontsize\secsize{11.5pt}{6}}
  \newcommand\pnsize{\@setfontsize\pnsize{10.5pt}{6}}
  \newcommand\appsize{\@setfontsize\appsize{9pt}{6}}
\makeatother

\titleformat{\chapter}[hang]
   {\chapsize\centering}{\thechapter.\ }{0pt}{}
\titleformat{\section}[hang] 
    {\secsize\centering}{\thesection\ }{0pt}{}
\titlespacing*{\chapter}{0pt}{0pt}{8mm}
\titlespacing*{\section}{0pt}{24pt}{4.233mm}

\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\gdef\rightmark{#1}}
\fancyhf{}
\fancyfoot[LE,RO]{\pnsize{\thepage}}
\fancyhead[CO]{\fontsize{9.5}{10.5}\selectfont\scshape\MakeLowercase{\rightmark}}
\fancyhead[CE]{\fontsize{9.5}{10.5}\selectfont\scshape\MakeLowercase{\leftmark}}
\renewcommand{\headrulewidth}{0pt}

\let\oldsection\section
\makeatletter
\def\section{%
\@ifstar{\@Starred}{\@nonStarred}%
}
\def\@Starred{%
\@ifnextchar[%
{\GenericWarning{}{Warning: A starred section can not have optional parameters. I am going to ignore them!}\@StarredWith}%
{\@StarredWithout}%
}      
\def\@StarredWith[#1]#2{%%
\oldsection{#2}\sectionmark{#1}%
}
\def\@StarredWithout#1{%
\oldsection*{#1}%
}
\def\@nonStarred{%
\@ifnextchar[%
{\@nonStarredWith}%
{\@nonStarredWithout}%
}
\def\@nonStarredWith[#1]#2{%
\oldsection[#1]{#2}%
}
\def\@nonStarredWithout#1{%
\oldsection{#1}%
}
\makeatother 

\begin{document}

\chapter{Chapter One}
\section{First Section}
\kant[1-7]
\section*[Second section]{Second section: Too much text for a running head}
\kant[8-15]
\end{document}
koleygr
  • 20,105
  • An even better way would be to redefine in such a way that will accept two optional arguments (first for TOC and second for header)... Feel free to ask me show you how to do it... But I think the code already provided should be enough for you. – koleygr Sep 24 '19 at 15:16
  • Thanks a lot! But for my case the whole redefinition of section seems not to be necessary. Just adding \renewcommand{\sectionmark}[1]{\def\rightmark{#1}} to the preamble, or even better, from your last posting, \renewcommand{\sectionmark}[1]{\gdef\rightmark{#1}} instead of \renewcommand{\sectionmark}[1]{\markright{#1}} seems to do it. Would that observation be worth to be added to the answer? – shevek Sep 24 '19 at 21:02
  • @shevek .. I added that answer but it makes more difficult the usage (as mentioned in my comment there). My redefinition of section, are just some lines of code (usefull in many cases) that have no neggeative or whatever additional effect except of the additional ability to add a starred section with an optional argument to be used just for header (and the section will not really be starred) – koleygr Sep 24 '19 at 21:54
0

Just put additional \sectionmark before \section

\sectionmark{Second section}
\section{Second section: Too much text for a running head}
\sectionmark{Second section}
Manx
  • 7
  • This is not a useful answer, because, although it puts up the short section title, it puts it up with the number of the preceding section. – Geoff Pointer Sep 05 '21 at 23:16