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.

I have two tables (sorry, inline tables are not working with pgfplotstable 2011/12/19 on my system, even with the option format=inline):

  1. Data table (data.csv):

    X,A,B
    U,1.1,1.2
    V,2.1,2.2

  2. References table (references.csv):

    X,A,B
    U,ref1,ref2
    V,ref2,ref1

I want to read both tables with PgfplotsTable and create a new table with {cell content}={cell content of data.csv}\citep{cell content of references.csv}. I tried to do this with pgfkeys in the preprocessing step, but was not able to get it working this way. Obviously the macro \ori does not get expanded in the argument of \pgfkeyssetvalue. Therefore I tried to expand only the second argument of \pgfkeyssetvalue using \exp_args:Nnx from package expl3:

\documentclass{article}
\usepackage[numbers]{natbib}
\usepackage{pgfplotstable}
\usepackage{ifthen}
\usepackage{expl3}
%
\begin{document}
    \pgfplotstableread[col sep=comma, trim cells=true]{data.csv}\data
    \pgfplotstableread[col sep=comma, trim cells=true, header=false]{references.csv}\references
    \pgfplotstabletypeset[
        string type,
        preproc cell content/.append code={%
            \pgfmathtruncatemacro{\col}{\pgfplotstablecol-1}
            \ifthenelse{\col>0}{
                \pgfmathtruncatemacro{\row}{\pgfplotstablerow+1}
                \pgfplotstablegetelem{\row}{\col}\of{\references}
                \pgfkeysgetvalue{/pgfplots/table/@cell content}{\ori}
                \expandafter\def\expandafter\ori\expandafter{\ori\citep{\pgfplotsretval}}
                \ExplSyntaxOn
                \exp_args:Nnx \pgfkeyssetvalue {/pgfplots/table/@cell content} {\ori}
                \ExplSyntaxOff
            }{}
        },
    ]\data
    \begin{thebibliography}{9}
        \bibitem{ref1} ref1 
        \bibitem{ref2} ref2 
    \end{thebibliography}
\end{document}

With this code I obtain the following errors (just one example here):

! Undefined control sequence.
\pgfplotstable@result ...ular}{ccc}X&A&B \\U&\ori 
                                                  &\ori \\V&\ori &\ori \\\en...
l.24    ]\data

The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.

What I'm doing wrong and how is the best way to do such a table operation?

share|improve this question
    
Welcome to TeX.SX. Great question. The problem is most likely (from what I can see) that the macro \ori does not get expanded upon \pgfkeyssetvalue{/pgfplots/table/@cell content}{\ori}. You should then also expand that into tokens. Try and create a minimal example (without scrbook and all unnecessary packages. That will make it easier for other users to help you! :) Enjoy your stay here! –  zeroth Feb 22 '12 at 9:36
    
Thanks for your answer. I tried to minimize the example and expand \ori in the argument of \pgfkeyssetvalue - see edited question. However, I'm still glueless about what's really going wrong here... –  bonanza Feb 22 '12 at 19:45

1 Answer 1

up vote 2 down vote accepted

There are several issues in trying to edit the table in place. First of all \pgfkeyssetvalue{/pgfplots/table/@cell content}{\ori} will set the content to the macro \ori. The problem occurs due to preproc cell content is done within a group and thus makes \ori unreachable outside.

This is also true for any other macro usages within that piece of code you have.
Once you get past one level of expansion in \ori you will reach \pgfplotsretval which is also only defined within your code piece. \citep is not a problem as it is defined outside of the group.

What you need to do is fully expand you arguments and thus use \pgfkeyssetvalue with an argument of macros defined outside of the process group, i.e. in the typeset group. Thus the solution will be:

\pgfplotstabletypeset[string type,
  preproc cell content/.append code={%
    \pgfmathtruncatemacro{\col}{\pgfplotstablecol-1}
    \ifthenelse{\col>0}{
        \pgfmathtruncatemacro{\row}{\pgfplotstablerow+1}
        \pgfplotstablegetelem{\row}{\col}\of{\references}
        \pgfkeysgetvalue{/pgfplots/table/@cell content}{\ori}
        % Be sure to have \ori contain the fully expanded values, we do not want to expand \citep
        \edef\ori{\ori\noexpand\citep{\pgfplotsretval}}
        % edef the key so that you do not have any reference to \ori.
        % This is just instead of many \expandafter's
        \edef\tmp{%
            \noexpand\pgfkeyssetvalue{/pgfplots/table/@cell content}{\expandafter\unexpanded\expandafter{\ori}}
        }\tmp
    }{}
  }]\data

This will yield (I have used \cite instead of your natbib equivalent)

enter image description here

share|improve this answer
    
Great! It works so far and I understand how it works in principle. However, there seems to be another issue with \pgfplotstableread. In spite of setting trim cells=true I obtain always a space character appended to the cell contents in the last row of the table. This leads to undefined references (see image attached to the answer). Obviously there is also something different in row counting. –  bonanza Feb 23 '12 at 9:07
    
@bonanza please update your question to reflect this. I did not quite understand what you changed in order to get your image? In any case the space should be inserted due to the \cite and thus the undefined reference. Maybe I forgot a % termination on each line, try that. –  zeroth Feb 23 '12 at 9:35
    
I changed nothing in your code (except replacing \citep by \cite). Of course there should be a space between the data and the reference in the final output - but not in the cell content after \pgfplotstableread. Maybe I should open a new question since this seems to be related to some other issue with PgfplotsTable? I also wonder why inline tables are not working on my system... –  bonanza Feb 23 '12 at 11:06
    
@bonanza try just place a comma in the cvs, which should constrain the field. –  zeroth Feb 23 '12 at 11:47
    
appending an additional comma to each line in the references table file resolved the undefined references. But this does not explain why on your system pgfplotstable start column counting (sorry for confusing rows with columns in my first comment) in the data table at zero and on my system at one (That's why I have to set \pgfmathtruncatemacro{\col}{\pgfplotstablecol-1}). –  bonanza Feb 23 '12 at 12:16

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.