1

Details:

  • I want my \tableofcontents to actually appear as table
  • i am using latex
  • i traced what the command \tableofcontents does, and in the end,
    i found that the \contentsline macro chain do not save the \thechapter and \chaptername as different entities
  • ... so, the modification of it to table seems difficult to me atm
  • this is also mentioned in the manual of package[a] tocloft at page 24)

For the purposes at hand it is generally impossible to treat the typesetting of a title and its number separately, as both are bundled into the <title> argument within \contentsline.

MWE

Minimum Working Examples (as asked here):

Default:

\documentclass{report}
\begin{document}

\tableofcontents

\chapter{First sdddsssss % sssssssssssssssssssss ssssssssssssss ssssssss sssssss % ssssssssss sssssssssss sssssssssssssss ssssss ssssssss % sssssssssssss sssssssss} \chapter{Second}

\end{document}

Default tableofcontents

Target

(well, somewhat! as the package tabularray, nice name, doesn't seem orthogonal in its implementation of functions to me, so, i am still unable to properly define all the things in it, but this will work)

\documentclass{report}
\usepackage{tabularray}
\begin{document}

%\begin{tblr}{| m{1em} | m{9cm} | m{2cm} |} \begin{tblr}{| c | m{9cm} | m{2cm} |} \hline \bfseries thechapter & \bfseries chaptername & \bfseries comments \ \hline 1 & First sdddsssss % sssssssssssssssssssss ssssssssssssss ssssssss sssssss % ssssssssss sssssssssss sssssssssssssss ssssss ssssssss % sssssssssssss sssssssss & \ \hline 2 & Second & \ \hline \end{tblr} \end{document}

Wanted "tableofcontents"

PS:

On the orthogonality of tabularray:

I wanted to specify following options:

  • horizontal arrangement (l,c,r)
  • vertical arrangement (t,m,b,h,f)
    update: i think t,m,b concern with relative alignment of the specified base?lines of the columncells across their whole row; whereas h,f concern with the absolute alignment in individual cell
  • columnwidth
  • special vertical alignment for the header row
  • row formatting for the header
  • yes, sorry for that, i was trying to keep it minimal for the default, but was testing the line auto wrapping for the target, and while posting it here, forgot to make those both consistent. I will sync this in the code example above. should i update the screenshot too? – user8395964 Dec 10 '22 at 15:35

1 Answers1

0

The perfectly imperfect solution I used to get the toc in a table was:

(yes, changed format is intentional i.e. reduced the rules a bit, added chapter children and page numbers for completeness sake)

\documentclass{report}

% Table spanning pages \usepackage{longtable}

\usepackage{ifthen}

% Vertically padded hlines or rules % \usepackage{booktabs}

% Padded Rule \newcommand{\prule}{\noalign{\smallskip}\hline\noalign{\smallskip}}

% Extract (.sty, \usepackage): Modify toc commands for table \renewcommand{\numberline}[1]{#1 & }%\bfseries} \def\contentsline#1#2#3#4{\gdef@contentsline@destination{#4}% \ifthenelse{\equal{#1}{chapter}}{\ \prule}{ \ } #2 & #3 & } \newcommand{\mytoc}{ \begin{longtable}{ p{5mm} | p{6.5cm} | p{1cm} | p{2cm} } % \toprule \bfseries sr & \bfseries chaptername & \bfseries pg & \bfseries comments \input{\jobname.toc} \ % chktex "Command terminated with space" is wrong (VSCodium: LaTeX Workshop) % Do NOT use \prule{} \prule \end{longtable} }

\begin{document}

% Run this command to update the toc file, then recomment again % \tableofcontents

\mytoc{}

\chapter{First sdddsssss % sssssssssssssssssssss ssssssssssssss ssssssss sssssss % ssssssssss sssssssssss sssssssssssssss ssssss ssssssss % sssssssssssss sssssssss} \section{Child} \section{Child} \chapter{Second}

\end{document}

Conditional Rule

This is "perfectly imperfect" because:

  • it works while nothing else did (hence perfect)
  • indentation of sections need to be re-implemented too
  • the packages dealing with toc modification like bookmark etoc etc likely won't work with this
  • contents enteries no longer clickable/linked (hyperref package)

Note 2023.05.14:

  • Disable auto "Clean" options in tex-related build tasks like in "LaTeX Workshop" extension in VSCodium (honestly though, if compiling in TXS, then extensions TeXLab and vscode-pdf are enough, just disable this stupid latex-workshop extension with stupid chktex)

    old point: though breeze in TeXStudio (TXS); but with latexmk and chktex in VSCodium, running \tableofcontents and commenting it out becomes arduous task. Tried with following inside \mytoc definition to avoid that, but none worked: \makeatletter\@starttoc{toc}\makeatother or Generating .toc file without generating a ToC
"latex-workshop.latex.autoBuild.run": "never",
"latex-workshop.latex.autoBuild.cleanAndRetry.enabled": false,

Changes since original answer:

  • Fix (trailing vertical rule in end): Move the \makeat* pair outside the table
  • Dev: Remove @ using command, hence also the \makeat* pair
  • New (conditional1 hlines): hline before chapter (todo: "logical or" with "part") to group children (*sections)
  • New: Page column for completeness of answer for others
  • New: \prule and \mytoc for ease of use

Any suggestions, bug fixes or improvements welcome


Footnotes:

[1]: I used the Wikibook pdf - which remains outdated. I later found via LaTeX conditional expression that 'obsolete ifthen package' - This same question is also indirectly linked via footnotes in the Wikibook on web.


P.S.: Aaaah, just found from md-advanced-help that

  • language tags for syntax highlighting on SE are prefixed with "lang", hence becomes lang-<lang>
  • which's different than github/gitlab's <lang> tag :confused:
  • so, tex tags were ignored, rather, the lang-tex tag seems to be specified across this tex.se site... :facepalm:

Also, don't change the "Edit summary" to enable appending the changes to previous revision if allowed. This "appension" is likely constrained, so, doesn't work always.

  • I can't help too much, but: (1) you shouldn't need a separate style file, just surround that portion with \makeatletter/\makeatother. (2) booktabs abhors vertical lines in tables, and explicitly breaks them. – Teepeemm Feb 06 '23 at 14:42