3

Here is a method for putting the author name before the title of a chapter in the ToC. Add author before chapter title in ToC, I mean the answer https://tex.stackexchange.com/a/110256/80224.

This works perfectly fine. But suppose one author writes multiple chapters, and we would like the toc to look something like this :

Kafka
The Rejection...........7
The Judgement......77

Maupassant
Love..............15
Pierrot...........33

Tolstoy
Three questions....40.

That is, even if the works by a particular author are not in order, they are classed under the same Author in the ToC, with no repetitions of Author.

How should I modify the code given here https://tex.stackexchange.com/a/110256/80224?

Here is the code:

\documentclass[10pt,twoside]{memoir}
\usepackage[german]{babel}
\usepackage{blindtext}
\usepackage[ansinew]{inputenc} 

\renewcommand{\thesection}{\arabic{section}}
\setcounter{secnumdepth}{3} % Subsection mit Zähler (1.1) versehen

%%
% Kolumnentitel
%%
\renewcommand{\chaptermark}[1]{ \markboth{#1}{}  } % Stil der Kopfzeile zurücksetzen
\renewcommand{\sectionmark}[1]{ \markright{#1}{} } % Stil der Kopfzeile 

% Page style
\makepagestyle{mystyle}
\makeevenhead{mystyle}{\thepage}{\small\chapterauthor}{}
\makeoddhead{mystyle}{}{\slshape\leftmark}{\thepage}
\pagestyle{mystyle}

%%
% Inhaltsverzeichnis
%%
\renewcommand*{\cftchapterdotsep}{\cftdotsep}
\settocdepth{chapter}
\renewcommand{\cftchapterfont}{\normalfont}
\renewcommand{\cftchapterpagefont}{\normalfont}

\makeatletter
\DeclareRobustCommand\authortoctext[1]{%
{\addvspace{10pt}\nopagebreak\leftskip0em\relax
\rightskip \@tocrmarg\relax
\noindent\itshape#1\par\addvspace{-7pt}}}
\makeatother
\newcommand\authortoc[1]{%
  \gdef\chapterauthor{#1}%
  \addtocontents{toc}{\authortoctext{#1}}}

%%
% Dokumentenbeginn
%%
\begin{document}
\tableofcontents* 
\authortoc{Walter von der Vogelweide}
\chapter{Ich sass uf eynem Steine?}
\section{Und dachte Bein mit Beine}
\Blindtext\Blindtext\Blindtext\Blindtext

\authortoc{Hartman von Aue}
\chapter[Was auch immer]{So gebt nur mehr und immer mehr}
\section{Moralische Quellen der Irrationalitat}
\Blindtext\Blindtext\Blindtext\Blindtext
\end{document}
  • Please include the code you are interested in here. If that answer gets edited, this question becomes useless. I'm not sure you want a ToC at all. This looks more like an index to me. – cfr Jul 26 '15 at 15:04
  • As you already figured out: Adding the author name with \addtocontents is not difficult, grouping the chapters for the same author is a problem! Are you sticking to the code above? – user31729 Jul 26 '15 at 15:30
  • No, I am not sticking to the code above. I would only like to have a toc which looks like the form I have shown. – usr203050 Jul 26 '15 at 16:29
  • @usr203050: Ok, I have some almost working version, but memoir is a little bit tricky ;-) – user31729 Jul 26 '15 at 16:34
  • Please show me anything, may be I can adapt it somehow... :-) – usr203050 Jul 26 '15 at 16:35
2

Well, this is somewhat a hack...

The ordered appearance of \chapter in a ToC is 'messy' considered for the request, so I stored the contentslines in a Atoc, Btoc etc. file, where each author get's his own file, however only with 26 authors. If more is to be done, alphalph package will help ;-)

The \chapter macro does no longer write an entry to the ToC, but this can changed rather easily back to the defaults.

In the end (well, at the beginning actually), the \authortableofcontents collects all the individual Atoc etc. files and shows them a row.

The vertical spacing is wrong, most likely.

\documentclass[10pt,twoside]{memoir}


\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc} 



\renewcommand{\thesection}{\arabic{section}}
\setcounter{secnumdepth}{3} % Subsection mit Zähler (1.1) versehen




%%
% Kolumnentitel
%%
\renewcommand{\chaptermark}[1]{ \markboth{#1}{}  } % Stil der Kopfzeile zurücksetzen
\renewcommand{\sectionmark}[1]{ \markright{#1}{} } % Stil der Kopfzeile 

% Page style
\makepagestyle{mystyle}
\makeevenhead{mystyle}{\thepage}{\small\chapterauthor}{}
\makeoddhead{mystyle}{}{\slshape\leftmark}{\thepage}
\pagestyle{mystyle}

%%
% Inhaltsverzeichnis
%%
\renewcommand*{\cftchapterdotsep}{\cftdotsep}
\settocdepth{chapter}
\renewcommand{\cftchapterfont}{\normalfont}
\renewcommand{\cftchapterpagefont}{\normalfont}


\usepackage{blindtext}


%%%%% My additions and changes
\usepackage{xpatch}
\usepackage{morewrites}
\usepackage{totcount}
\usepackage{etoolbox}

\listgadd{\authortoclist}{}



\newtotcounter{authorcounter}

\newcounter{dummycounter}
\newcounter{realposition}
\newcommand{\grabitem}[2]{%
  \stepcounter{dummycounter}
  \ifstrequal{#1}{#2}{%
    \setcounter{realposition}{\number\value{dummycounter}}
    \listbreak
  }{%
  }%
}

\makeatletter
\DeclareRobustCommand\authortoctext[1]{%
{\addvspace{10pt}\nopagebreak\leftskip0em\relax
\rightskip \@tocrmarg\relax
\noindent\itshape#1\par\addvspace{-7pt}}}



\newcommand\authortoc[1]{%    
  \xifinlistcs{#1}{authortoclist}{%  Does have been there already?
    \setcounter{dummycounter}{0}
    \forlistloop{\grabitem{#1}}{\authortoclist}
    \typeout{found at \number\value{realposition}}
  }{%
    \gdef\chapterauthor{#1}%  Nope, new author name. 
    \refstepcounter{authorcounter}%
    \setcounter{realposition}{\number\value{authorcounter}}
    \listgadd{\authortoclist}{#1}
    \addtocontents{\Alph{realposition}toc}{\authortoctext{#1}}
  }%
}


%%% Kicking the chapter to the toc entry
\xpatchcmd{\@chapter}{%
  \addcontentsline{toc}{chapter}{% 
    \protect\chapternumberline{\thechapter}\f@rtoc}%
}{%
  \addcontentsline{\Alph{realposition}toc}{chapter}{%
    \protect\chapternumberline{\thechapter}\f@rtoc}%
}{}{}


\usepackage{forloop}

\newcommand{\authortableofcontents}{%
  \chapter*{\contentsname
    \@mkboth{%
      \MakeUppercase\contentsname}{\MakeUppercase\contentsname}}%
  \forloop{dummycounter}{1}{\value{dummycounter} < \numexpr\totvalue{authorcounter}+1}{%
    \@starttoc{\@Alph\c@dummycounter toc}%
  }%
}

%%% End of my additions

\makeatother

\begin{document}
%\tableofcontents*

\authortableofcontents

\authortoc{Walter von der Vogelweide}

\chapter{Ich sass uf eynem Steine?}
\section{Und dachte Bein mit Beine}
\Blindtext\Blindtext\Blindtext\Blindtext


\authortoc{Hartmann von Aue}
\chapter[Was auch immer]{So gebt nur mehr und immer mehr}
\section{Moralische Quellen der Irrationalitat}
\Blindtext\Blindtext\Blindtext\Blindtext

\authortoc{Walter von der Vogelweide}
\chapter[Unter der Linden]{Unter der Linden an der Heide}

\section{Text}

\begin{quote}
Under der linden 

an der heide, 

dâ unser zweier bette was, 

dâ muget ir vinden 

schône beide 

gebrochen bluomen unde gras. 

Vor dem walde in einem tal, 

tandaradei, 

schône sanc diu nahtegal.
\end{quote}


\authortoc{Hartmann von Aue}
\chapter{Der arme Heinrich}
\section{Text}

\blindtext

\authortoc{Walter von der Vogelweide}
\chapter{Daz sluezzelin}

du bist mîn ich bin dîn 

des solt dû gewis sîn 

dû bist beslozzen 

in mînem herzen

verlorn ist daz slüzzelîn

dû muost immer drinne sîn


\authortoc{Shakespeare}
\chapter{Hamlet}


\end{document}

enter image description here

Edit A better version

Changes compared to the 1st version:

  • Added the \authortocname which means the heading name of the author toc content --> change with \renewcommand{\authortocname}{...} so something more appropiate
  • The \sections etc. can appear in the author toc now too
  • The number of authors isn't any longer limited to 26, since each author defines a number and the ending is .1toc, .2toc, etc.
  • It works with hyperref as long that package is loaded after all patching to \chapter etc. have been made.

\documentclass[10pt,twoside]{book}


\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc} 


\usepackage{blindtext}


%%%%% My additions and changes
\usepackage{xpatch}
\usepackage{morewrites}
\usepackage{totcount}
\usepackage{etoolbox}

\newtoggle{authortoconly}
\togglefalse{authortoconly}   % Set \toggletrue{authortoconly} if the chapters etc. and sections should not appear in the toc as well

\makeatletter

\listcsgadd{authortoclist}{} % This holds a list of author names

\newcommand{\addauthortolist}[2][authortoclist]{%    % wrapper command to add names to the author list 
  \listcsgadd{#1}{#2}%   % 
}


% A command to generate the toc ending
\newcommand{\authortoc@@tocname}[1]{%
  \arabic{#1}toc%
}


\newtotcounter{authorcounter}


% Helper counters
\newcounter{authtoc@@dummycounter}
\newcounter{authtoc@@position}

% Get the position of an author in the list
\newcommand{\authtoc@@getauthornumber}[2]{%
  \stepcounter{authtoc@@dummycounter}%
  \ifstrequal{#1}{#2}{%
    \setcounter{authtoc@@position}{\number\value{authtoc@@dummycounter}}
    \listbreak
  }{%
  }%
}


% From the original O.P. 
\DeclareRobustCommand\authortoctext[1]{%
{\addvspace{10pt}\nopagebreak\leftskip0em\relax
\rightskip \@tocrmarg\relax
\noindent\itshape#1\par\addvspace{-7pt}}}



\newcommand{\authortoc}[1]{%    
  \xifinlistcs{#1}{authortoclist}{%  Is the author already known?
    \setcounter{authtoc@@dummycounter}{0}%
    \forlistcsloop{\authtoc@@getauthornumber{#1}}{authortoclist}%  loop through the names
  }{% Nope, the author has not been specified so far
    \refstepcounter{authorcounter}%  
    \addauthortolist[authortoclist]{#1}%   Store the author name
    \setcounter{authtoc@@position}{\number\value{authorcounter}}% Set the toc file to the author number
    \addtocontents{\authortoc@@tocname{authtoc@@position}}{\authortoctext{#1}}% Add the author name to the relevant author toc file
  }%
}


%%% Enabling chapters making additional entries to other tocs
\xpatchcmd{\@chapter}{%
  \addcontentsline{toc}{chapter}{% 
    \protect\numberline{\thechapter}#1}%
}{%
  \nottoggle{authortoconly}{%
  \addcontentsline{toc}{chapter}{% 
    \protect\numberline{\thechapter}#1}%
  }{}%
  \addcontentsline{\authortoc@@tocname{authtoc@@position}}{chapter}{%
    \protect\numberline{\thechapter}#1}%
}{}{}

\xpatchcmd{\@sect}{%
  \addcontentsline{toc}{#1}{%
    \ifnum #2>\c@secnumdepth \else
    \protect\numberline{\csname the#1\endcsname}%
    \fi
    #7}%
}{%
  \nottoggle{authortoconly}{%
  \addcontentsline{toc}{#1}{%
    \ifnum #2>\c@secnumdepth \else
    \protect\numberline{\csname the#1\endcsname}%
    \fi
    #7}%
  }{}%
  \addcontentsline{\authortoc@@tocname{authtoc@@position}}{#1}{%
    \ifnum #2>\c@secnumdepth \else
    \protect\numberline{\csname the#1\endcsname}%
    \fi
    #7}%
}{\typeout{Patched @sect}}{}


\newcommand{\authortocname}{Autoreninhaltsverzeichnis}


% This is the standard latex kernel \@starttoc, it's not necessary if `book.cls` is used, but for `memoir.cls` etc. 
\def\latex@starttoc#1{%
  \begingroup
  \makeatletter
  \@input{\jobname.#1}%
  \if@filesw
  \expandafter\newwrite\csname tf@#1\endcsname
  \immediate\openout \csname tf@#1\endcsname \jobname.#1\relax
  \fi
  \@nobreakfalse
  \endgroup
}



\newcommand{\authortableofcontents}{%
  \chapter*{\authortocname
    \@mkboth{%
      \MakeUppercase\contentsname}{\MakeUppercase\contentsname}}%
  \setcounter{authtoc@@dummycounter}{0}%
  \loop
  \ifnum\value{authtoc@@dummycounter} < \numexpr\totvalue{authorcounter}
  \stepcounter{authtoc@@dummycounter}%
  \latex@starttoc{\authortoc@@tocname{authtoc@@dummycounter}}%
  \repeat%
}
\makeatother


\renewcommand{\thesection}{\arabic{section}}
\setcounter{secnumdepth}{3} 
\setcounter{tocdepth}{2}

\usepackage[bookmarksopen=true,bookmarksnumbered=true]{hyperref}
\usepackage{bookmark}


%%% End of my additions


\begin{document}
\tableofcontents

\authortableofcontents

\authortoc{Walter von der Vogelweide}

\chapter{Ich sass uf eynem Steine?}
\section{Und dachte Bein mit Beine}
\Blindtext\Blindtext\Blindtext\Blindtext


\authortoc{Hartmann von Aue}
\chapter[Was auch immer]{So gebt nur mehr und immer mehr}
\section{Moralische Quellen der Irrationalitat}
\Blindtext\Blindtext\Blindtext\Blindtext

\authortoc{Walter von der Vogelweide}
\chapter[Unter der Linden]{Unter der Linden an der Heide}

\section{Text}

\begin{quote}
Under der linden 

an der heide, 

dâ unser zweier bette was, 

dâ muget ir vinden 

schône beide 

gebrochen bluomen unde gras. 

Vor dem walde in einem tal, 

tandaradei, 

schône sanc diu nahtegal.
\end{quote}


\authortoc{Hartmann von Aue}
\chapter{Der arme Heinrich}
\section{Text}

\blindtext

\authortoc{Walter von der Vogelweide}
\chapter{Daz sluezzelin}

du bist mîn ich bin dîn 

des solt dû gewis sîn 

dû bist beslozzen 

in mînem herzen

verlorn ist daz slüzzelîn

dû muost immer drinne sîn


\authortoc{Shakespeare}
\chapter{Hamlet}

\section{The meaning of Brontosaurs to Shakespeare}
\subsection{Miss Ann Elk}

\end{document}
|improve this answer|||||
  • I will add a better version without spurious spaces etc. ;-) – user31729 Jul 26 '15 at 17:02
  • 1
    This works for the moment in any case. Thank you! – usr203050 Jul 26 '15 at 17:07
  • The new version is interesting and better. However, the first is also excellent. Thank you. – usr203050 Jul 28 '15 at 16:22
  • @usr203050: You made me thinking about a new package for such additions to the toc. – user31729 Jul 28 '15 at 19:46
  • @ Christian Hupfer, I think many people will be interested in such flexible toc. – usr203050 Jul 29 '15 at 8:21
2

Here's something that should be workable. It assumes you have a relatively small list of authors, since their names are hard-coded into the author list commands and files.

enter image description here

\documentclass{memoir}

\newcommand{\authortoc}[2]{
  \chapter{#2}
  \addcontentsline{#1}{chapter}{\numberline{#2}}\par%
}

\newcommand{\listkafkaname}{Kafka}
\newlistof{listofkafka}{kafka}{\listkafkaname}
\newcommand{\listmaupassantname}{Maupassant}
\newlistof{listofmaupassant}{maupassant}{\listmaupassantname}
\newcommand{\listtolstoyname}{Tolstoy}
\newlistof{listoftolstoy}{tolstoy}{\listtolstoyname}

\begin{document}

\tableofcontents*

\listofkafka*
\listofmaupassant*
\listoftolstoy*

\authortoc{kafka}{The Rejection}

\authortoc{maupassant}{Love}

\authortoc{maupassant}{Pierrot}

\authortoc{tolstoy}{Three Questions}

\authortoc{kafka}{The Judgment}

\end{document}
|improve this answer|||||
  • This is useful. Also, deleting the \tableofcontents* works as well, and it gives almost what I want. – usr203050 Jul 26 '15 at 17:06

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.