Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Using the following code I could put the ToC lines in the center in beamer:

\begin{frame}
  \begin{center}
  \tableofcontents
  \end{center}
\end{frame}

But how to make these ToC lines left aligned and still in the center?

share|improve this question
1  
You can only put the \tableofcontents inside a minipage-environment which can be centered via \centering. –  Marco Daniel Dec 10 '11 at 17:12
    
The question tex.stackexchange.com/questions/47702/… contains a fairly simple solution (manually calculated though) –  naught101 May 2 '12 at 0:27
add comment

1 Answer

up vote 4 down vote accepted

A small calculation could advance the margins sufficiently to obtain the centered & left-aligned \tableofcontents display.

enter image description here

\documentclass{beamer}% http://ctan.org/pkg/beamer
\newsavebox{\longestsec}% Box to save longest sectional heading
\begin{document}

\begin{frame}
  \frametitle{Table of contents}
  \begin{lrbox}{\longestsec}Last section in the presentation\end{lrbox}% Capture longest title
  \setlength{\leftskip}{\dimexpr.5\textwidth-.5\wd\longestsec\relax}% Advance left margin accordingly
  \tableofcontents
\end{frame}

\section{First section}
​\begin{frame}
  \frametitle{First slide}
  This is a slide.
\end{frame}

\section{Last section in the presentation}% Longest sectional title
\begin{frame}
  \frametitle{Another slide}
  This is another slide
\end{frame}
\end{document}

​The reason for this round-about way is because beamer collapses the spacing when putting \tableofcontents within a minipage or varwidth (from the varwidth package).


Here is an automated way of capturing the width of the longest section:

\documentclass{beamer}% http://ctan.org/pkg/beamer
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\makeatletter
\newlength{\secnamelength}
\newsavebox{\longestsec}% Box to save longest sectional heading
\patchcmd{\beamer@section}% <cmd>
  {\beamer@savemode}% <search>
  {\begin{lrbox}{\longestsec}#1\end{lrbox}%
   \ifdim\wd\longestsec>\secnamelength\relax\setlength{\secnamelength}{\wd\longestsec}\fi%
   \beamer@savemode}% <replace>
  {}{}% <success><failure>
\AtEndDocument{% http://tex.stackexchange.com/q/137495/5764
  \immediate\write\@auxout{\global\secnamelength=\the\secnamelength}%
}
\makeatother
\begin{document}

\begin{frame}
  \frametitle{Table of contents}
  \setlength{\leftskip}{\dimexpr.5\textwidth-.5\secnamelength\relax}% Advance left margin accordingly
  \tableofcontents
\end{frame}

\section{First section}
​\begin{frame}
  \frametitle{First slide}
  This is a slide.
\end{frame}

\section{Last section in the presentation}% Longest sectional title
\begin{frame}
  \frametitle{Another slide}
  This is another slide
\end{frame}
\end{document}

The above stores the length of the longest section in \secnamelength that is written to the .aux at the end of the document. A patch of \beamer@section (with the aid of etoolbox is necessary to grab the maximum length of a section name and drop it into the .aux \AtEndDocument so it can be retrieved in a successive run.

share|improve this answer
1  
Great. But is it possible to automatically find out the longest sectional title? –  Z.H. Dec 16 '11 at 12:07
    
Is this still a valid solution? Doesn't work in my texlive 2012 ... –  BandGap Oct 11 '13 at 16:12
1  
@Z.H.: A little late, but added the automation procedure. –  Werner Oct 12 '13 at 18:05
    
@BandGap: Yes, and I've just added some automation. What is the problem you're experiencing with the above MWE? –  Werner Oct 12 '13 at 18:06
    
@Werner Thanks. I love this solution. –  Z.H. Oct 13 '13 at 8:28
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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