2

I'm attempting to use dictionary-style ranges for page headers using titlesec, but I'm running into a problem when trying to collapse them if there's only a single range on a given page.

The documentation suggests this is possible, but is also stunningly vague:

"Of course, this example should be fine tuned to collapse the ranges if there is a single section in a page, but it gives a hint of how to get stunning headers."

Given the following code below, I'd like the header on the second page to say "3" rather than "3-3". Why doesn't ifstrequal work?

\documentclass[]{book}
\usepackage{lipsum}
\usepackage[pagestyles]{titlesec}
\usepackage{etoolbox}

\renewcommand{\thesection}{\arabic{section}}

\newpagestyle{pagestyle}{
  \sethead[\ifstrequal{\toptitlemarks\thesection}{\bottitlemarks\thesection}{\toptitlemarks\thesection}{\toptitlemarks\thesection---\bottitlemarks\thesection}]
  []
  []
  {}
  {}
  {\ifstrequal{\toptitlemarks\thesection}{\bottitlemarks\thesection}{\toptitlemarks\thesection}{\toptitlemarks\thesection---\bottitlemarks\thesection}}
  }
\pagestyle{pagestyle}

\begin{document}

\section{}
\lipsum[1-2]
\section{}
\lipsum[1]
\section{}
\lipsum[1-6]
\section{}
\lipsum[1-2]

\end{document}
2

Here is a solution. titleps's documentation

One of the disavantaged of the switching mechanism is that comparing a command from, say, the top marks and the botttom marks is not trivial. This macro just makes it simpler. If you want to compare the currect \thesection with that in the top mark, write:

\ifsamemark\toptitlemarks\thesection{<true>}{<false>}
\documentclass[]{book}
\usepackage{lipsum}
\usepackage[pagestyles]{titlesec}
\usepackage{etoolbox}

\renewcommand{\thesection}{\arabic{section}}

\newpagestyle{pagestyle}{
  \sethead[\ifsamemark\toptitlemarks\thesection{\ifsamemark\bottitlemarks\thesection{\toptitlemarks\thesection}{\toptitlemarks\thesection---\bottitlemarks\thesection}}{\toptitlemarks\thesection---\bottitlemarks\thesection}]
  []
  []
  {}
  {}
  {\ifsamemark\bottitlemarks\thesection{\ifsamemark\toptitlemarks\thesection{\toptitlemarks\thesection}{\toptitlemarks\thesection---\bottitlemarks\thesection}}{\toptitlemarks\thesection---\bottitlemarks\thesection}}
  }
\pagestyle{pagestyle}

\begin{document}

\section{}
\lipsum[1-2]
\section{}
\lipsum[1]
\section{}
\lipsum[1-6]
\section{}
\lipsum[1-2]

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.