# How to use label and pageref inside titleformat to show chapter with each section (but only once a page)

For aesthetic reasons, I'm trying to repeat the chapter name before a section, but only if the chapter referencing is not on the same page, and no previous section on the same page is referencing to the chapter.

In short: page 1:

1. First Chapter

1.1 First Section

1.2 Second Section

page 2:

First Chapter

1.3 Third Section

1.4 Fourth Section

I know that using \thepage is not the correct way to do it, and that \pageref is preferred, but I can't get this to work. Apart from the \thepage being incorrect on the second page, the following does work, but will break very fast (as \thepage would not be correctly set on the second page, leaving it on the first page and not showing the chapter name):

\documentclass[10pt,a4paper]{report}
\usepackage{ifthen,xcolor}
\usepackage[small,sf,bf]{titlesec}

% get the text from the current chapter's title
\let\Chaptermark\chaptermark
\def\chaptermark#1{\def\Chaptername{#1}\Chaptermark{#1}}

\xdef\cp{\thepage} %define the page where the last chapter is
\xdef\sp{\thepage} %define the page where the last section is

\titleformat{\chapter}  %command
[block]                 %shape
{\huge\bfseries}        %format
{\thechapter}           %label
{0pt}                   %sep
{}                      %before-code
[\xdef\cp{\thepage}]    %after-code

% reformat \section
\titleformat{\section}
[block]
{\huge\bfseries}
{   % only show Chaptername once a page
\ifthenelse{\cp = \thepage}%
{}% if last chapter on same page, do nothing
{
\ifthenelse{\sp =  \thepage}%
{}% if last section on same page, do nothing
{\Chaptername \\} %show chapter name
}
}
{0pt}
{}
[\xdef\sp{\thepage}]

\begin{document}
\chapter{First Chapter}
\section{First Section}
\section{Second Section}
\lipsum
\section{Third Section}
\section{Fourth Section}
\end{document}


Using \label{\Sectionname} inside \titleformat for the section (\Sectionname analogue to \Chaptername) and then using \pageref{\Sectionname} instead of \thepage does not seems to work. (Nor does using a custom counter for both)

How can I properly use a reference to the page?

Here is a way with \label{\Chaptername} (hoping the chapter names are unique) and retrieving the page number with \getpagerefnumber from refcount package, comparing with \ifnum....

Don't redefine \sp -- this macro has a special meaning in TeX

There were a lot of spurious spaces as well, I removed them.

\documentclass[10pt,a4paper]{report}
\usepackage{lipsum}
\usepackage{xcolor}
\usepackage[small,sf,bf]{titlesec}

\usepackage{refcount}

% get the text from the current chapter's title
\let\Chaptermark\chaptermark
\def\chaptermark#1{\def\Chaptername{#1}\Chaptermark{#1}}

\xdef\currentp{\thepage} %define the page where the last chapter is
\xdef\lastp{\thepage} %define the page where the last section is

\titleformat{\chapter}  %command
[block]                 %shape
{\huge\bfseries}        %format
{\thechapter\ }           %label
{0pt}                   %sep
{}                      %before-code
[\label{\Chaptername}\xdef\currentp{\thepage}]    %after-code

% reformat \section
\titleformat{\section}
[block]
{\huge\bfseries}
{% only show Chaptername once a page
\ifnum\getpagerefnumber{\Chaptername}=\value{page}%
\else
\ifnum\lastp=\value{page}%
\else
\Chaptername\ \\%
\fi
\fi
}%
{0pt}%
{}%
[\xdef\lastp{\thepage}]

\begin{document}
\chapter{First Chapter}
\section{First Section}
\section{Second Section}
\lipsum
\section{Third Section}
\section{Fourth Section}
\clearpage
\section{Fifth Section}
\end{document}