[Tex/LaTex] Long title problem with fancyhdr and titlesec

fancyhdrtitlesec

I have the same problem as How to use short subsection title in header but not in table of contents?, and the solution given works almost well.

The difference is that also use the package titlesec. Here the code

\documentclass[a4paper,12pt]{article}

\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage{lipsum}
\usepackage{fancyhdr}
\usepackage{titlesec}

\pagestyle{fancy}

\newcommand{\markedsection}[2]{\section[#2]{#2%
\sectionmark{#1}}
\sectionmark{#1}}

\newcommand{\markedsubsection}[2]{\subsection[#2]{#2%
\subsectionmark{#1}}
\subsectionmark{#1}}

\lhead{\nouppercase \leftmark} \chead{} \rhead{\nouppercase \rightmark}

\begin{document}
\thispagestyle{plain}
\tableofcontents

\markedsection{short section}{Some section with quite a lengthy title}

\lipsum

\markedsubsection{short subsection}{Very very very long title of subsection}

\lipsum

\end{document}

But on the page where the subsection appears for the first time, the title in the header has not been modified, it is still the original long title, whereas on the next pages, the subsection titles ih the header is the short one. I want of course the short one even the first time. If I remove the titlesec package, everything works fine, as on the other post.
Can anyone help me with that ?

Best Answer

I don't know how to tackle the problem with fancyhdr, but since you use titlesec, I suggest doing it with the titleps module of titlesec instead. For unnumbered subsections, I had to define a new command (nonumsubsection) because titlesec doesn't seem to manage the formatting of starred (sub)sections with an optional argument :

\documentclass[a4paper,12pt]{article}

\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}

\usepackage{filecontents}

\begin{filecontents}{\jobname.bib}
@Book{Dodgson,
 author = {Lewis Carroll},
 title = {The Hunting of the Snark},
 year = {1876},
 publisher = {Macmillan},
 owner = {Bernard},
 timestamp = {2016.01.27}
}
 @book{lear72,
 author = {Edward Lear},
 year = {1872},
 title = {More Nonsense Pictures, Rhymes, Botany, Etc. },
 address = {London},
 publisher = {Robert John Bush}
 }
@book{Lear12,
author = {Edward Lear},
year = {2011},
title ={The Dong with a Luminous Nose},
address= {Petaluma (CA)},
Publisher = {Pomegranate Press}
}
\end{filecontents}

\usepackage{lipsum}
\usepackage{etoolbox, chngcntr} 
\usepackage[pagestyles, toctitles]{titlesec}
\newcommand\nonumsubsection[2][]{{\setcounter{secnumdepth}{0}\def\thesubsection{\relax}\ifblank{#1}{\subsection{#2}}{\subsection[#1]{#2}}
\setcounter{secnumdepth}{2}}\counterwithin{subsection}{section}}
\newpagestyle{ownstyle}{%
\headrule
\sethead{\thesection~\sectiontitle}{}{\ifnumequal{\value{secnumdepth}}{0}{\subsectiontitle}{\thesubsection~\subsectiontitle}}%
\setfoot{}{\thepage}{}
}%
\pagestyle{ownstyle}
\newpagestyle{bibetc}{%
\sethead{\scshape\sectiontitle}{}{}
\setfoot{}{\thepage}{}
}%

\begin{document}
\thispagestyle{plain}
\tableofcontents

\section[Short Section]{Some section with quite a lengthy title}

\lipsum

\subsection[Short subsection]{Very very very long title of subsection}

\lipsum

\nonumsubsection[Unnumbered subsection]{Very very very long title of unnumbered subsection}

\lipsum

\subsection{Another numbered subsection}
\lipsum

\pagestyle{bibetc}
\nocite{*}
\bibliographystyle{plain}
\bibliography{\jobname}

\end{document} 

enter image description here enter image description here enter image description here