Misaligned identation in TOC

149 Views Asked by At

When the section number is at least 10, then the subsection numbers get misaligned with the section titles in the TOC in a beamer presentation in LaTeX. Futhermore, the spacing between the (sub)subsection numbers and the (sub)subsection titles gets smaller. I am looking for a generic way to fix this regardless of whether the section number is at least 10, at least 100, or less than 10. In this case I would not have to tweak these things manually. Is there a way to do this?

\documentclass[9pt,trans,xcolor={table},envcountsect]{beamer} % Plain

\usefonttheme[onlymath]{serif}
\usetheme[shownavsym,right]{Aalborg}
\setbeamertemplate{navigation symbols}{\insertframenavigationsymbol \insertsectionnavigationsymbol \insertbackfindforwardnavigationsymbol} % Nav symbols
\setbeamertemplate{section in toc}[sections numbered] % Automatic enumeration of sections
%
\makeatletter
\newcommand{\ShiftSectionNumber}[1]{%
\beamer@tocsectionnumber=\numexpr#1+\beamer@tocsectionnumber}
\makeatother
%
\setbeamertemplate{subsection in toc}{\leavevmode\leftskip=3.14em\rlap{\hskip-2em\inserttocsectionnumber.\inserttocsubsectionnumber}\inserttocsubsection\par} % Automatic enumeration of subsections. WITH indentation of subsection numbers (\leftskip changed from 3.2em to 3.14em)
%
\setbeamertemplate{subsubsection in toc}{\leavevmode\leftskip=6.9em\rlap{\hskip-3em\inserttocsectionnumber.\inserttocsubsectionnumber.\inserttocsubsubsectionnumber}\inserttocsubsubsection\par} % Automatic enumeration of subsubsections. WITH indentation of subsubsection numbers (\leftskip changed from 3.14em to 6.9em, \hskip changed from -2em to -3em)
%
\begin{document}
%
\begin{frame}%[allowframebreaks]
    \frametitle{Overview}
    \tableofcontents
\end{frame}
%
\def \numbershift {0} % Increasing of enumeration of section numbers and theorems
\ShiftSectionNumber{\numbershift} % Increasing the enumeration of section numbers
\addtocounter{section}{\numbershift} % Theorem numbers follow section numbers
%
%
\section{First Section (1-digit section number)} %What happens if a very long section title is used?} % https://tex.stackexchange.com/questions/89708/indent-subsections-in-beamers-toc
\subsection{First Subsection}
\subsubsection{First Subsubsection}

\ShiftSectionNumber{8}
\section{Second Section (2-digit section number)} % https://imathworks.com/tex/tex-latex-beamer-how-to-get-subsection-indent-with-a-numbered-toc/
\subsection{Second Subsection}
\subsubsection{Second Subsubsection}

\ShiftSectionNumber{89}
\section{Third Section (3-digit section number)}
\subsection{Third Subsection}
\subsubsection{Third Subsubsection}
%
\begin{frame}[<+->]{Misaligned identation in TOC}
    Hello.
\end{frame}

\end{document}
1

There are 1 best solutions below

8
samcarter_is_at_topanswers.xyz On

You could measure the width of the last section and align the numbers according to this width:

\documentclass[9pt,trans,xcolor={table},envcountsect]{beamer} % Plain

\usefonttheme[onlymath]{serif}
\usetheme[shownavsym,right]{Aalborg}
\setbeamertemplate{navigation symbols}{\insertframenavigationsymbol \insertsectionnavigationsymbol \insertbackfindforwardnavigationsymbol} % Nav symbols


\begin{document}


\makeatletter

\newlength{\secnumwidth}
\settowidth{\secnumwidth}{\the\beamer@sectionmax.\space}
\newlength{\subsecnumwidth}
\settowidth{\subsecnumwidth}{\the\[email protected]\space\space}
\newlength{\subsubsecnumwidth}
\settowidth{\subsubsecnumwidth}{\the\[email protected]}

\setbeamertemplate{section in toc}{%
  \leavevmode%
  % prevents the period to be printed with the first/last section option
  \ifnum\beamer@tempcount>\beamer@toclastsection
  \else
  \ifnum\beamer@tempcount>0
    \makebox[\secnumwidth][l]{\inserttocsectionnumber.}%
  \fi\fi%
  \inserttocsection\par%
}

\setbeamertemplate{subsection in toc}{%
\leavevmode\leftskip=\secnumwidth\rlap{\inserttocsectionnumber.\inserttocsubsectionnumber}\hspace*{\subsecnumwidth}\inserttocsubsection\par}

\setbeamertemplate{subsubsection in toc}{%
\leavevmode\leftskip=\dimexpr\subsecnumwidth+\secnumwidth\rlap{\inserttocsectionnumber.\inserttocsubsectionnumber.\inserttocsubsubsectionnumber}\hspace*{\subsubsecnumwidth}\inserttocsubsubsection\par}
\makeatother


\begin{frame}%[allowframebreaks]
    \frametitle{Overview}
    \tableofcontents
\end{frame}

\section{Section}

\subsection{Subsection}
\subsubsection{Subsubsection}

\section{Section}
\section{Section}
\section{Section}
\section{Section}
\section{Section}
\section{Section}
\section{Section}
\section{Section}
\section{Section}

\subsection{Subsection}
\subsubsection{Subsubsection}

\begin{frame}[<+->]{Misaligned identation in TOC}
    Hello.
\end{frame}

\end{document}

enter image description here