Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
1  
You may be interested in tex.stackexchange.com/questions/12668/… –  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 9:22
1  
Sounds cool! You can answer and accept your own question. –  Christian Feuersänger Mar 18 '13 at 15:52

1 Answer 1

up vote 1 down vote accepted
\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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.