不要在 \tableofcontents 中使用替代章节标题

不要在 \tableofcontents 中使用替代章节标题

我的文档到处都使用如下标题。

\section[short]{longlonglong}

标题short用于页眉和目录。longlonglong标题仅用于章节标题本身。

我如何longlonglong在目录中使用标题?我想保留页眉的当前格式。

谢谢你!

更新:

\documentclass[12pt,a4paper]{report}

%\usepackage{../sty/reportpage}

\begin{document}
\pagestyle{reportpage}
\pagenumbering{roman}
\setcounter{page}{1}

\cleardoublepage
\renewcommand{\baselinestretch}{1.25}
\small\normalsize

\tableofcontents

\renewcommand{\baselinestretch}{1}
\small\normalsize

\cleardoublepage

\pagenumbering{arabic}
\setcounter{page}{1}

\chapter[First Chapter]{The first chapter has a long title that only is shown as the chapters headline}
Some irelevant text.

\cleardoublepage

more irelevant text.

\end{document}

答案1

我不会弄乱目录,只是不设置简短版本,而是将标题更改为标题,例如\chaptermark

不幸的是您的代码不包含任何要测试的标题,因此我改用了fancyhdr

\documentclass[12pt,a4paper]{report}

%\usepackage{../sty/reportpage}

\usepackage{fancyhdr}

\begin{document}
%\pagestyle{reportpage}
\pagestyle{fancy}
\pagenumbering{roman}
\setcounter{page}{1}

\cleardoublepage
\renewcommand{\baselinestretch}{1.25}
\small\normalsize

\tableofcontents

\renewcommand{\baselinestretch}{1}
\small\normalsize

\cleardoublepage

\pagenumbering{arabic}
\setcounter{page}{1}

\chapter{The first chapter has a long title that only is shown as the chapters headline}
\chaptermark{short version}

Some irelevant text.

\cleardoublepage

more irelevant text.

\end{document}

答案2

重新定义\@chapter或使用补丁来修改写入 ToC 条目的代码,即\addcontentsline

report用途

\ifnum \c@secnumdepth >\m@ne
                         \refstepcounter{chapter}%
                         \typeout{\@chapapp\space\thechapter.}%
                         \addcontentsline{toc}{chapter}%
                                   {\protect\numberline{\thechapter}#1}%
                    \else
                      \addcontentsline{toc}{chapter}{#1}%
                    \fi

所以#1意味着可选参数,但#2在这里是需要的。

基本也是如此\@sect,它使用#7,必须用 替换#8

下面的代码使用开关来修改条目:

假设\uselongtitletrue在目录中启用长标题,但在标题栏中保留短标题,\uselongtitlefalse以便使用短标题。

\documentclass[12pt,a4paper]{report}

%\usepackage{../sty/reportpage}

\usepackage{xpatch}

\newif\ifuselongtitle

\uselongtitletrue

\makeatletter
\xpatchcmd{\@chapter}{%
  \ifnum \c@secnumdepth >\m@ne
  \refstepcounter{chapter}%
  \typeout{\@chapapp\space\thechapter.}%
  \addcontentsline{toc}{chapter}%
  {\protect\numberline{\thechapter}#1}%
  \else
  \addcontentsline{toc}{chapter}{#1}%
  \fi}{%
  \ifnum \c@secnumdepth >\m@ne
  \refstepcounter{chapter}%
  \typeout{\@chapapp\space\thechapter.}%
  \addcontentsline{toc}{chapter}%
  {\protect\numberline{\thechapter}\ifuselongtitle#2\else#1\fi}%
  \else
  \addcontentsline{toc}{chapter}{\ifuselongtitle#2\else#1\fi}%
  \fi
}{\typeout{Success}}{\typeout{Failure}}

\xpatchcmd{\@sect}{%
  \fi #7%
}{\fi\ifuselongtitle#8\else#7\fi}{}{}

\makeatother


\begin{document}
%\pagestyle{reportpage}
\pagenumbering{roman}
\setcounter{page}{1}

\cleardoublepage
\renewcommand{\baselinestretch}{1.25}
\small\normalsize

\tableofcontents

\renewcommand{\baselinestretch}{1}
\small\normalsize

\cleardoublepage

\pagenumbering{arabic}
\setcounter{page}{1}

\chapter[First Chapter]{The first chapter has a long title that only is shown as the chapters headline}
Some irelevant text.

\section[Foo]{Long foo section}

\subsection[Foo]{Long foo subsection}


\cleardoublepage

more irrelevant text.

\end{document}

在此处输入图片描述

相关内容