When using the titlesec package to customize part headers, the counter \thepart is somehow incremented before its value is used in, for example, running headers:
\documentclass[oneside]{book}
\usepackage{titlesec}
% If you do not issue the \titleformat command, everything is fine
\titleformat{\part}[display]
{\bfseries\huge\filcenter}
{\partname\ \thepart%
% A working hack: set the mark here instead, expanding \thepart
% (note the lack of \protect)
%\markright{Part \thepart, page \protect\thepage}
}
{20pt}
{\Huge}
\titlespacing*{\part}{0pt}{*36}{*1}
\begin{document}
\part{Intro}
\pagestyle{myheadings}
% Remove this to see the hack work.
\markright{Part \protect\thepart, page \protect\thepage}
This page should have ``\textsl{Part \thepart, page \thepage}'' at the top.
\newpage
This page should have ``\textsl{Part \thepart, page \thepage}'' at the top.
\part{A Problem} % This command does the incrementing
\end{document}
Page 3 – the last page in Part I – is the problem:

Somehow the second \part{A Problem} is advancing the counter before the counter is used in the header.
What exactly is the problem, and what is the suggested fix? One can hack a solution as shown, saving the value of the \thepart counter when the part is typeset, but this is not a satisfactory solution as I would like a user to be able to use the counter in custom headers etc.
The titlesec manual notes that parts are somewhat unique and non-starndard, but this seems like a bug to me.
Update
In response to the answer by @hugovdberg, I can trace the problem to the following definition in titlesec.sty:
\makeatletter\def\ttl@page@i#1[#2]#3{%
\gdef\ttl@savemark{\csname#1mark\endcsname{#3}}%
\def\ttl@savewrite{\ttl@write{#1}{#3}}% A default value. Not #2!
\def\ttl@savetitle{#3}%
\ttl@labelling{#1}{#2}%
\ttl@startargs\ttl@page@ii{#1}{#3}}
The \clearpage commands that will cause the headers to be processed is inside the
\ttl@startargs\ttl@page@ii{#1}{#3} call while the part counter is increased in the previous \ttl@labelling{#1}{#2} call. Switching the order of these fixes the problem, but I do not know if there are other consequences. Seems like a bug to me.
Second Update
There are some questions about why I need to \protect\thepage: it is because \markright expands its arguments, but because of the way the standard classes reset the headers each time, this forces me to call \markright after each part, so using an expanded (unprotected) \thepart will appear to solve the problem. Here is a second, not quit MWE that uses fancyhdr to demonstrate that there really is a problem, and it has nothing to do with protection:
\documentclass[oneside]{book}
\usepackage{titlesec}
\usepackage{fancyhdr}
% Now I can do this once and for all: no protection needed.
\chead{Part \thepart, page \thepage}
% If you do not issue the \titleformat command, everything is fine
\titleformat{\part}[display]
{\bfseries\huge\filcenter}
{\partname\ \thepart%
% A working hack: set the mark here instead, expanding \thepart
% (note the lack of \protect)
%\markright{Part \thepart, page \protect\thepage}
}
{20pt}
{\Huge}
\titlespacing*{\part}{0pt}{*36}{*1}
\begin{document}
\pagestyle{fancy}
\part{Intro}
This page should have ``\textsl{Part \thepart, page \thepage}'' at the top.
\newpage
This page should have ``\textsl{Part \thepart, page \thepage}'' at the top.
\part{A Problem} % This command does the incrementing
A new page
\end{document}

Related
- Problems with part-labels using titlesec: Addresses related issues with the part label, noting that
titlesecdoes some strange things with parts. The answer there does not solve this problem. - titlesec: \assignpagestyle problem with part: Also mentions some issues with parts, but not obviously related to this question.
\thepartin\markright(you shouldn't). If I remove\protectit works. – Javier Bezos Feb 26 '14 at 14:43\markrightexpands its arguments. This is a little hard to demonstrate with the standard classes since I need to reset the header each time, but is easy to show with the KOMA classes. (I thought at first it might be a KOMA/titlesec issue, but it is not.) I will update the MWE shortly with will usefancyhdr: there expansion is not done, and using an unprotected\thepartfails exactly as here. – mforbes Feb 26 '14 at 18:38