目录中的作者位于标题之前且有重复

目录中的作者位于标题之前且有重复

这是将作者姓名放在目录中章节标题之前的方法。在目录中的章节标题前添加作者,我的意思是答案https://tex.stackexchange.com/a/110256/80224

这完全没问题。但假设一位作者写了多个章节,我们希望目录看起来像这样:

卡夫卡
拒绝......7
判断......77

莫泊桑
愛...............15
皮埃罗...............33

托尔斯泰
三个问题....40.

也就是说,即使特定作者的作品没有按顺序排列,它们也会在目录中归类为同一作者,并且不会有重复的作者。

我应该如何修改这里给出的代码https://tex.stackexchange.com/a/110256/80224

以下是代码:

\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}

答案1

嗯,这有点像黑客攻击......

考虑到请求,\chaptera 中的有序外观是“混乱的”,因此我将内容行存储在等文件中,每个作者都有自己的文件,但只有 26 位作者。如果要做更多事情,包会有所帮助 ;-)ToCAtocBtocalphalph

\chapter不再向 写入条目ToC,但可以很容易地将其改回默认值。

最后(嗯,实际上是在开始),\authortableofcontents收集所有单独的Atoc文件等并向它们显示一行。

很可能是垂直间距错误。

\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}

在此处输入图片描述

编辑更好的版本

与第一版相比的变化:

  • 添加了\authortocname表示作者目录内容的标题名称 --> 更改为\renewcommand{\authortocname}{...}更合适的内容
  • 等等\sections现在也可以出现在作者目录中
  • 作者的数量不再限制为 26 位,因为每个作者定义一个数字,并且以.1toc.2toc等结尾。
  • hyperref只要在\chapter完成所有修补等之后加载该包,它就可以工作。

\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}

答案2

这是可行的。它假设您有一个相对较小的作者列表,因为他们的名字被硬编码到作者列表命令和文件中。

在此处输入图片描述

\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}

相关内容