1

In the same way that creating a standard table of contents makes use of levels (chapters, sections, subsections, etc.) Is there a way to create a custom function that that mimics this using tocloft? I have played around with modifications to #8 in this list of ways to customize toc to get the list of lists effect as well as this more in-depth guide on the behavior of the functions with no luck.

I am envisioning some custom function of the form:

\function{the chapter}{the section} 

Called like this in the code:

\function{pine}{cone}

\function{pine}{wood}

\function{foo}{bar}

Generates a table of contents something like this when \listofWHATEVER is called:

  1. Pine

    • cone

    • wood

  2. foo

    • bar

I know there are examples of doing this using custom environments or brute-forcing it using enumerate/itemize. But I'm hoping there is some way to do it with a custom command.

I've searched around and haven't seen a solution for this, it's possible I have been using the wrong keywords or simply missed it. Examples or links to a solution would be much appreciated.

  • Welcome to TeX.SX! You can use tocloft. Please read the documentation or guides like this for that purpose and then ask a specific question. – TeXnician Mar 10 '17 at 6:02
  • I thought it was clear enough, but I have added some clarifications. – Joe Kissling Mar 10 '17 at 14:34
  • Well, what should happen when \function{pine}{sawmill} is called after \function{foo}{bar}? – user31729 Mar 10 '17 at 18:59
  • It should not behave any differently, the order does not matter – Joe Kissling Mar 10 '17 at 19:08
  • @JoeKissling: Well, it should add 3. Pine and sawmill then, but not 'sorted' into the first Pine appearance? – user31729 Mar 10 '17 at 19:18
1

A preliminary version without hyperlinking etc. of page numbers.

The code stores the individual keywords and their values in expl3 sequences and writes the values to a .foo ToC file at the end of the document, which is then typeset with \listofwhatever.

The keywords are set with chapter like ToC - entries, the values with section like entries, tocloft is used to disable pagenumbers for the keywords in the .foo file.

\documentclass{book}

\usepackage{xparse}

\newcounter{keyword}
\newcounter{keywordvalue}



\usepackage{tocloft}


\makeatletter
\newcommand{\listofwhatever}{%
  \begingroup
  \clearpage
  \section*{List of Whatever}
  \@starttoc{foo}
  \endgroup
}
\makeatother

\ExplSyntaxOn

\seq_new:N \g_full_keywords_seq

\NewDocumentCommand{\addkeyword}{m}{%
  \seq_if_in:NnF \g_full_keywords_seq {#1} {%
    \seq_new:c {g_#1_local_keywords_seq }
    \seq_new:c {g_#1_local_pagenumber_seq }
  }
  \seq_gput_right:Nn \g_full_keywords_seq {#1}
  \seq_gremove_duplicates:N \g_full_keywords_seq
}

\NewDocumentCommand{\function}{m+m}{%
  \addkeyword{#1}%
  \seq_if_exist:cT {g_#1_local_keywords_seq } {
    \seq_gput_right:cx {g_#1_local_keywords_seq } {#2;;;;;\thepage}
  }
}

\NewDocumentCommand{\generatelistof}{}{%
  \addtocontents{foo}{\protect\renewcommand{\protect\cftsecdotsep}{\cftnodots}}%
  \addtocontents{foo}{\protect\cftpagenumbersoff{chapter}}
  \seq_map_inline:Nn \g_full_keywords_seq {%
    \refstepcounter{keyword}%
    \addcontentsline{foo}{chapter}{\protect\numberline{\thekeyword}##1}%
    \seq_map_inline:cn {g_##1_local_keywords_seq} {%
      \seq_set_split:Nnn \l_tmpa_seq {;;;;;} {####1}
      % Use this without hyperref
      \addtocontents{foo}{\protect\contentsline{section}{\textbullet\protect\quad \seq_item:Nn \l_tmpa_seq {1}}{\seq_item:Nn \l_tmpa_seq {2}}}
      % Use this with hyperref
      %\addtocontents{foo}{\protect\contentsline{section}{\textbullet\protect\quad \seq_item:Nn \l_tmpa_seq {1}}{\seq_item:Nn \l_tmpa_seq {2}}{}{}}
    }
  }
}



\ExplSyntaxOff

\usepackage{blindtext}



\AtEndDocument{%
  \generatelistof
}



\begin{document}
\listofwhatever
\tableofcontents

\chapter{Foo}
\section{Foo section}

\function{pine}{cone}
\function{pine}{wood}

\blindtext[10]
\function{foo}{bar}

\function{pine}{sawmill}

\function{foo}{Hello Darkness my old friend}



\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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