4

In Sorting according to more than one key in pgfplotstable, I added the column Kind by hand. I need to define mapping between the column Mtx and the column Kind in latex code or in another raw text file. That is, A will map to LP, C will map to OPT, etc.

This is like using HashMap<String, String> in Java.

I include the MWE in the previously-mentioned question here also because after mapping operations, following sorting method will be used:

\documentclass[a4paper]{standalone}

\usepackage{pgfplots}

\usepackage{pgfplotstable}

\begin{document}

\def\pgfplotsinvokeiflessthan#1#2#3#4{%
    \pgfkeysvalueof{/pgfplots/iflessthan/.@cmd}{#1}{#2}{#3}{#4}\pgfeov
}%
\def\pgfplotsmulticmpthree#1#2#3#4#5#6\do#7#8{%
    \pgfplotsset{string <}%
    \pgfplotsinvokeiflessthan{#1}{#4}{%
        % first key <:
        #7%
    }{%
        \pgfplotsinvokeiflessthan{#4}{#1}{%
            % first key >:
            #8%
        }{%
            % first key ==:
            \pgfplotsset{string <}%
            \pgfplotsinvokeiflessthan{#2}{#5}{%
                % second key <
                #7%
            }{%
                \pgfplotsinvokeiflessthan{#5}{#2}{%
                    % second key >
                    #8%
                }{%
                    % second key ==
                    \pgfplotsset{float <}%
                    \pgfplotsinvokeiflessthan{#3}{#6}{%
                        % third key <
                        #7%
                    }{%
                        % third key >=
                        #8%
                    }%
                }%
            }%
        }%
    }%
}%

\pgfplotstabletypeset[
    create on use/sortkey/.style={
        create col/assign/.code={%
            \edef\entry{{\thisrow{Kind}}{\thisrow{Mtx}}{\thisrow{P}}}%
            \pgfkeyslet{/pgfplots/table/create col/next content}\entry
        }
    },
    sort key=sortkey,
    sort cmp={%
        iflessthan/.code args={#1#2#3#4}{%
            \edef\temp{#1#2}%
            \expandafter\pgfplotsmulticmpthree\temp\do{#3}{#4}%
        },
    },
    sort,
    columns/Mtx/.style={string type},
    columns/Kind/.style={string type},
]{
Mtx Kind P   Sp
A   LP   16  4.2
C   OPT  16  72.5
A   LP   64  20.3
B   OPT  16  5.7
B   OPT  64  16.4
A   LP   256 90.4
}
\end{document}
Kadir
  • 1,537
  • 1
  • 11
  • 27
  • 1
    You may be interested in http://tex.stackexchange.com/questions/12668/where-do-i-start-latex-programming/27589#27589 – Christian Feuersänger Mar 17 '13 at 18:21
  • Thanks, I have managed to learn pgfkeys. Following piece of code achieves what I want: \pgfkeys{/mtx/A/.initial={LP},/mtx/C/.initial={OPT}} \pgfplotstablecreatecol[create col/assign/.code={\global\edef\entry{\pgfkeysvalueof{/mtx/\thisrow{MTXA}}} \pgfkeyslet{/pgfplots/table/create col/next content}\entry},]{Kind}\table – Kadir Mar 18 '13 at 09:22

1 Answers1

1
\pgfkeys{/mtx/A/.initial={LP},/mtx/C/.initial={OPT}} 

\pgfplotstablecreatecol[
  create col/assign/.code={
      \global\edef\entry{\pgfkeysvalueof{/mtx/\thisrow{MTXA}}}
      \pgfkeyslet{/pgfplots/table/create col/next content}\entry
  }]{Kind}\table
Kadir
  • 1,537
  • 1
  • 11
  • 27