在如何对字母数字列表进行排序,有一个很好的简短的排序功能,我在这里重现一下:
%% from https://tex.stackexchange.com/questions/6988/how-to-sort-an-alphanumeric-list
\usepackage{etoolbox}
\makeatletter
% #1 - comparator
% #2 - token list to sort
\newcommand\sort[2]{%
\ifstrempty{#2}
{}% else
{%
\sort@begin#1{}#2\sort@s\sort@begin
}%
}
% helpers
\def\sort@s{\sort@s}
\def\ifsort@s#1{%
\ifx\sort@s#1%
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
}
% #1 - comparator
% #2 - tokens processed so far
% #3 - smallest token so far
% #4 - rest of the list
\def\sort@begin#1#2#3#4\sort@begin{%
\ifsort@s{#4}
{%
\sortend{#3}%
\sort#1{#2}%
}% else
{%
\sort@go#1{#2}{#3}#4\sort@go
}%
}
% #1 - comparator
% #2 - tokens processed so far
% #3 - smallest token so far
% #4 - token under consideration
% #5 - rest of the list
\def\sort@go#1#2#3#4#5\sort@go{%
#1{#3}{#4}{\sort@output#1{#2}{#5}}%
}
% #1 - comparator
% #2 - tokens processed so far
% #3 - rest of the list
% #4 - smaller of the two tokens
% #5 - larger of the two tokens
\def\sort@output#1#2#3#4#5{%
\ifsort@s{#3}
{%
\sortoutput{#4}%
\sort#1{#2{#5}}%
}% else
{%
\sort@begin#1{#2{#5}}{#4}#3\sort@begin
}%
}
\def\sort@numlt#1#2#3{%
\ifnumcomp{#1}<{#2}
{#3{#1}{#2}}% else
{#3{#2}{#1}}%
}
\def\sort@numgt#1#2#3{%
\ifnumcomp{#1}>{#2}
{#3{#1}{#2}}% else
{#3{#2}{#1}}%
}
\def\sort@alpha#1#2#3{%
\ifnumcomp{\pdfstrcmp{#1}{#2}}<{0}
{#3{#1}{#2}}% else
{#3{#2}{#1}}%
}
\newcommand*\sortnumlt{\sort\sort@numlt}
\newcommand*\sortnumgt{\sort\sort@numgt}
\newcommand*\sortalpha{\sort\sort@alpha}
\makeatother
\newcommand*\sortoutput[1]{#1 $\rightarrow$ }
\newcommand*\sortend[1]{#1. $\leftarrow$ }
我将上述内容粘贴到sort.sty
文件中,现在我正尝试将其用于样本。
\documentclass{article}
\usepackage{sort}
\def\mystring{}
\makeatletter\def\addstring#1{\g@addto@macro\mystring{ #1}}\makeatother
\begin{document}
Sorted Literals: \sortalpha{{Goodbye, 1}{Cruel, 2}{World, 3}}
\bigskip
\addstring{goodbye, \pageref{pg:goodbye}.} \label{pg:goodbye}
\addstring{cruel, \pageref{pg:cruel}.} \label{pg:cruel}
\addstring{world, \pageref{pg:world}.} \label{pg:world}
Collected: \texttt{\mystring}
Sorted Collected: \sortalpha{\mystring}
\end{document}
输出已根据文字进行了适当排序,但未根据字符串的内容进行适当排序。before \expandafter
似乎\mystring
也没有这样做。
我希望在宏的内容上实现这一点的魔法很简单。感谢您的帮助。
PS:我承认我想在我的 latex 文件末尾创建自己的小型排序关键字索引(使用简单的顺序运行而不是更花哨的格式)没有必须调用类似的外部程序\makeindex
。我可能还需要添加一个\MakeUppercase
。
答案1
这只是 Steven Segletes 和 Marijn 的回答的总结。谢谢你们两位。
\documentclass{article}
\usepackage{sort}
\makeatletter
\def\mykeywords{}
\def\kwclear{\def\mykeywords{}}
\def\addkeyword#1{\g@addto@macro\mykeywords{{#1}}}
\def\chkpageref#1{\@ifundefined{r@#1}{na}{\pageref{#1}}}
\def\kw#1{\textbf{#1}\addkeyword{#1, p.\chkpageref{pg:#1}}\label{pg:#1}}
\def\kwprint{\par\textbf{Keywords: } \expandafter\sortalpha\expandafter{\mykeywords}\par}
\makeatother
\begin{document}
\kwclear
The text is hello and \kw{goodbye}. \kw{cruel} people are in this \kw{world}.
\bigskip
\kwprint
\end{document}
产量
为了完整起见,sort.sty
再次说明:
%% from https://tex.stackexchange.com/questions/6988/how-to-sort-an-alphanumeric-list
\usepackage{etoolbox}
\makeatletter
% #1 - comparator
% #2 - token list to sort
\newcommand\sort[2]{%
\ifstrempty{#2}
{}% else
{%
\sort@begin#1{}#2\sort@s\sort@begin
}%
}
% helpers
\def\sort@s{\sort@s}
\def\ifsort@s#1{%
\ifx\sort@s#1%
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
}
% #1 - comparator
% #2 - tokens processed so far
% #3 - smallest token so far
% #4 - rest of the list
\def\sort@begin#1#2#3#4\sort@begin{%
\ifsort@s{#4}
{%
\sortend{#3}%
\sort#1{#2}%
}% else
{%
\sort@go#1{#2}{#3}#4\sort@go
}%
}
% #1 - comparator
% #2 - tokens processed so far
% #3 - smallest token so far
% #4 - token under consideration
% #5 - rest of the list
\def\sort@go#1#2#3#4#5\sort@go{%
#1{#3}{#4}{\sort@output#1{#2}{#5}}%
}
% #1 - comparator
% #2 - tokens processed so far
% #3 - rest of the list
% #4 - smaller of the two tokens
% #5 - larger of the two tokens
\def\sort@output#1#2#3#4#5{%
\ifsort@s{#3}
{%
\sortoutput{#4}%
\sort#1{#2{#5}}%
}% else
{%
\sort@begin#1{#2{#5}}{#4}#3\sort@begin
}%
}
\def\sort@numlt#1#2#3{%
\ifnumcomp{#1}<{#2}
{#3{#1}{#2}}% else
{#3{#2}{#1}}%
}
\def\sort@numgt#1#2#3{%
\ifnumcomp{#1}>{#2}
{#3{#1}{#2}}% else
{#3{#2}{#1}}%
}
\def\sort@alpha#1#2#3{%
\ifnumcomp{\pdfstrcmp{#1}{#2}}<{0}
{#3{#1}{#2}}% else
{#3{#2}{#1}}%
}
\newcommand*\sortnumlt{\sort\sort@numlt}
\newcommand*\sortnumgt{\sort\sort@numgt}
\newcommand*\sortalpha{\sort\sort@alpha}
\makeatother
\newcommand*\sortoutput[1]{#1 $\rightarrow$ }
\newcommand*\sortend[1]{#1. $\leftarrow$ }