![按名称对自定义目录进行排序](https://linux22.com/image/369966/%E6%8C%89%E5%90%8D%E7%A7%B0%E5%AF%B9%E8%87%AA%E5%AE%9A%E4%B9%89%E7%9B%AE%E5%BD%95%E8%BF%9B%E8%A1%8C%E6%8E%92%E5%BA%8F.png)
我已经使用两个参数定义了自定义目录\DeclareNewTOC
:
\DeclareNewTOC[
type=api,
types=apis,
float,
floattype=5,
name=Api,
floatpos=ht,
listname={API-List}
]{loa}
\addtotoclist[api]{api}
目前,条目按它们在文档中出现的顺序列出。是否可以按名称对它们进行排序(在我的例子中是参数 #1)?
这是针对上述图片的一个小型可编译文档:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% KOMA Document
\documentclass{scrbook}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Packages
\usepackage{array}
\usepackage{tabularx, colortbl}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Macros
\DeclareNewTOC[
type=api,
types=apis,
float,
floattype=5,
name=Api,
floatpos=ht,
listname={API-List}
]{loa}
\addtotoclist[api]{api}
\makeatletter
\newwrite\@apifile
\newif\if@apifilewritten
\renewcommand*{\@apifilewrittentrue}{\global\let\if@apifilewritten\iftrue}
\renewcommand*{\@apifilewrittenfalse}{\global\let\if@apifilewritten\iffalse}
\newcommand*{\apioverview}{
\closeapioverview
\InputIfFileExists{\jobname-\thechapter.api}{}{
\typeout{No file `\jobname-\thechapter.api' found.}
}
}
\newcommand*{\startapioverview}{
\if@apifilewritten\else
\if@filesw
\typeout{Open new api file `\jobname-\thechapter.api'}%
\@apifilewrittentrue
\openout\@apifile \jobname-\thechapter.api
\protected@write\@apifile{}{%
\string\begin{tabularx}{\string\textwidth}{|X|c|c|}^^J
\string\hline^^J
\string\textbf{API}&
\string\textbf{Method}&
\string\textbf{Page}\string\\^^J
\string\hline^^J
\string\hline
}%
\fi
\fi
}
\newcommand*{\closeapioverview}{%
\if@apifilewritten
\protected@write\@apifile{}{%
\string\end{tabularx}%
}%
\closeout\@apifile
\@apifilewrittenfalse
\fi
}
\newcommand*{\setapientry}[2]{%
\startapioverview
\protected@write\@apifile{}{ \detokenize{#1}&\detokenize{#2}&\thepage\string\\^^J \string\hline}%
}
\makeatletter
\AtEndDocument{\closeapioverview}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Document Content
\begin{document}
\chapter{Chapter 1}
\section{Overview}
\apioverview
\section{Content}
\subsection{def}
\setapientry{def}{SomeMethod}
\subsection{abc}
\setapientry{abc}{AnotherMethod}
\chapter{Chapter 2}
\section{Overview 2}
\apioverview
\section{Content 2}
\subsection{jkl}
\setapientry{jkl}{SomeMethod}
\subsection{ghi}
\setapientry{ghi}{AnotherMethod}
\end{document}
答案1
您可以使用makeindex
。先用 运行文件PDFLaTeX
,然后运行 ,MakeIndex
再运行PDFLaTeX
。
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% KOMA Document
\documentclass{scrbook}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Packages
\usepackage{array}
\usepackage{tabularx, colortbl}
\usepackage{filecontents, makeidx}
\makeindex
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Macros
\begin{filecontents}{\jobname.mst}
preamble "\\begin{tabularx}{\\textwidth}{|X|c|c|}\n\\hline\n\\textbf{API}\&\\textbf{Method}\&\\textbf{Page}\\\\\n\\hline\\hline\n"
postamble "\n\\hline\n\\end{tabularx}\n"
group_skip "\\hline"
item_0 "\n"
item_1 "\n"
delim_0 "\&"
delim_1 "\&"
delim_t "\\\\"
\end{filecontents}
\newcommand{\setapientry}[2]{%
\index{\detokenize{#1}&\detokenize{#2}}%
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Document Content
\begin{document}
\chapter{Chapter 1}
\section{Overview}
%\apioverview
\printindex
\section{Content}
\subsection{def}
\setapientry{def}{SomeMethod}
\subsection{abc}
\setapientry{abc}{AnotherMethod}
\end{document}
编辑1:下面使用imakeidx
而不是 ,并makeidx
进行了一些调整和修改,允许多个章节。但是,它会占用大量项目文件夹,而且速度有点慢。您必须运行PDFLaTeX
两次。
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% KOMA Document
\documentclass{scrbook}
\newcounter{maxchap}
\setcounter{maxchap}{5} % Set to the total of chapters in your document
\count255=0
\loop\ifnum\count255<\number\themaxchap
\advance\count255 by 1
\expandafter\IfFileExists\expandafter{chap\number\count255.idx}{%
\immediate\write18{makeindex -s idxstyle.ist chap\number\count255.idx}{}%
}
\repeat
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Packages
\usepackage{array}
\usepackage{tabularx, colortbl}
\usepackage{filecontents}
\usepackage[noautomatic]{imakeidx}
\count255=0
\loop\ifnum\count255<\number\themaxchap
\advance\count255 by 1
\begingroup\edef\x{\endgroup
\noexpand\makeindex[name=chap\number\count255]}\x
\repeat
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Macros
\begin{filecontents}{idxstyle.ist}
preamble "\\begin{tabularx}{\\textwidth}{|X|c|c|}\n\\hline\n\\textbf{API}\&\\textbf{Method}\&\\textbf{Page}\\\\\n\\hline\\hline\n"
postamble "\n\\hline\n\\end{tabularx}\n"
group_skip "\\hline"
item_0 "\n"
item_1 "\n"
delim_0 "\&"
delim_1 "\&"
delim_t "\\\\"
\end{filecontents}
\newcommand{\setapientry}[2]{%
\expandafter\index\expandafter[chap\thechapter]{\detokenize{#1}&\detokenize{#1}}%
}
\newcommand{\apioverview}[1]{%
\expandafter\InputIfFileExists\expandafter{chap\thechapter.ind}{}{}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Document Content
\begin{document}
\chapter{Chapter 1}
\section{Overview}
\apioverview
\section{Content}
\subsection{def}
\setapientry{def}{SomeMethod}
\subsection{abc}
\setapientry{abc}{AnotherMethod}
\chapter{Chapter 2}
\section{Overview}
\apioverview
\section{Content}
\subsection{jkl}
\setapientry{jkl}{SomeMethod}
\newpage
\subsection{ghi}
\setapientry{ghi}{AnotherMethod}
\subsection{abc}
\setapientry{abc}{AgainAnotherMethod}
\end{document}
文档更改后,您应该删除所有chap
文件并让脚本重新创建它们。
编辑2:我将您的方法与我的方法合并。这比我第一次尝试快得多,第一次尝试会生成和处理大量索引文件,但这些文件最终可能会为空。所以这里终于有一个可行的解决方案。要用 编译两次PDFLaTeX
。
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% KOMA Document
\documentclass{scrbook}
\newcounter{maxchap}
\setcounter{maxchap}{5} % Set to the total of chapters in your document
\count255=0
\loop\ifnum\count255<\number\themaxchap
\advance\count255 by 1
\expandafter\IfFileExists\expandafter{chap\number\count255.idx}{%
\immediate\write18{makeindex -s idxstyle.ist chap\number\count255.idx}{}%
}
\repeat
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Packages
\usepackage{array}
\usepackage{tabularx, colortbl}
\usepackage{filecontents}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Macros
\begin{filecontents}{idxstyle.ist}
preamble "\\begin{tabularx}{\\textwidth}{|X|c|c|}\n\\hline\n\\textbf{API}\&\\textbf{Method}\&\\textbf{Page}\\\\\n\\hline\\hline\n"
postamble "\n\\hline\n\\end{tabularx}\n"
group_skip "\\hline"
item_0 "\n"
item_1 "\n"
delim_0 "\&"
delim_1 "\&"
delim_t "\\\\"
\end{filecontents}
\makeatletter
\newwrite\@apifile
\newif\if@apifilewritten
\renewcommand*{\@apifilewrittentrue}{\global\let\if@apifilewritten\iftrue}
\renewcommand*{\@apifilewrittenfalse}{\global\let\if@apifilewritten\iffalse}
\newcommand*{\apioverview}{
\closeapioverview
\InputIfFileExists{chap\thechapter.ind}{}{
\typeout{No file `chap\thechapter.ind' found.}
}
}
\newcommand*{\startapioverview}{
\if@apifilewritten\else
\if@filesw
\typeout{Open new idx file `chap\thechapter.idx'}%
\@apifilewrittentrue
\openout\@apifile chap\thechapter.idx
\fi
\fi
}
\newcommand*{\closeapioverview}{%
\if@apifilewritten
\closeout\@apifile
\@apifilewrittenfalse
\fi
}
\newcommand*{\setapientry}[2]{%
\startapioverview
\protected@write\@apifile{}{ \string\indexentry{\detokenize{#1}&\detokenize{#2}}{\thepage} }%
}
\makeatletter
\AtEndDocument{\closeapioverview}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Document Content
\begin{document}
\chapter{Chapter 1}
\section{Overview}
\apioverview
\section{Content}
\subsection{def}
\setapientry{def}{SomeMethod}
\subsection{abc}
\setapientry{abc}{AnotherMethod}
\chapter{Chapter 2}
\section{Overview 2}
\apioverview
\section{Content 2}
\subsection{jkl}
\setapientry{jkl}{SomeMethod}
\subsection{ghi}
\setapientry{ghi}{AnotherMethod}
\end{document}