重新定义 \chapter 宏后,titleps 中的 \chaptertitle 被忽略

重新定义 \chapter 宏后,titleps 中的 \chaptertitle 被忽略

以下 MWE 表明,如果\chapter像我之前的问题一样重新定义宏:在带星号和不带星号的 \chapter 命令前添加不同的内容,来自(或在?)包\chaptertitle中的宏会被例如、和命令忽略(但不会被普通章节忽略)。titlepstitlesec\tableofcontents\listoffigures\listoftables

\documentclass{book}
\usepackage{lipsum}
\usepackage{xparse,xpatch}
\usepackage{titleps}
\usepackage{tocvsec2}
%
\makeatletter%
% Hack to be sure \resetsecnumdepth* from tocvsec2 behaves well
\@ifpackagelater{tocvsec2}{2014/03/10}{%
}{%
  \renewcommand\resetsecnumdepth{%
    \@ifstar{%
      \setcounter{secnumdepth}{\value{max@secnumdepth}}%
      \edef\stack@secnumdepth{\the\c@secnumdepth\relax}%
    }{%
      \ifx\stack@secnumdepth\@empty%
      \PackageWarning{tocvsec2}{There is no previous value for secnumdepth}%
      \else%
      \afterassignment\gobble@secnumdepth%
      \expandafter\c@secnumdepth\expandafter\numexpr\stack@secnumdepth\@nil%
      \fi%
    }%
  }%
}%
\makeatother%
\let\ORIchapter\chapter%
\RenewDocumentCommand\chapter{som}{%
  \IfBooleanTF{#1}
    {
     \setsecnumdepth{none}%
    }
    {%
     \resetsecnumdepth*
    }
  \IfNoValueTF{#2}
    {\ORIchapter{#3}}
    {\ORIchapter[#2]{#3}}%
}
% we don't want \chapter* for \tableofcontents and similar lists
\xpatchcmd{\tableofcontents}{\chapter}{\ORIchapter}{}{}
\xpatchcmd{\listoffigures}{\chapter}{\ORIchapter}{}{}
\xpatchcmd{\listoftables}{\chapter}{\ORIchapter}{}{}
% populate the stack
\AtBeginDocument{\setsecnumdepth{none}}
%
\newpagestyle{liminary}[]{%
  \sethead[\thepage]%
  []%
  [\chaptertitle]%
  {%
    \sectiontitle%
  }%
  {}%
  {\thepage}%
  \headrule%
}%
%
\pagestyle{liminary}
\begin{document}
\tableofcontents
\listoffigures
\listoftables
\chapter{A 1st chapter}
\lipsum[1-10]
\section{A 1st section of the 1st chapter}
\lipsum[1-10]
\end{document}

如果行:

\xpatchcmd{\listoffigures}{\chapter}{\ORIchapter}{}{}
\xpatchcmd{\listoftables}{\chapter}{\ORIchapter}{}{}

被注释掉,相应的内容\chaptertitle不会被忽略(可能有我没有注意到的副作用)但如果出现以下行:

\xpatchcmd{\tableofcontents}{\chapter}{\ORIchapter}{}{}

被注释掉后,会出现“TeX capacity beyond”的错误:

! TeX capacity exceeded, sorry [input stack size=5000].
\@nameuse #1->\csname #1
                        \endcsname 
l.62 \tableofcontents

!  ==> Fatal error occurred, no output PDF file produced!

无论有没有,注意到这一点可能会有所帮助:

\xpatchcmd{\printglossary}{\chapter}{\ORIchapter}{}{}

\printglossary命令相对于 表现良好\chaptertitle,如以下 MWE 所示:

\documentclass{book}
\usepackage{lipsum}
\usepackage{xparse,xpatch}
\usepackage{titleps}
\usepackage{tocvsec2}
\usepackage{glossaries}
\makeglossaries
\newglossaryentry{bar}{%
  name={bar},
  description={bar}
}
%
\makeatletter%
% Hack to be sure \resetsecnumdepth* from tocvsec2 behaves well
\@ifpackagelater{tocvsec2}{2014/03/10}{%
}{%
  \renewcommand\resetsecnumdepth{%
    \@ifstar{%
      \setcounter{secnumdepth}{\value{max@secnumdepth}}%
      \edef\stack@secnumdepth{\the\c@secnumdepth\relax}%
    }{%
      \ifx\stack@secnumdepth\@empty%
      \PackageWarning{tocvsec2}{There is no previous value for secnumdepth}%
      \else%
      \afterassignment\gobble@secnumdepth%
      \expandafter\c@secnumdepth\expandafter\numexpr\stack@secnumdepth\@nil%
      \fi%
    }%
  }%
}%
\makeatother%
\let\ORIchapter\chapter%
\RenewDocumentCommand\chapter{som}{%
  \IfBooleanTF{#1}
    {
     \setsecnumdepth{none}%
    }
    {%
     \resetsecnumdepth*
    }
  \IfNoValueTF{#2}
    {\ORIchapter{#3}}
    {\ORIchapter[#2]{#3}}%
}
% we don't want \chapter* for \tableofcontents and similar lists
\xpatchcmd{\tableofcontents}{\chapter}{\ORIchapter}{}{}
\xpatchcmd{\listoffigures}{\chapter}{\ORIchapter}{}{}
\xpatchcmd{\listoftables}{\chapter}{\ORIchapter}{}{}
\xpatchcmd{\printglossary}{\chapter}{\ORIchapter}{}{}
% populate the stack
\AtBeginDocument{\setsecnumdepth{none}}
%
\newpagestyle{liminary}[]{%
  \sethead[\thepage]%
  []%
  [\chaptertitle]%
  {%
    \sectiontitle%
  }%
  {}%
  {\thepage}%
  \headrule%
}%
%
\pagestyle{liminary}
\glsaddall
\begin{document}
\printglossary
\tableofcontents
\listoffigures
\listoftables
\chapter{A 1st chapter}
\lipsum[1-10]
\section{A 1st section of the 1st chapter}
\lipsum[1-10]
\end{document}

因此我的问题是:是否可以\chapter像我之前的问题一样调整宏的重新定义:在带星号和不带星号的 \chapter 命令前添加不同的内容为了\chaptertitletitleps包中获取信息,不会忽略诸如\tableofcontents\listoffigures和之类的命令\listoftables

答案1

titlesec\chapter在开始文档时重新定义,因此您必须延迟自己的重新定义。

我提供了带有词汇表的版本,以表明一切进展顺利;tocvsec2自从我更新了我的发行版后,我删除了 hack。

\documentclass{book}
\usepackage{lipsum}
\usepackage{xparse,xpatch}
\usepackage{tocvsec2}
\usepackage{titleps}

\usepackage{glossaries}
\makeglossaries

\AtBeginDocument{% delay the redefinition when titlesec has done its own
\let\ORIDENISchapter\chapter
\RenewDocumentCommand\chapter{som}{%
  \IfBooleanTF{#1}
    {
     \setsecnumdepth{none}%
    }
    {%
     \resetsecnumdepth*
    }%
  \IfNoValueTF{#2}
    {\ORIDENISchapter{#3}}
    {\ORIDENISchapter[#2]{#3}}%
}
}% end of \AtBeginDocument
% we don't want \chapter* for \tableofcontents and similar lists
\xpatchcmd{\tableofcontents}{\chapter}{\ORIDENISchapter}{}{}
\xpatchcmd{\listoffigures}{\chapter}{\ORIDENISchapter}{}{}
\xpatchcmd{\listoftables}{\chapter}{\ORIDENISchapter}{}{}
% populate the stack
\AtBeginDocument{\setsecnumdepth{none}}
%
\newpagestyle{liminary}[]{%
  \sethead[\thepage]%
  []%
  [\chaptertitle]%
  {%
    \sectiontitle%
  }%
  {}%
  {\thepage}%
  \headrule%
}%
%
\pagestyle{liminary}

\newglossaryentry{bar}{
  name={bar},
  description={bar}
}

\glsaddall


\begin{document}
\printglossary
\tableofcontents
\listoffigures
\listoftables
\chapter{A 1st chapter}
\lipsum[1-10]
\section{A 1st section of the 1st chapter}
\lipsum[1-10]
\end{document}

第 4 页(目录后的空白页)的顶部显示正确的标题。

在此处输入图片描述

相关内容