1

Let's say I've created a large numbered list of items with some cyrillic characters, but now I want to reorder them alphabetically. By the way the numbers at each item should also be reordered. I have read the Alphabetically display the items in itemize article, but the method here was shown to fail in my case (perhaps I did smth wrong). Isn't it just easier to put the whole code into Excel and be happy with its sorting tool?

Here is what I have:

1) БВГ

2) АБВ

3) ДЕЖ

4) ХАУ

There is what I need:

1) АБВ

2) БВГ

3) ДЕЖ

4) ХАУ

The code to work with:

\documentclass{article}

\usepackage[T2A]{fontenc}           
\usepackage[utf8]{inputenc}         
\usepackage[english,russian]{babel} 


\begin{document}
    \begin{enumerate}
        \item БВГ
        \item АБВ
        \item ДЕЖ
        \item ХАУ
        \end{enumerate}

\end{document}
Rubisko
  • 57

1 Answers1

2

xindy does a nice job of sorting and there's a lot of questions in TeX.SX about it. However, if you accept a LuaTeX-based solution without external software, there's a new package lua-uca which supports the basics of Unicode Collation Algorithm. You fortunate! Russian is among the languages predefined in the package, so let's use it:

%!TEX program = lualatex
\documentclass{article}
\usepackage{fontspec}
%Change the font if necessary
\setmainfont{Noto Sans}
%For using \luadirect and luacode* environment
\usepackage{luacode}
\begin{luacode*}
%Copied from the documentation almost literally
kpse.set_program_name "luatex"

ducet = require "lua-uca.lua-uca-ducet"
collator = require "lua-uca.lua-uca-collator"
languages = require "lua-uca.lua-uca-languages"

collator_obj = collator.new(ducet)
collator_obj = languages.ru(collator_obj)

%Sorting
function russian_sort(mylist)
    table.sort(mylist, function(a,b)return collator_obj:compare_strings(a,b)end)
    return mylist
end

%Printing as list
function table_to_tex(mylist)
    tex.print([[\begin{enumerate}]])
    for _, v in ipairs(mylist) do
        tex.print([[\item]].." "..v)
    end
    tex.print([[\end{enumerate}]])
end

ourlist = ourlist or {}

\end{luacode*}
%Passing TeX content to Lua
\newcommand{\addtoourlist}[1]{\luadirect{table.insert(ourlist, \luastringN{#1})}}
%Print an ordered list
\newcommand\printtoourlist{\directlua{russian_sort(ourlist) table_to_tex(ourlist)}} 
\begin{document}
\addtoourlist{БВГ}
\addtoourlist{АБВ}
\addtoourlist{ДЕЖ}
\addtoourlist{ХАУ}
\printtoourlist
\end{document}

enter image description here

  • 1
    Thanks! Wouldn't say I'm in good relationship with Lua, but I'll take a look. Btw never heard of xindy, got to try. – Rubisko Jun 08 '20 at 05:44
  • Oh, I've forgotten to say you have to use TeX Live 2020 (the package is fresh new). In case you use MiKTeX, let it install on-the-fly. :) –  Jun 08 '20 at 05:46