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.

A computed column, columnWithFloatingPointNumbers, is added to the table, \table. Then I am trying to compute geometric mean of this newly-added column. However I get this error:

! Package PGF Math Error: Unknown operator `Y' or `Y1' (in '1Y1.1328e1]').

If I use \pgfmathprintnumberto, I get this error:

! Package PGF Math Error: Could not parse input '$11.33$' as a floating point number, sorry. The unreadable part was near '$11.33$'..

Here is MWE:

\documentclass{article}
\usepackage{pgfplotstable}
\usepackage{multirow}

\begin{filecontents}{table.txt}
cA  P   Sp
A   16  15.1
B   4   3.2
C   8   6.7
D   4   3.5
E   16  10.9
F   8   7.2
\end{filecontents}


\begin{document}
\pgfplotstableread{table.txt}\table

\pgfplotstablecreatecol[create col/assign/.code={
\global\edef\tp{\thisrow{Sp}}
\global\edef\p{\thisrow{P}}
\pgfkeys{/pgf/fpu=true}
\pgfmathparse{\tp * 14.16 / \p}
\pgfkeys{/pgf/fpu=false}
\global\edef\entry{\pgfmathresult}
%\pgfmathprintnumberto[/pgf/number format/.cd,fixed,precision=2]{\entry}{\entryb}
\pgfkeyslet{/pgfplots/table/create col/next content}\entry
}]{columnWithFloatingPointNumbers}\table

\pgfplotstabletypeset[columns={cA, P, Sp,columnWithFloatingPointNumbers }, columns/cA/.style={string type},columns/cB/.style={string type},columns/columnWithFloatingPointNumbers/.style={column name={FP}, column type=l, numeric type}]\table 
\end{document}

Here is MWE with geomean part: (I need the geomean part to work with the column columnWithFloatingPointNumbers)

\documentclass{article}
\usepackage{pgfplotstable}
\usepackage{multirow}

\begin{filecontents}{table.txt}
cA  P   Sp
A   16  15.1
B   4   3.2
C   8   6.7
D   4   3.5
E   16  10.9
F   8   7.2
\end{filecontents}


\begin{document}
\pgfplotstableread{table.txt}\table

\pgfplotstablecreatecol[create col/assign/.code={
\global\edef\tp{\thisrow{Sp}}
\global\edef\p{\thisrow{P}}
\pgfkeys{/pgf/fpu=true}
\pgfmathparse{\tp * 14.16 / \p}
\pgfkeys{/pgf/fpu=false}
\global\edef\entry{\pgfmathresult}
%\pgfmathprintnumberto[/pgf/number format/.cd,fixed,precision=2]{\entry}{\entryb}
\pgfkeyslet{/pgfplots/table/create col/next content}\entry
}]{columnWithFloatingPointNumbers}\table

\pgfplotstabletypeset[columns={cA, P, Sp,columnWithFloatingPointNumbers }, columns/cA/.style={string type},columns/cB/.style={string type},columns/columnWithFloatingPointNumbers/.style={column name={FP}, column type=l, numeric type}]\table 

\foreach \col in {P} {% I want geomean for the column  columnWithFloatingPointNumbers
    \pgfmathsetmacro\sum{0}%
    \foreach \x in {1,2,...,6} {%           
        \pgfmathtruncatemacro\ir{\x-1}  % get number without .0
        \pgfplotstablegetelem{\ir}{\col}\of\table%
        \pgfmathsetmacro{\val}{\pgfplotsretval}%
            \pgfkeys{/pgf/fpu=true}%
                \pgfmathparse{\sum + (ln \val)}%
            \pgfkeys{/pgf/fpu=false}%
            \global\let\sum\pgfmathresult%
    }%
    \pgfkeys{/pgf/fpu=true}%
    \pgfmathparse{\sum /  6} %
    \pgfkeys{/pgf/fpu=false}%
    \global\let\sum\pgfmathresult%
    \pgfkeys{/pgf/fpu=true}%
    \pgfmathparse{exp \sum}%
    \pgfkeys{/pgf/fpu=false}%
    \global\let\sum\pgfmathresult%
    \pgfmathprintnumberto{\sum}{\fsum}%
    Geomean: \fsum
}%
\end{document}
share|improve this question
    
The example doesn't compile to begin with: the table has no cB column, for instance. It's anyway a bad idea to globally set \sum, which is a LaTeX command; use \Sum or something else. –  egreg Apr 18 '13 at 9:51
    
Dear @egreg, thanks for the remark about \Sum. I have just switched to TexStudio and it was running in nonstop mode, so I have missed removing the columns while I was trying to minimize the sample code. –  Kadir Apr 18 '13 at 9:59
    
I get ! Package PGF Math Error: Unknown operator Y' or Y1' (in '1Y1.33635e1]'). –  egreg Apr 18 '13 at 10:28
    
Dear @egreg, I have added one more MWE and updated them: The first MWE does not calculate geomean, the second MWE calculates geomean but it calculates geomean of another column, P. I need the geomean part to work with the column columnWithFloatingPointNumbers. Regards. –  Kadir Apr 18 '13 at 13:06
add comment

1 Answer

up vote 3 down vote accepted

The problem is with the way you generate the final column. Using creat col/expr is much simpler, uses the floating point library, and produces the correct type of result.

Sample output

\documentclass{article}
\usepackage{pgfplotstable}
\usepackage{multirow}

\begin{filecontents}{table.txt}
cA  P   Sp
A   16  15.1
B   4   3.2
C   8   6.7
D   4   3.5
E   16  10.9
F   8   7.2
\end{filecontents}

\begin{document}
\pgfplotstableread{table.txt}\mytable

\pgfplotstableset{
  create on use/columnWithFloatingPointNumbers/.style={
    create col/expr={\thisrow{Sp}*14.16/\thisrow{P}}}
}

\pgfplotstabletypeset[columns={cA,P,Sp,columnWithFloatingPointNumbers},
  columns/cA/.style={string type},
  columns/cB/.style={string type},
  columns/columnWithFloatingPointNumbers/.style={
  column name={FP}, column type=l, numeric type}]\mytable 

\foreach \col in {P,Sp,columnWithFloatingPointNumbers} {
  \pgfmathsetmacro\mysum{0}%
  \pgfplotstableforeachcolumnelement{\col}\of\mytable\as\val{%
     \pgfkeys{/pgf/fpu=true}%
         \pgfmathparse{\mysum + (ln \val)}%
     \pgfkeys{/pgf/fpu=false}%
     \global\let\mysum\pgfmathresult%
  }%
  \pgfplotstablegetrowsof\mytable
  \pgfkeys{/pgf/fpu=true}%
    \pgfmathparse{exp(\mysum / \pgfplotsretval)}%
  \pgfkeys{/pgf/fpu=false}%
  \global\let\mysum\pgfmathresult%
  \pgfmathprintnumberto{\mysum}{\fsum}%
  Geomean of \col: \fsum\par
}%

\end{document}

In the above code I have also tried to avoid reusing names of standard latex commands, have used pgfplotstable's commands for running over elements of a column and have shortened your mathematics computations.

share|improve this answer
    
Dear @AndrewSwann, thanks for the solution with create col/exp! When I need to retrieve value from another table (e.g. using \pgfplotstablegetelem{\ir}{\col}\of\table), then how I can achieve both this and taking geomean? –  Kadir Apr 20 '13 at 13:19
    
Your looping code with \pgfplotstablegetelem still works, provided the table element is created correctly in the first place. I used \pgfplotstableforeachcolumnelement merely for efficiency. –  Andrew Swann Apr 21 '13 at 9:48
add comment

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.