2

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.

enter image description here

Pratched
  • 409

2 Answers2

4

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}

enter image description here

  • 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
  • @Djiwahwah This owrks: \newcommand{\colorlistA}{\red\blu} \newcommand{\colorlistB}{\gre} Test 4\x{\colorlistA\colorlistB} as long as no two colors are repeated in the list. Note you can't call a macro \colorlist1, as it can't contain a number, Also, your defn of \colorlist1 called for an argument which you did not provide. – Steven B. Segletes May 24 '18 at 23:51
  • I dont know if I understood your first answer correctly: R, G,B are only examples and I am actually using long words or even a group of words for every command like \red. – Pratched May 25 '18 at 08:20
  • But concerning the nested problem I did not make myself clear. I will add an answer with another working example, which should explain my problem even better than I am able within the comment section. – Pratched May 25 '18 at 08:26
  • @Djiwahwah I am afraid that this current answer is set up to expand the macros and to sort character by character (since that is how your question was phrased), rather than word by word. However, the alpha-bubble sorter I referenced in my answer (https://tex.stackexchange.com/questions/372639/trying-to-eliminate-stack-overflow-during-recursion) is capable of sorting alphabetically a comma separated list, which would be a better way to handle input of words or phrases. – Steven B. Segletes May 25 '18 at 09:55
0

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}
Pratched
  • 409
  • 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 09:58