fancyhdr 和 titlesec 的长标题问题

fancyhdr 和 titlesec 的长标题问题

我有同样的问题如何在页眉中使用简短的小节标题,但不在目录中使用?,并且给出的解决方案几乎有效。

不同之处在于还使用了包titlesec。以下是代码

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

但在子部分首次出现的页面上,页眉中的标题没有被修改,仍然是原来的长标题,而在下一页上,页眉中的子部分标题是短标题。当然,即使是第一次,我也想要短标题。如果我删除 titlesec 包,一切都会正常工作,就像在另一篇文章中一样。有人能帮我吗?

答案1

我不知道如何使用 来解决这个问题fancyhdr,但是既然您使用了titlesec,我建议改用 模块来解决这个问题titlepstitlesec对于未编号的子节,我不得不定义一个新命令 ( nonumsubsection),因为titlesec似乎无法使用可选参数 来管理带星号的(子)节的格式:

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

在此处输入图片描述 在此处输入图片描述 在此处输入图片描述

相关内容