1

I have sections with names that follow the format and some of the numbers are longer than others. When the table of contents is created, the portions are not aligned. For example, with the attached MWE, the table of contents looks like this: enter image description here

I would like the table of contents to look like this: enter image description here

I appears as though you cannot insert \tab (e.g. from tabto package) into a table of contents. I tried with titletoc or toctoft packages. Is there another way to achieve this?

% Preamble
\documentclass[11pt]{article}
% Packages
\usepackage{amsmath, titlesec}
\usepackage{lipsum}
\usepackage{titletoc}
\titlecontents{section}[1em]{}{}{}{\titlerule*[1pc]{.}\contentspage}[]

% Document \begin{document} \tableofcontents \newpage \section[PT12345678 Sample Section]{Sample Section} \lipsum[1] \section[PT123456789876543 Second Sample Section]{Second Sample Section} \lipsum[1] \end{document}

rsgny
  • 281
  • Please see the updated answer. – Simon Dispa May 07 '21 at 12:48
  • The solution works. You can see the output from my system. I tried with lualatex and pdflatex. Please try my exact code in a new empty directory. Second step: update your system. – Simon Dispa May 08 '21 at 18:37
  • That the solution works is before your eyes. Are you using a different setup or preamble? You get the wrong output and/or LaTeX error messages? Please expand your question with new information to find a better a solution. – Simon Dispa May 09 '21 at 12:46

2 Answers2

1

UPDATED It can be done using the tabular environment. I defined a new commands with two parameters.

a

% Preamble
\documentclass[11pt]{article}
% Packages
\usepackage{amsmath, titlesec}
\usepackage{lipsum}
\usepackage{titletoc}
\titlecontents{section}[1em]{}{}{}{\titlerule*[1pc]{.}\contentspage}[]

\newlength{\colw} \setlength{\colw}{10em} %adjust to fit the longest entry

\newcommand{\ctocx}[2]{%% entry to TOC with two parameters \protect \begin{tabular}{ll} \parbox{\colw}{#1} &#2 \ \end{tabular}}

% Document \begin{document} \tableofcontents
\newpage \section[\ctocx{PT12345678}{Sample Section}]{Sample Section} \lipsum[1] \section[\ctocx{PT123456789876543}{Second Sample Section}]{Second Sample Section} \lipsum[1]

\end{document}

Simon Dispa
  • 39,141
  • Thank you, but the MWE was overly simplified. There will be anywhere from 50-75 different sections from a list of about 125 possible sections. Your proposed approach would require 125 different custom commands similar to \ctoci and that would be hard to maintain because the library of sections that this pulls from changes frequently (e.g. new editions of sections with new section numbers). And the issue with trying to use one command with parameters is that anything within the [ ] after \section cannot contain parameters (at least that is what seems to be the case). – rsgny May 07 '21 at 05:07
  • @rsgny I this is your real problem, its is better define a command with two arguments.. See the updated answer. – Simon Dispa May 07 '21 at 12:48
  • This solution does not work, for the same reason that a simple \tab (using tabto package) doesn't work - there are restrictions on what can be inside the \section – rsgny May 08 '21 at 17:04
  • @rsgny That the solution works is before your eyes. Are you using a different setup or preamble? You get the wrong output and/or LaTeX error messages? Please expand your question with new information to find a better a solution. – Simon Dispa May 09 '21 at 12:45
1

Here, I create a macro \Plabel that sets the argument, left aligned, in a 1.5 inch box, essentially acting like a tab.

% Preamble
\documentclass[11pt]{article}
% Packages
\usepackage{amsmath, titlesec}
\usepackage{lipsum}
\usepackage{titletoc}
\titlecontents{section}[1em]{}{}{}{\titlerule*[1pc]{.}\contentspage}[]
\newcommand\Plabel[1]{\makebox[1.5in][l]{#1}}
% Document
\begin{document}
\tableofcontents
\newpage
\section[\Plabel{PT12345678} Sample Section]{Sample Section}
\lipsum[1]
\section[\Plabel{PT123456789876543} Second Sample Section]{Second Sample Section}
\lipsum[1]
\end{document}

enter image description here