创建 toc、lof 或 lot 等列表

创建 toc、lof 或 lot 等列表

我想创建一个像 toc、lof、lot 这样的列表。应该叫做 listofsongs 或类似的名称。问题是:是否有任何软件包允许我创建类似的列表,还是我必须自己做这一切?

LaTeX 中是否有已使用的文件扩展名列表?例如,我不应该使用 MyDocument.toc,因为这会导致与目录冲突。

最后,我应该能够\listofsongs像我写的那样写作\tableofcontents,并且它的行为也应该类似。

答案1

您或许可以使用 LaTeX2e-kernel-macros\@starttoc\addtocontents{<file-extension>}{<tokens>}

\addtocontents当在 LaTeX 运行结束时处理辅助文件时,如果该文件已打开以供输入,则会将一些未扩展的写入指令(未扩展)写入辅助文件<tokens>\jobname.<file-extension>

\@starttoc{<file-extension>}如果文件存在,将使 TeX 读取/处理该文件\jobname.<file-extension>,然后销毁该文件,然后重新创建它并打开它进行输入,以便在 LaTeX 运行结束时处理辅助文件时,该文件将打开进行输入。

如果你想做一些更奇特的事情,看看\addcontentslineLaTeX2e-kernel 的宏。

基本上,\addcontentsline{<file-extension>}{<command name>}{<tokens>}将向辅助文件写入一些指令,用于在 LaTeX 运行结束时处理辅助文件时,将未扩展的标记序列写入\contentsline{<command name>}{<tokens>}{<page number>}文件,以防该文件打开以供输入。在 LaTeX 运行结束时处理辅助文件期间写入后,您将在其中找到该指令 。因此,当由于文件被读取/处理时,该指令将被执行。\jobname.<file-extension>\jobname.<file-extension>\contentsline{<command name>}{<tokens>}{<page number>}\@starttoc\jobname.<file-extension>

\contentsline{<command name>}{<tokens>}{<page number>}反过来会调用一个\l@<command name>处理两个参数的宏,即<tokens>-argument 和<page number>-argument。

当此机制与 toc-file 和 sectioning-commands 一起使用时,<command name>将类似于sectionsubsection产生宏的执行\l@section,或者\l@subsection但您也可以\l@..为自己的 rubrification 类别定义自己的宏。

\addtocontents...-机制\@starttoc{<file-extension>}确实在其他事物之下定义了控制序列\tf@<file-extension>。因此,为了查明该机制是否已经分配了\write用于写入文件的句柄\jobname.<file-extension>,您可以使用\@ifundefined- 宏来检查控制序列是否\tf@<file-extension>已定义。

但这只有在 scrwfile 包未使用时才有效——顺便说一句:如果您打算在文档中创建许多列表(如 toc、lof 或 lot),您可能会对 scrwfile 包感兴趣。有关该包的更多信息,请访问 CTAN:https://www.ctan.org/pkg/scrwfile

LaTeX2e 内核的解释和注释在文件 source2e.pdf 中,该文件位于https://www.ctan.org/pkg/source2e。- -
机制和利用该机制的 -macro 在名为文件 F ltsect.dtx → 59 分段命令 → 59.3 目录等的部分中进行了解释。\addtocontents\@starttoc\addcontentsline

\documentclass{article}
\usepackage{hyperref}
\usepackage{verbatim}
\makeatletter
\newcommand\l@songline[2]{%
  \par Now we have data about another song:\\
  Title of song: #1. Song is printed on page: #2.%
}%
\makeatother
\begin{document}
\LaTeX{} will now write directives to aux-file for writing the first
 line into \jobname.weird. These directives will be carried out at
 the end of the \LaTeX-run when the aux-file is read/processed in
 case at that time the file \jobname.weird is open for writing to it.\\
\addtocontents{weird}{First line in file \jobname.weird.}%
\bigskip

