3

Dear TeX(stackexchange) community, I'm trying to create automatically my own TOCs: using \keyword command I define the entries of my own toc list so that each belonging to same chapter will be printed at beginning of chapter using \listofkeywords.

There is a problem for first chapter: no .key1 file created and no toc printed. That's my MWE:

\documentclass[oneside,11pt,fleqn]{memoir}
\usepackage{titletoc}
\setcounter{tocdepth}{0}
\usepackage[compact,toctitles]{titlesec}
\titleformat{\chapter}[block]
{\huge}{\chaptertitlename\ \thechapter:\ }{0pt}{\huge}[\addtocounter{cherrychapter}{1}]
\title{H\&R}
%%% COUNTERS
\newcounter{cherrychapter}%[chapte]
\setcounter{cherrychapter}{0}
\newcommand\keyword[1]{%
\noindent%
{#1}%
\phantomsection% comment out if hyperref is noy used
\thecherrychapter\thechapter\addcontentsline{key-\thecherrychapter}{figure}{#1}
}
\makeatletter
\newcommand\listkeywordsname{KEYWORDS}
\newcommand\listofkeywords{%
\thecherrychapter\thechapter
\section*{\listkeywordsname}\@starttoc{key-\thecherrychapter}
\thecherrychapter\thechapter}
\makeatother
\begin{document}%BEGIN
\maketitle
\tableofcontents*
\part{A}
\chapter{aa}
\listofkeywords
\keyword{bb}
\section{aaa}
\keyword{bbb}
\subsection{aaaa}
\keyword{bbbb}
\chapter{cc}
\listofkeywords
\keyword{dd}
\section{ccc}
\keyword{ddd}
\subsection{cccc}
\keyword{dddd}
\end{document}

Thank you for aid. Best regards.

4
  • 1
    Why use \addcontentsline? Are you willing to write your keywords to a file and read that back in?
    – jon
    Feb 21, 2017 at 21:37
  • I don't get this too. The keywords are not sorted in the output
    – user31729
    Feb 21, 2017 at 21:42
  • The issue depends on the fact that memoir redefines the internal \@starttoc. I think a more robust solution will be provided by the package glossaries tex.stackexchange.com/questions/89107/… Feb 21, 2017 at 22:08
  • The usage of titletoc and memoir's emulation of tocloft etc. features is also doubtful
    – user31729
    Feb 21, 2017 at 22:12

2 Answers 2

2

The costepping of cherrychapter can be done with xassoccnt, provided, this auxiliary counter is needed at all.

The main issue is that memoir does a redefinition of \@starttoc. One possible solution is to store the original LaTeX core definition to \@starttocorig before \documentclass and use that version instead of \@starttoc.

For more than one chapter it will report any number but the last one to be not existing. This looks like an expansion issue, actually, since memoir's version of \@starttoc tries to write to generate the file handle at the end of the document and opens the relevant file then, so #1 expands to the wrong chapter number.

\makeatletter
\let\@starttocorig\@starttoc
\makeatother

\documentclass[oneside,11pt,fleqn]{memoir}

\providecommand{\phantomsection}{}
\usepackage{morewrites}
\usepackage{xassoccnt}
\newcounter{cherrychapter}
\DeclareAssociatedCounters{chapter}{cherrychapter}

\usepackage{titletoc}
\setcounter{tocdepth}{0}
\usepackage[compact,toctitles]{titlesec}
\titleformat{\chapter}[block]
{\huge}{\chaptertitlename\ \thechapter:\ }{0pt}{\huge}%
\title{H\&R}
\newcommand\keyword[1]{%
  \noindent%
  {#1}%
  \phantomsection% comment out if hyperref is not used
  \addcontentsline{key-\thecherrychapter}{figure}{#1}
}
\makeatletter
\newcommand{\listofkeywordsbasic}{%
  \@starttocorig{key-\thecherrychapter}
}
\makeatother
\newcommand\listkeywordsname{KEYWORDS}
\newcommand\listofkeywords{%
  \section*{\listkeywordsname}\listofkeywordsbasic
}



\begin{document}%BEGIN
\maketitle
\tableofcontents*
\part{A}
\chapter{aa}
\listofkeywords
\keyword{bb}
\section{aaa}
\keyword{bbb}
\subsection{aaaa}
\keyword{bbbb}
\chapter{cc}
\listofkeywords
\keyword{dd}
\section{ccc}
\keyword{ddd}
\subsection{cccc}
\keyword{dddd}
\end{document}
0
1

As explained the internal command \@starttoc is redefined by memoir. The redefinition of the command \@starttoc is used to define the user command \newlistof.

To use the possibilities of \newlistof you can do the following:

\documentclass[oneside,11pt,fleqn]{memoir}

\providecommand{\phantomsection}{}

\usepackage{xassoccnt}
\newcounter{cherrychapter}
\DeclareAssociatedCounters{chapter}{cherrychapter}

\newcommand\keyword[1]{#1\phantomsection\addcontentsline{key-\thecherrychapter}{figure}{#1}}

\providecommand\listofkeywords{}
\newcommand\listkeywordsname{KEYWORDS}
\renewcommand\memendofchapterhook{
  \edef\x{\noexpand\newlistof{listofkeywords}{key-\thecherrychapter}{\listkeywordsname}}\x%
}


\begin{document}%BEGIN
\chapter{aa}
\listofkeywords

\keyword{bb}
\section{aaa}
\keyword{bbb}
\subsection{aaaa}
\keyword{bbbb}

\chapter{cc}
\listofkeywords

\keyword{dd}
\section{ccc}
\keyword{ddd}
\subsection{cccc}
\keyword{dddd}

\chapter{dd}
\listofkeywords

\keyword{ee}
\section{eee}
\keyword{eee}
\subsection{eeee}
\keyword{eeeee}
\end{document}

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.