0

My document uses titles like the following all over the place.

\section[short]{longlonglong}

The short title is used in page headers and the table of contents. The longlonglong title is only used at the section title itself.

How can I use the longlonglong title in the table of contents? I'd like to keep the page headers in thier current form.

Thank you!

Update:

\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}
David Carlisle
  • 757,742
  • 1
    you haven't given a usable example so it is hard to help. Some document classes allow the three versions (heading, toc and page head) to be specified separately, other classes only allow two of them and require some changes to make the toc use teh long form, but it depends on which document class you are using. – David Carlisle Mar 30 '18 at 13:36
  • Thank you! I'm using the document class report. I'm going to update my example code. – user159521 Mar 30 '18 at 13:41
  • Which document classes allow three versions, and how can I specify them? – user159521 Mar 30 '18 at 13:57
  • 1
    @user159521: You're mentioning \section but now it is \chapter? –  Mar 30 '18 at 13:57
  • @user159521 The memoir class (which encompasses the book, report and article classes) provides for different entries in the ToC, header, and main text. Read chapter 6 "Document divisions" in the manual >texdoc memoir – Peter Wilson Mar 30 '18 at 17:52

2 Answers2

2

Either redefine \@chapter or use a patch in order to modify the code that writes the ToC entry, i.e. \addcontentsline.

report uses

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

so #1 means the optional argument, but #2 is needed here.

The same is basically true for \@sect, which uses #7, which must be replaced by #8.

The code below uses a switch in order to modify the entry:

Say \uselongtitletrue to enable the long titles in the toc but keep the short ones in the header and \uselongtitlefalse in order to use the short ones.

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

enter image description here

  • Thank you! It appears to be the answer that I'm looking for :) I guess I need to redefine the commands for sections and subsections the same way. – user159521 Mar 30 '18 at 14:06
  • @user159521: I've added the version for \section etc. as well –  Mar 30 '18 at 14:23
  • Thank you! Unfortunately there is a package that I use for bib-infos and it is refusing to be compatible with your solution. It worked well in my test-file. – user159521 Mar 30 '18 at 14:50
  • @user159521: That's why you should provide the relevant information. Now my solution is useless –  Mar 30 '18 at 14:51
2

I would not mess with the toc, but simply not set a short version, but change the title for the headline e.g. with \chaptermark.

Unfortunate your code does not contain any headers to test, so I used the fancyhdr instead.

\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}
  • Believe me, manipulating ToC is fun –  Mar 30 '18 at 14:40
  • @ChristianHupfer Oh right, there is even a space left on my list of "fun things to do", just below the entry "fighting with an hungry lion" – samcarter_is_at_topanswers.xyz Mar 30 '18 at 14:49
  • Actually I wasn't joking ... –  Mar 30 '18 at 14:56
  • Thank you. Adding chaptermarks seems to solve my problem very well. I think manipulation the ToC can be fun. At least I could learn a lot about the inner workings of latex and its packages. But the top priority item on my funny-ToDo-list is to help finishing my girlfriends thesis ;) – user159521 Mar 30 '18 at 14:57
  • @ChristianHupfer I know it can be fun https://tex.stackexchange.com/a/303968/36296, but the level of fun strongly correlated with the use document class and if the used packages are nice :) – samcarter_is_at_topanswers.xyz Mar 30 '18 at 15:13
  • @samcarter: I already voted for that ;-) And it is using etoc ... well, I referred to the deep internals of ToCs actually which are quite funny –  Mar 30 '18 at 15:15