15. Februar 2009

Combining the advantages of Open Office, Gnuplot and Latex with CSV

Category: Computer — Tags: , , , , , , , , , , , – Kay Smarczewski @ 17:52

I have had to write a record containing some tables and use this for table calculation, gnuplot and latex. So I decided to create a Open Office spreadsheets, export to csv and then convert this file to a Latex includable snippet. Thus a small bash shell script converting csv-files to Latex was needed.

Csv is perfect for this challenge: First, Open Office can export tables to this format (save as…, type: csv, separator: ; ). Second, gnuplot can handle csv data files. You simply have to give it the right information about the used seperator. Finally, the conversion of csv to a Latex snippet is quite easy. I have written a quick and dirty shell script doing this for you:

#!/bin/bash
align="c"
for file in $1; do
newname=`echo $file | sed 's/\(.*\)\..*/\1.tex/'`
echo "Convert CSV-File $file to Tex"
count=`head -1 $file | grep -o ";" | wc -l`
echo "Guess that there are $count columns."
content=`cat $file | sed 's/\;/\t\& /g' | sed 's/$/\\\\\\\\/g' | sed 's/^/\t\t/'`
output="\begin{table}\n\t\centering"

for ((i=0;i<=$count;i++)); do
col=$col"c"
done

output="\begin{table}
\centering
\begin{tabular}{$col}
"$content"
\end{tabular}
\caption{}
\label{}
\end{table}"
echo -n "Write to new file $newname"
echo "$output" > $newname
echo "."
done

You could save this in /usr/local/bin/ as csv2latex for example so you can use it like this:
csv2latex first_data.csv second_data.csv
This will give you the Latex snippets first_data.csv and second_data.csv you can include in your Latex file.
The and are just jump marks for vim so you can easy move to them by typing Ctrl+j. Of course, this will be only useful if you use that editor.
Another big advantage of the ods to csv conversion is that all the format stuff of Open Office is lost (Yes, I think this is a great advantage).

Note: I cannot give any warrenty that this programm has the expected effect. It is possible that your system suffers damage because of the use of this program! Please check the script for yourself!

Update: I find it easier to copy-paste a highlighted range in Open Office direct into vim. Then you can search and replace all commas by points to satisfy the GnuPlot default decimal separator:
:%s/,/./g
The pasted columns are separated by a tab and that is the same as the Gnuplot default. So you can give that file to GnuPlot without any further steps.

1 Kommentar »

  1. Pretty good stuff!
    Getting rid of the OOfice junk is easy now.
    OOfice is easy typing and spell checking then vacuum the junk under the rug.

    Thanks

    Kommentar von Pete — 21. Juli 2009 @ 12:48

RSS Feed für Kommentare zu diesem Artikel. | TrackBack URI

Hinterlasse einen Kommentar

XHTML ( You can use these tags):
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> .

Kay Smarczewski


mein Blog

Was beschäftigt mich gerade...

  • Alles in allem: Sensationeller und unvergesslicher Tag! #Leben 6 hrs ago
  • Im dritten Spiel gegen Möbius/Zielke etwas die Spannung verloren und deswegen im 3. Satz knapp unterlegen #Beachvolleyball #Leipzig 6 hrs ago
  • Auf dem Center Court gegen Becker/Drößler super gespielt und knapp verloren #Leipzig #Beachvolleyball 6 hrs ago
  • More updates...

Posting tweet...

Powered by Twitter Tools

Kalender mit Posts

Februar 2009
M D M D F S S
« Jan   Apr »
 1
2345678
9101112131415
16171819202122
232425262728  

Dieses Blog durchsuchen

© Kay Smarczewski – Powered by WordPress