按长度对单词数组进行排序

按长度对单词数组进行排序

我有一个很长的单词数组,需要按长度递增的顺序显示。我尝试使用包 arraysort 并自定义比较器。

我的简化代码是:

\documentclass{article}                                                                                                        
\usepackage{calc} 
\usepackage{arraysort} 

\newlength{\somelength} 
\newcommand{\showLen}[1]{#1 \setlength{\somelength}{\widthof{#1}}\the\somelength\\} 

\def\IsPositive#1{% 
  TT\fi 
  \ifcat_\ifnum0<0#1 _\else A\fi 
} 
%customised comparator based on example from arraysort manual 
\newcommand{\cmpLen}[2]{% 
  \edef\cmpA{\showLen{#1}}% 
  \edef\cmpB{\showLen{#2}}% 
  \if\IsPositive{\cmpA}% 
  \if\IsPositive{\cmpB}% 
    \arraysortcomparenum{\cmpA}{\cmpB}% 
  \else% 
    \togglefalse {arraysortresequal}% 
    \toggletrue{arraysortresult}% 
  \fi % 
  \else% 
  \if\IsPositive{\cmpB}% 
  \togglefalse{arraysortresequal}% 
  \togglefalse{arraysortresult}% 
  \else% 
  \arraysortcomparestr{\cmpA}{\cmpB}% 
  \fi % 
  \fi % 
} 
\begin{document} 
\newarray{A} 
\readarray{A}{Compulsion&His&Obsession&Girl&Relationship&His}% 
\sortArray[cmpLen]{63}{A} 
\A{1}\A{2}\A{3}\A{4}\A{5} 
\end{document}

我修改了 arraysort 手册第 4 页中的示例“\sortArray with custom comparator”。我的修改是在 的定义中\cmpA\cmpB其中我提供了使用命令 \showLen 计算的长度。

它不工作并且显示的错误不明显。

! Undefined control sequence.
\GenericError  ...                                
                                                    #4  \errhelp \@err@     ...
l.37 \A
       {1}\A{2}\A{3}\A{4}\A{5}

为什么我的代码不起作用?有没有更好的方法,只使用 LaTeX 而不预先对数组进行排序?

答案1

您还需要选项comparenum。这似乎可以做到。

编译时间明显过长,好像加载了数十万行代码……啊,好吧,这是因为\sortArray[cmpLen]{63}{A}来自 OP 的。将其变成\sortArray[cmpLen]{6}{A},然后就可以正常编译了。

\documentclass{article}
\usepackage{calc}
\usepackage[comparenum]{arraysort}

\newsavebox\mybox

%\newlength{\somelength}
%\newcommand{\showLen}[1]{#1 \setlength{\somelength}{\widthof{#1}}\the\somelength\\}

\def\IsPositive#1{%
  TT\fi
  \ifcat_\ifnum0<0#1 _\else A\fi
}
%customised comparator based on example from arraysort manual
\newcommand{\cmpLen}[2]{%
  \sbox\mybox{#1}\edef\cmpA{\number\wd\mybox}%
  \sbox\mybox{#2}\edef\cmpB{\number\wd\mybox}%
  \if\IsPositive{\cmpA}%
      \if\IsPositive{\cmpB}%
          \arraysortcomparenum{\cmpA}{\cmpB}%
      \else
          \togglefalse {arraysortresequal}%
          \toggletrue  {arraysortresult}%
      \fi
  \else
      \if\IsPositive{\cmpB}%
          \togglefalse {arraysortresequal}%
          \togglefalse {arraysortresult}%
      \else
          \arraysortcomparenum{\cmpA}{\cmpB}%
      \fi
  \fi
}

\begin{document}
\newarray{A}
\readarray{A}{Compulsion&His&Obsession&Girl&Relationship&His}%

\sortArray[cmpLen]{63}{A}

\A(1)

\A(2)

\A(3)

\A(4)

\A(5)

\A(6)
\end{document}

生产

His
His
Girl
Obsession
Compulsion
Relationship

答案2

以下是使用 和 的实现expl3l3sort*-variant 按相反顺序排序;可选参数(默认“逗号空格”)是打印输出中项目之间的分隔符。

\documentclass{article}
\usepackage{xparse,l3sort}

\ExplSyntaxOn
% \sortwordsbylength has a *-variant (for reverse ordering),
% an optional argument with default value “comma space”
% and a mandatory argument
\NewDocumentCommand{\sortwordsbylength}{ s +O{,~} m }
 {
  \IfBooleanTF{#1}
   {% for decreasing ordering we pass <
    \kees_sort_bylength:nnn { < } { #2 } { #3 }
   }
   {% for increasing order we pass >
    \kees_sort_bylength:nnn { > } { #2 } { #3 }
   }
 }

% some variables
\seq_new:N \l__kees_sort_items_seq
\box_new:N \l__kees_sort_boxa_box
\box_new:N \l__kees_sort_boxb_box

% the main macro
\cs_new_protected:Nn \kees_sort_bylength:nnn
 {% split the input at commas
  \seq_set_split:Nnn \l__kees_sort_items_seq { , } { #3 }
  % sort the sequence according to l3sort
  \seq_sort:Nn \l__kees_sort_items_seq
   {
    \hbox_set:Nn \l__kees_sort_boxa_box { ##1 }
    \hbox_set:Nn \l__kees_sort_boxb_box { ##2 }
    \dim_compare:nTF { \box_wd:N \l__kees_sort_boxa_box #1 \box_wd:N \l__kees_sort_boxb_box }
     { \sort_reversed: }
     { \sort_ordered: }
   }
  % print the sequence, with the stated separator between items
  \seq_use:Nn \l__kees_sort_items_seq { #2 }
 }
\ExplSyntaxOff

\begin{document}

\sortwordsbylength{Compulsion,His,Obsession,Girl,Relationship,His}

\bigskip

\sortwordsbylength*[\par]{Compulsion,His,Obsession,Girl,Relationship,His}

\end{document}

在此处输入图片描述

相关内容