我有一長串單字,需要按長度遞增的順序顯示。我嘗試使用套件 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
這是一個帶有expl3
和 的實現l3sort
。 * 變體依相反順序排序;可選參數(預設“逗號空格”)是列印輸出中項目之間的分隔符號。
\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}