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 a function which grabs cells out of a table and returns them. Many of these cells end with two asterisks, and I would not like these returned from the function if they are present. For example:

% Function to get data
\def\getdata#1#2{%
\pgfplotstablegetelem{#1}{#2}\of{\table}\pgfplotsretval%
}

If this returns a value such as "2.3**", I would like "2.3". Thanks, I assume this is rather straightforward.

share|improve this question
    
I'm sure someone will come up with a LaTeX solution. But in the mean time, I'd approach this with a shell script in perl or ruby or something that can use regular expression replacement and run the data through the shell script first. –  A.Ellett Dec 9 '12 at 3:23
    
That could be good -- I want the tables themselves to have the asterisks in them though. I also have no idea how to do that. –  mike Dec 9 '12 at 3:25
1  
If you provide a sample of how the data is formatted, I could try to write up a perl script. –  A.Ellett Dec 9 '12 at 3:25
1  
You could also use the xstring package and the \StrDel{<text that has * removed>}{*} macro. –  Qrrbrbirlbel Dec 9 '12 at 3:51
add comment

2 Answers 2

up vote 3 down vote accepted

pgfplotstable offers a possibility to ignore a list of characters while the data is being read from the table via ignore chars={} key.

\documentclass{article}
\usepackage{pgfplotstable}
\begin{filecontents*}{scientists.csv}
name,surname,age
Alb*ert,Einstein**,133
Marie**,Curie,145
Thomas,Edis**on,165**
\end{filecontents*}

\pgfplotstableread[col sep=comma,ignore chars={*}]{scientists.csv}\mytable
\def\getcell#1#2#3{%
\pgfplotstablegetelem{#1}{#2}\of{#3}\pgfplotsretval%
}
\begin{document}
\pgfplotstabletypeset[
    string type,
    columns/name/.style={column name=Name, column type={|l}},
    columns/surname/.style={column name=Surname, column type={|l}},
    columns/age/.style={column name=Age, column type={|c|}},
    every head row/.style={before row=\hline,after row=\hline},
    every last row/.style={after row=\hline},
    ]\mytable

\bigskip
\getcell{0}{name}{\mytable} \getcell{0}{surname}{\mytable} is \getcell{0}{age}{\mytable} 
years old. \getcell{1}{name}{\mytable} \getcell{1}{surname}{\mytable} is 
\getcell{1}{age}{\mytable} years old. But \getcell{2}{name}{\mytable} 
\getcell{2}{surname}{\mytable} is still older, he is \getcell{2}{age}{\mytable} years old.
\end{document}

enter image description here

However, one should be careful since this method eats every instance of asterisk. For example Alb*ert is also modified. So one must be careful (x2).

share|improve this answer
    
Awesome, thanks! To be clear (might want to update answer), the intention was to have the asterisks in the table, but not the text, so my solution was to just read in the table twice, one being \pgfplotstableread[col sep=comma,ignore chars={*}]{scientists.csv}\nostartable –  mike Dec 9 '12 at 17:15
1  
@mike That's a totally different question :) I would just use xstring package with String Substitution commands. –  percusse Dec 9 '12 at 17:51
add comment

I'm sure someone will come up with a LaTeX solution. But in the mean time, I'd approach this with a shell script in perl or ruby or something that can use regular expression replacement and run the data through the shell script first.

Here's a shell script (in perl) that will strip off *. Save this script in a file and call it script.pl. I'll assume that the name of the data file is data.txt. At the command prompt, run

$ perl script.pl data.txt

This will print the reformated data to the console. To capture this in another file:

$ perl script.pl data.txt > reformatted_data.txt

That way you still have the original to do with as you wish and the data without the astricks.

Here's the perl script:

#!/usr/bin/perl
use strict 'vars';
&MAIN(@ARGV);
sub MAIN {
   my ($filehandle) = @_;
   open FILE, "<$filehandle";
   my @filecontents = <FILE>;
   close FILE;

   foreach my $line ( @filecontents ){
      $line =~ s/\*//g;
      print $line;
    }

}

Hope this helps.

share|improve this answer
    
That's not really what the OP wants, but sed -i 's/\*//g' data.txt does the equivalent of your perl script in one line. –  Mafra Dec 9 '12 at 14:57
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.