# Long title problem with fancyhdr and titlesec

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}}

\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 ?

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. },
publisher = {Robert John Bush}
}
@book{Lear12,
author = {Edward Lear},
year = {2011},
title ={The Dong with a Luminous Nose},
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}{%
\setfoot{}{\thepage}{}
}%
\pagestyle{ownstyle}
\newpagestyle{bibetc}{%
\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}


• thanks, it works perfectly! I didn't know this package extension, can it do the same as fancyhdr does ? To be complete, if someone has a solution using fancyhdr, it might be helpfull for others. – antoine Apr 18 '16 at 10:25
• As far as I know, it can do more or less the same, without having the hassle to cope with marks. I always could do what I wanted with titleps. Note it is mentioned in the doc of titlesec, but its own documentation with more details. – Bernard Apr 18 '16 at 10:30
• I unchecked the solution so far because I have some issues with the titeps solution, if nobody can solve the original problem, I'll check it back. The problem I have is I want some sections unnumbered, and in that case it becomes very tedious because using starred section does not go along with titleps. Moreover, for the appendix, it is the same, since apparently Latex uses \markboth and this is actually the problem. So the use of fancyhdr would be better for me for those reasons, but I will have again my original problem... I don't know if this is very clear – antoine Apr 19 '16 at 9:30
• @antoine: For the starred (sub)sections, I have a solution. For appendix what you want is not quite clear to me. Could give some details on what you want? – Bernard Apr 19 '16 at 10:26
• Oups sorry, it is not the appendix but the references. When I use \bibliography the header is the number of the previous section followed by "References", even using \bibliographystyle{plain}. So the number shoud not be there – antoine Apr 19 '16 at 11:24