This is what \jobname.weird looks like before calling \verb|\@starttoc|:    \\
\verbatiminput{\jobname.weird}
\bigskip

This is how \verb|\l@songline| is defined:\\{%
  \csname verbatim@font\endcsname\selectfont
  \expandafter\meaning\csname l@songline\endcsname
}%
\bigskip

The file \jobname.weird is \csname @ifundefined\endcsname{tf@weird}{not}{already} allocated.
\bigskip

\LaTeX will now read/process \jobname.weird and then destroy that file
and create it anew and open it for writing. Thus at the end of the
\LaTeX-run, when the aux-file is read/processed, that file will be open
for writing to it:\bigskip

\csname @starttoc\endcsname{weird}%
\bigskip

The file \jobname.weird is \csname @ifundefined\endcsname{tf@weird}{not}{already} allocated.
\bigskip

\LaTeX{} will now write directives to aux-file for writing the second
line into \jobname.weird. These directives will be carried out at
the end of the \LaTeX-run when the aux-file is read/processed in
case at that time the file \jobname.weird is open for writing to it.\\
\addtocontents{weird}{Second line in file \jobname.weird.}%
\bigskip

Now two \verb|\addcontentsline|-entries for writing things to \jobname.weird
that need to be "rubrified" by means of applying the \verb|l@songgline|-macro.
\addcontentsline{weird}{songline}{Morning has broken}
\addcontentsline{weird}{songline}{Final Countdown}

\end{document} 

答案2

不幸的是,问题中没有 MWE。所以我不知道你真正想做什么。阅读你的个人资料,我猜你正在使用scrbook。所以这里有一个建议,需要最新的 KOMA-Script。

\documentclass[listof=totoc,ngerman]{scrbook}[2016/06/14]% needs at least KOMA-Scritp version 3.21
\usepackage{babel}
\DeclareNewTOC[
  type=song,
  tocentryindent=0pt,
  tocentrynumwidth=2.3em,
  tocentrystyle=tocline,
  tocentrylinefill=\hfill,
  tocentryentryformat=\sffamily,
  tocentrypagenumberformat=\sffamily
]{los}
\newcaptionname{ngerman}{\listsongname}{Liederverzeichnis}

\DeclareNewSectionCommand[
  style=chapter,
  level=0,
  beforeskip=-1sp,
  innerskip=0pt,
  afterskip=\baselineskip,
  font=\usekomafont{section},
  prefixfont=\usekomafont{chapter},
  pagestyle=plain
]{song}

\renewcommand\songformat{}
\renewcommand\addsongtocentry[2]{\addxcontentsline{los}{song}{#2}}

\usepackage{hyperref}

\begin{document}
\tableofcontents
\addchap{Einleitung}
Text
\listofsongs
\song{Ein Liedtitel}
\end{document}

这是使用标准类book和 KOMA 包的另一个版本tocbasic。但请注意,我不知道您如何定义\song命令或song环境:

\documentclass[ngerman]{book}
\usepackage{babel}

\usepackage{tocbasic}[2016/06/14]% needs at least KOMA-Scritp version 3.21
\DeclareNewTOC[
  type=song,
  tocentryindent=0pt,
  tocentrynumwidth=2.3em,
  tocentrystyle=tocline,
  tocentrylinefill=\hfill,
  tocentryentryformat=\sffamily,
  tocentrypagenumberformat=\sffamily
]{los}
\newcaptionname{ngerman}{\listsongname}{Liederverzeichnis}

\usepackage{lipsum}
\usepackage{hyperref}

\begin{document}
\listofsongs

\clearpage
\phantomsection\addxcontentsline{los}{song}{Ein Liedtitel im Verzeichnis}
\section*{Ein Liedtitel}

\lipsum

\clearpage
\phantomsection\addxcontentsline{los}{song}{Ein anderer Liedtitel im Verzeichnis}
\section*{Ein anderer Liedtitel}
\end{document}

相关内容