# automatically sort superscripts alphabetically

I would like to sort the superscripts of the following mwe alphabetically. Automatically.

\documentclass{article}

\usepackage[utf8]{inputenc}

\newcommand{\x}[1]{\textsuperscript{#1}}

\newcommand{\red}{R}
\newcommand{\blu}{B}
\newcommand{\gre}{G}

\parindent0pt

\begin{document}
\section{the order I will use}
test 1\x{\red\gre} \\
test 2\x{\blu\red} \\
test 3\x{\gre\blu}
\section{the order I should use/what should be displayed}
test 1\x{\gre\red} \\
test 2\x{\blu\red} \\
test 3\x{\blu\gre}
\end{document}


Surely this can be easily done. But I never really started 'programing' within latex or whatever this might be called. Any help will be appreciated please ask if any clarification is needed.

• Can't you generate the code / sorted indices with an external programming system or MS Excel? – Dr. Manuel Kuehner May 18 '18 at 21:13
• well this would be way over the top. Basically its not about the indices, but about the order the written '\newcommand's will be displayed. – Pratched May 18 '18 at 21:24
• or putting it another way, I am simply too lazy to write them in the right order from the beginning. – Pratched May 18 '18 at 21:25
• Ok, I see. Let's see if there's a clever LaTeX-based solution. – Dr. Manuel Kuehner May 18 '18 at 21:29
• So basically you want \x to sort its argument (and then display the result as a superscript), where the argument always consists of some combination of \red, \gre and \blu? – Nicola Talbot May 19 '18 at 11:57

I dumbed down the alpha-bubble sorter at Trying to eliminate stack overflow during recursion so that it would handle non-comma-separated, 1-character data.

LIMITATIONS: this uses ascii code of each token to sort the data, so that capital Z comes before lowercase a.

I show how to generically use the sorter at the end of the MWE.

\documentclass{article}

\usepackage[utf8]{inputenc}

\newcommand{\x}[1]{\edef\tmp{#1}\alphabubblesort{\tmp}\textsuperscript{\sortedlist}}
\newcommand{\red}{R}
\newcommand{\blu}{B}
\newcommand{\gre}{G}

\parindent0pt

\newcommand\alphabubblesort[1]{\def\sortedlist{}\expandafter\sortlist#1\relax\relax}
\def\sortlist#1#2#3\relax{%
\let\next\relax
\ifx\relax#2\relax%
\edef\sortedlist{\sortedlist#1}%
\else
\ifnum#1<#2\relax%
\edef\sortedlist{\sortedlist#1}%
\def\next{\sortlist#2#3\relax}%
\else%
\ifnum#1>#2\relax%
\let\tmp\sortedlist%
\def\sortedlist{}%
\def\next{\expandafter\sortlist\tmp#2#1#3\relax}%
\fi%
\fi%
\fi%
\next
}
\begin{document}
\section{the order I will use}
test 1\x{\red\gre} \\
test 2\x{\blu\red} \\
test 3\x{\gre\blu}
\section{the order I should use/what should be displayed}
test 1\x{\gre\red} \\
test 2\x{\blu\red} \\
test 3\x{\blu\gre}

\textbf{To use just the sorter:}

\def\mydata{vkp!s45J[YO}
ASCII sorted data for \mydata{} is obtained simply with
\alphabubblesort{\mydata}\sortedlist.
\end{document}


• At first, thank you very much for this solution. So instead of yousing \newcommand for the definition of the colors I should use \def? – Pratched May 24 '18 at 21:34
• furthermore: It does not seem to be possible to use one sortedlist within another. I adjusted this code for normal text as well. If calling alphabubblelist for ...{\colorlist1\colorlist2\colorlist3} but those commands are calling alphabubblelist themself e.g. \newcommand{\colorlist1}[1]{List1\x{\red\blu}} a few errors occure. Obviously I could write different sortedlists, but maybe there is a way around. – Pratched May 24 '18 at 22:22
• @Djiwahwah To your first question, just using RGB would actually be easier than using macros for the colors. Because of this, I had to expand the argument before sorting it. – Steven B. Segletes May 24 '18 at 23:35
• @Djiwahwah The 2nd question is more challenging, as the sort routine needs its argument expanded (and does so once in preparation). But with nested arguments as you describe, you end up trying to sort the macronames, rather than the underlying RBG letters that they represent. I will give it some thought, but I fear there is no simple fix. – Steven B. Segletes May 24 '18 at 23:39

Regarding the answer of Steven B. Segletes I expanded the question looking for a nested sorting function. This should be able to sort the macros of type X given in \sortedNormal as well as the macros of type Y given in \sortedSuperscript within those first macros of type X.

Writing this answer I realize, that this might be too much, especially when the macros of type X should be separated by comma. Which means I have to write to different sortedLists anyway, since macros of type Y should not be separated by comma.

So if there is a solution I appreciate the help, but maybe this gets out of hand.

\documentclass{article}

\usepackage[utf8]{inputenc}

\newcommand{\sortedSuperscript}[1]{\edef\tmp{#1}\alphabubblesort{\tmp}\textsuperscript{\sortedlist}}
\newcommand{\sortedNormal}[1]{\edef\tmp{#1}\alphabubblesort{\tmp}\sortedlist}

\newcommand{\x}[1]{\sortedSuperscript{#1}}

%% macros of type X
\newcommand{\listA}{ColorlistA\x{\red\blu}}
\newcommand{\listB}{ColorlistB\x{\gre\blu}}
\newcommand{\listC}{ColorlistC\x{\red\gre}}
%% macros of type Y
\newcommand{\red}{R}
\newcommand{\blu}{B}
\newcommand{\gre}{G}

\parindent0pt

\newcommand\alphabubblesort[1]{\def\sortedlist{}\expandafter\sortlist#1\relax\relax}
\def\sortlist#1#2#3\relax{%
\let\next\relax
\ifx\relax#2\relax%
\edef\sortedlist{\sortedlist#1}%
\else
\ifnum#1<#2\relax%
\edef\sortedlist{\sortedlist#1}%
\def\next{\sortlist#2#3\relax}%
\else%
\ifnum#1>#2\relax%
\let\tmp\sortedlist%
\def\sortedlist{}%
\def\next{\expandafter\sortlist\tmp#2#1#3\relax}%
\fi%
\fi%
\fi%
\next
}

\begin{document}
\section{expected result hard coded}
ColorlistA\textsuperscript{BR}, ColorlistB\textsuperscript{BG}, ColorlistC\textsuperscript{RG}

\section{expected result with macros}
\sortedNormal{\listC\listA\listB}

\end{document}

• Generally, once answers have been provided (and in this case accepted), it is better to ask this as a new question, rather than try to edit significantly the meaning of the original question. – Steven B. Segletes May 25 '18 at 9:58