0

I am used to redefine the section command (in a beamer document if it matters) as follows:

\let\oldsection\section
\renewcommand{\section}[1]{
  \oldsection{#1}
  % some code that generates a pretty frame with title section and all
}

In doing so, the call to \oldsection deals with the non visible stuffs and I only concentrate on the good look I want to give to my frame.

Lately I wanted to use the short title option for my toc, so I decided to adapt that code:

\let\oldsection\section
\renewcommand{\section}[2][]{
  \oldsection[#1]{#2}
  % some code that generates a pretty frame with title section and all
}

Surprisingly (for me), it does not work. The toc still uses the full title. What is happening under the hood here? How can I adapt the code to have the behaviour I want?

Pece
  • 381

2 Answers2

2

Disclaimer: Where possible you should use the class specific stuff (so the beamer code provided by @samcarter in this case). The following answer is just a quick writeup on how one should do such redefinitions to keep all the possibilities of a macro like \section.


You should use \LetLtxMacro from the letltxmacro package for macros taking an optional in general. For the \section macro this doesn't seem necessary (at least with the article class) as it isn't defined with something like \newcommand.

Still you'll have to make sure that you test for an optional star and the optional argument. The optional argument shouldn't be passed on as is, as with the default empty argument you'll only get empty entries in the ToC. The following does the test for a star using the \@ifstar macro. It also uses & as the default content of the optional argument to test whether it was used at all. One could do this differently using \@ifnextchar[, but the approach here is easier to code.

\documentclass[]{article}

\let\oldsection\section
\makeatletter
\renewcommand\section
  {%
    \@ifstar
      {\sectionauxB}
      {\sectionauxA}%
  }
\newcommand\sectionauxA[2][&]
  {%
    \ifx&#1%
      \oldsection{#2}%
    \else
      \oldsection[#1]{#2}%
    \fi
  }
\newcommand\sectionauxB[1]
  {%
    \oldsection*{#1}%
  }
\makeatother

\begin{document}
\tableofcontents
\section{Test}
\section[Tes]{Test}
\end{document}

Alternatively using \@ifnextchar:

\documentclass[]{article}

\let\oldsection\section
\makeatletter
\renewcommand\section
  {%
    \@ifstar
      {\sectionauxD}
      {\sectionauxA}%
  }
\newcommand\sectionauxA
  {%
    \@ifnextchar[
      {\sectionauxB}
      {\sectionauxC}%
  }
\@ifdefinable\sectionauxB
  {%
    \long\def\sectionauxB[#1]#2{\oldsection[#1]{#2}}%
  }
\newcommand\sectionauxC[1]
  {%
    \oldsection{#1}%
  }
\newcommand\sectionauxD[1]
  {%
    \oldsection*{#1}%
  }
\makeatother

\begin{document}
\tableofcontents
\section{Test}
\section[Tes]{Test}
\end{document}
Skillmon
  • 60,462
2

enter image description here

I would not mess with the section definition in beamer, just use the build in mechanism that let's you automatically insert a section page. If you don't like the default layout of teh section page, you can modify it with \setbeamertemplate{section page}{some code that generates a pretty frame with title section and all}

\documentclass{beamer}

\AtBeginSection[]{
    \begin{frame}[plain]
    \usebeamertemplate{section page}
    \end{frame}
}    

\begin{document}
\section{section name}

\begin{frame}
content...
\end{frame}
\end{document}