# Trouble setting header content conditionally with titlesec and extramarks

I'm writing a commentary on a Classical text and trying to imitate many of the layout choices of the Cambridge Greek and Latin Classics series. In particular, I've been working on having the header specify what lines of the text are discussed on that page, e.g. COMMENTARY: 13-17.

Using features in titlesec and titleps (extramarks, topextramarks and botextramarks primarily), I'm most of the way there, but I've run into an edge case that I can't fix. The edge case is that some pages may have commentary only on one line. As things stand, my code will then give a heading of, e.g., COMMENTARY: 3-3.

So what I want to do (I think) is have a condition that says "if the first and last line number on the page are the same, only print it once; otherwise, print a range of top-bottom". But I haven't been able to get any such conditional to work.

Here's what I've tried:

• Using xparse and etoolbox to create a macro that tests with ifnumequal. This fails to compile. (I'm guessing because of when/where the macros are expanded?)

• Using ifsamemark from titleps to test whether topextramarks and botextramarks have the same value for the relevant extramark. I can get this to compile, but not to give the right results.

The minimal working example below shows the right output on the first line and the edge-case problem on its second page. (The details of how to choose which lines appear are slightly complicated and may not matter, so I've stuck those below the MWE.)

\documentclass[11pt]{article}
\usepackage{titlesec}
\usepackage[extramarks]{titleps}
\newcounter{firstLine}
\setcounter{firstLine}{1}
\newmarkset{firstLine}
\newextramark*{firstLine}{firstLine}
\usepackage{xparse}
\DeclareDocumentCommand \handleOneArg{m}{
\setcounter{firstLine}{#1}
\extramark{firstLine}
\subsection{#1}
}
\DeclareDocumentCommand \handleTwoArgs{m m}{
\setcounter{firstLine}{#1}
\extramark{firstLine}
\subsection{{#1}--{#2}}
}
\DeclareDocumentCommand \comSubSection{o m}{
\IfNoValueTF{#1}
{ \handleOneArg{#2} }
{ \handleTwoArgs{#1}{#2} }
}
% Not currently used: a failed attempt to solve my problem.
\usepackage{etextools}
\DeclareDocumentCommand \fixRange{m m} {
\ifnumequal{#2}{#1}{#1}{#1--#2}
}
% TODO
% I need a way to compare \topextramarks and \botextramarks, so that if they
% are equal, I just print COMMENTARY: 1 and not COMMENTARY: 1--1
\newpagestyle{commentary}{%
[COMMENTARY:~\topextramarks{firstLine}{\arabic{firstLine}}--%
\botextramarks{firstLine}{\arabic{firstLine}}]%even-center
[]% even-right
{}%odd-left
{COMMENTARY:~\topextramarks{firstLine}{\arabic{firstLine}}--%
\botextramarks{firstLine}{\arabic{firstLine}}}%even-center
{}% odd-right
\setfoot{}%left
{\thepage}%center
{}%right
}
\titleformat{\section}{\centering\normalfont\itshape}{}{0em}{}
\titleformat{\subsection}[runin]{\normalfont\bfseries}{}{0em}{}

\usepackage{lipsum}

\begin{document}

\pagestyle{commentary}
\section{1--10 Section Summary}

\lipsum[4]

\comSubSection{1}

\lipsum[10]

\comSubSection[1]{4}

\lipsum[20]

\comSubSection{2}

\lipsum[2]

\comSubSection[3]{7}

\lipsum[8]

\comSubSection[3]{5}

\lipsum[2]

\end{document}


• The relevant line numbers come from subsections in my document. Sections are for larger chunks of text with short introductions.
• Subsections can be notes on a single line or a range of lines: e.g. 4 or 4-8.
• If a subsection specifies a range, the important number is the first. So if at the top of the page, there's a note on lines 5-10 and at the bottom of the page a note on lines 7-9, the header should read thus: COMMENTARY: 5-7.
• The header should include a single number if (a) the page only comments on one line or (b) the first comment and last comment for a given page start commenting on the same line (or comment only on the same line). This edge case is most likely to happen on first and last pages, and can theoretically happen anywhere if a note grows very long.
• Can I reproduce this without Times New Roman or will that throw the example off due to space changes? If not, I'd recommend editing the MWE to use a standard font (e.g. let it default to Latin Modern and just set up the example so that triggers the edge case). – cfr Oct 20 '14 at 22:43

This is a bit kludgy:

\documentclass[11pt]{article}
\usepackage{fontspec}
\setmainfont{Times New Roman}
\usepackage{titlesec}
\usepackage[extramarks]{titleps}
\newcounter{firstLine}
\setcounter{firstLine}{1}
\newmarkset{firstLine}
\newextramark*{firstLine}{firstLine}
\usepackage{xparse}
\DeclareDocumentCommand \handleOneArg{m}{
\setcounter{firstLine}{#1}
\extramark{firstLine}
\subsection{#1}
}
\DeclareDocumentCommand \handleTwoArgs{m m}{
\setcounter{firstLine}{#1}
\extramark{firstLine}
\subsection{{#1}--{#2}}
}
\DeclareDocumentCommand \comSubSection{o m}{
\IfNoValueTF{#1}
{ \handleOneArg{#2} }
{ \handleTwoArgs{#1}{#2} }
}
\newcommand*\topfirstLine{\topextramarks{firstLine}{\arabic{firstLine}}}
\newcommand*\botfirstLine{\botextramarks{firstLine}{\arabic{firstLine}}}
\newpagestyle{commentary}{%
[COMMENTARY:~%
\edef\tempa{\topfirstLine}\edef\tempb{\botfirstLine}%
\topfirstLine\ifx\tempa\tempb\relax\else--\botfirstLine\fi]%even-center
[]% even-right
{}%odd-left
{COMMENTARY:~%
\edef\tempa{\topfirstLine}\edef\tempb{\botfirstLine}%
\topfirstLine\ifx\tempa\tempb\relax\else--\botfirstLine\fi}%even-center
{}% odd-right
\setfoot{}%left
{}%center
{\thepage}%right
}
\titleformat{\section}{\centering\normalfont\itshape}{}{0em}{}
\titleformat{\subsection}[runin]{\normalfont\bfseries}{}{0em}{}

\usepackage{lipsum}

\begin{document}

\pagestyle{commentary}
\section{1--10 Section Summary}

\lipsum[4]

\comSubSection{1}

\lipsum[10]

\comSubSection[1]{4}

\lipsum[20]

\comSubSection{2}

\lipsum[2]

\comSubSection[3]{7}

\lipsum[8]

\comSubSection[3]{5}

\lipsum[2]

\end{document}


• A "bit kludgy" but working is a huge advance for me! I'm going to leave this open a little longer to see if it attracts other thoughts, but thank you so much. – Telemachus Oct 21 '14 at 11:35