3

Suppose I have a file numbers.txt, whose contents are

x    y
1    2
3    4
50   20

I want to put this into a table using pgfplotstable, but while these numbers can change I want an additional column containing names for the rows (and these are just math symbols). The actual numbers in my txt file will change every so often, but what they represent will remain constant. What I want is a way to combine this txt file, i.e. my 'data' if you will, with another input containing the names of the rows.

Here is an example. The first table I can easily create using pgfplotstable, but what I really want is the second table.

\documentclass{article}
\usepackage[english]{babel}
\usepackage{lmodern}
\usepackage{amsmath}
\usepackage{pgfplotstable}

\begin{document}
\begin{table}
\centering
\pgfplotstabletypeset{numbers.txt}
\caption{pgfplotstable}
\end{table}

\begin{table}
\centering
\begin{tabular}{ccc}
& x & y \\
$\lambda$ & 1 & 2 \\
$\beta$ & 3 & 4 \\
$\gamma$ & 50 & 20
\end{tabular}
\caption{Target table}
\end{table}
\end{document}

My goal is then to use pgfplotstable to combine my txt file with these row names. How can I do this? I have tried just adding a column with $\lambda$, $\beta$ and $\gamma$ (also enclosed in curly brackets) and then going from there, but that didn't seem to work (I'm assuming the math stuff creates some issues in interpreting it).

4

You can create and store your symbol column somewhere in your document or you can even read it off from a text file.

\documentclass{standalone}
\usepackage{pgfplotstable,filecontents}

\begin{filecontents*}{data.txt}
x    y
1    2
3    4
50   20
\end{filecontents*}

\pgfplotstableset{
  create on use/symb/.style={
    create col/set list={$\alpha$,$\beta$,$\gamma$}
  },
  columns/symb/.style={string type}
}

\begin{document}
\pgfplotstabletypeset[
columns={symb,x,y},
]{data.txt}
\end{document}

enter image description here

  • Thanks! That seems to be what I'm looking for. Some follow-up questions: 1) Is there a way to suppress 'symb' in the first row? 2) In my "real" table, I don't have integers. My txt file has numbers of the form x.xx, but when it produces the tables some are cropped (so 3.00 is displayed as 3) and some are displayed as scientific numbers 6*10^(-2) instead of 0.06. Is there an easy fix for this? – hejseb Oct 8 '15 at 5:43
  • 1) is no longer needed, as I actually needed to name the column anyway. For 2), I managed to fix it by adding column/x/.style={fixed zerofill,precision=2}, and column/y/.style={fixed zerofill,precision=2}, but for some reason every column/.style={fixed zerofill,precision=2} did not seem to work. In my real table, I have eight columns. Any idea how to do this for all columns in one line, rather than copying and pasting eight times? – hejseb Oct 8 '15 at 6:57
  • @hejseb There was a recent question you can use that one too tex.stackexchange.com/questions/272512/… – percusse Oct 12 '15 at 21:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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