3

I'm using the following structure:

\section[short chapter]{my long long long long section}

I would like to use the long section name in my table of contents, because the short chapter is now used there. Is this possible?

UPDATE

My chapter and section overlap in my header, I'm using a fancyhead in a report style. As a solution I found the structure I described above to make a shorter chapter. That resulted in a shorter table of contents line. Now, I want to use the long long long section for my table of contents.

  • 1
    Welcome, omit the brackets completely. – Johannes_B Mar 21 '16 at 14:57
  • Using section and chapter as terms are confusing here – user31729 Mar 21 '16 at 15:02
  • 1
    @Pim: No, Johannes_B meant omitting the square brackets [short chapter], not the braces, i.e. \section{my long long long long section}, but your question is not really clear, in my point of view – user31729 Mar 21 '16 at 15:51
  • 1
    @Pim: As I stated above: Your question is totally unclear. In the last comment you mention the header problem for the first time, but not in the question itself. Please provide a minimal example that is compilable and that shows your problem. At the moment, this is guess-work only. Providing such an example is usual here, not a single line of code that shows nothing actually – user31729 Mar 21 '16 at 15:59
  • 1
    Either set the toc entry by hand, or the mark by hand: \section[dancing wombat]{the dancing wombat of NY zoo}\addcontentsline{toc}{section}{the dancing wombat of NY zoo} or \section{the dancing wombat of NY zoo}\markright{dancing wombat}. – Johannes_B Mar 21 '16 at 16:07
3

The following might be sufficient for your needs. It provides an additional, optional argument for \section that is used for typesetting the header in a similar way to the interface provided by memoir:

\documentclass[twoside]{report}

\usepackage{lipsum}% Just for this example
\usepackage{xpatch}

\let\oldsection\section
\RenewDocumentCommand{\section}{s o o m}{%
  \markright{}% Clear right mark
  \IfBooleanTF{#1}
    % \section*
    {\IfNoValueTF{#2}
       % \section*
       {\IfNoValueT{#3}
          % \section*{...}
          {\oldsection*{#4}}
       }
       % \section*[.]
       {\oldsection*{#4}%
       \addcontentsline{toc}{section}{\protect\numberline{}#2}%
       \IfNoValueF{#3}
          % \section*[.][..]{...}
          {\markright{#3}}
       }
    }
    % \section
    {\IfNoValueTF{#2}
       % \section
       {\oldsection{#4}}
       % \section[.]
       {\oldsection[#2]{#4}
        \IfNoValueF{#3}
          % \section[.][..]{...}
          {\markright{#3}}
       }
    }
}

\pagestyle{headings}
\sloppy% Just for this example

\begin{document}

\tableofcontents

\chapter{A chapter}
\lipsum[1-20]

\section{A section}
\lipsum[1-20]

\section[ToC entry][Header entry]{Another section}
\lipsum[1-20]

\section*{A starred section}
\lipsum[1-20]

\section*[Starred ToC section]{Another starred section}
\lipsum[1-20]

\end{document}

You can use

\section[*][<ToC>][<header>]{<title>}

Of course, some conditioning is not necessary, as you can't have only the second optional argument and not the first (using the default [..] notation).

  • 1
    I would suggest to use the 'header' optional argument after the mandatory one -- I find two optional arguments always difficult to remember which one stands for which purpose. – user31729 Mar 21 '16 at 18:49
  • Doing as Christian Hupfer suggested (somo as argument definition with the last o for the TOC entry) the author would not need to adjust any \section in his document. – jonas-schulze Mar 22 '16 at 11:37
  • There would most likely be a change required for the sections where the header should be different than the ToC entry anyway. My answer doesn't require the user to make any changes to their sectional commands unless this is the case (since the header-argument is optional). – Werner Mar 22 '16 at 15:37
  • But the header argument is the second optional one. So the first one, which is already set in the authors document, won't apply to the heading but to the TOC entry. This is not what the author is looking for, but the changes needed to your given solution are marginal. – jonas-schulze Mar 22 '16 at 23:30
3

As Johannes_B mentioned, you should try something like this in the preamble:

\let\oldsection\section
\renewcommand{\section}[2][]{\oldsection{#2}\def\currentheading{#1}}

The optional argument will be stored in \currentheading instead and the "default" \section{} will be executed with the long title only. Replace \currentheading by whatever you need to achieve the heading you want.

Edit: to be aware of \section* you should use:

\documentclass{article}

\usepackage{xparse}

\let\oldsection\section
\def\currentheading{}

\RenewDocumentCommand{\section}{som}{%
  \IfBooleanTF{#1}%
  {\oldsection*{#3}}%
  {\oldsection{#3}}%
  \IfValueTF{#2}%
  {\def\currentheading{#2}}%
  {}%
}

\begin{document}


\section[head]{foo}
\section[overwritten]{bar}
\section*{buz}
The current heading should be overwritten ... it is \currentheading.

\end{document}
  • 1
    Well, this is not really what Johannes_B mentioned. And you will get into troubles with \section*{Foo} versions (like being used in \tableofcontents etc.) – user31729 Mar 21 '16 at 16:55
  • Would \usepackage{xparse} and \RenewDocumentCommand{\section}{sO{}m}{\IfBooleanTF#1{\oldsection*{#3}}{\oldsection{#3}}\def\currentheading{#2}\currentheading} fix this? I'm no low-level tex user ... – jonas-schulze Mar 21 '16 at 16:59
  • Well, yes. I suggest to catch the optional argument with som and \IfValueTF{#2}{...}{...} but I leave it to you. – user31729 Mar 21 '16 at 17:00
  • You can use \IfValueT{#2}{...} here if you just want to provide for the true branch (as is done in your example), but I suggest to provide a compilable version, not just fragments, otherwise this post will get to the Low Quality Review Queue very quickly ;-) – user31729 Mar 21 '16 at 17:11
  • Gern geschehen :-P – user31729 Mar 21 '16 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.