# Export Plot from Table to $\LaTeX$ — conform with pgfplots

## General purpose

My intension is to use data created by Mathematica as in

but with a nicer $\LaTeX$ conform font style and similar stuff. This is the reason why I am using the $\LaTeX$ package pgfplots. This package also allows me to add further information and fancy graphical stuff (by using TikZ) to the plot at later time.

## Issue

Now I do not get Mathematica to write me a data-file which can be read by the $\LaTeX$ package pgfplots. The current data (and export) has the following structure:

DataTable = {{9/50, 5.00251*10^9, 3.02252*10^9, 2.17472*10^9, 1.97472*10^9},<<1000>>};
Export["Energy.csv", DataTable];


The idea is to plot in the end something like

\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[%
xlabel={xlabel},
ylabel={ylabel}]
table [x expr=\thisrowno{0}, y expr=\thisrowno{1}, col sep=space] {data.dat};
table [x expr=\thisrowno{0}, y expr=\thisrowno{2}, col sep=space] {data.dat};
\end{axis}
\end{tikzpicture}
\end{document}


## EDIT:

The $\LaTeX$ routine pgfplots uses the following data structure (where the option col sep=space|tab|comma|colon|semicolon|braces|&|ampersand defines the separation character - same is also vaid for the column separation row sep=newline|\\):

Field,Energy1,    Energy2,    Energy3,    Energy4
9/50, 5.00251e+09,3.02252e+09,2.17472e+09,1.97472e+09
... (all data like this first row of data)


You might want to check out chapter 4.3.2 (Reading Coordinates From Tables) in the pgfplots manual which contains all important informations. By the way the header is not necessary but helps other readers to understand your data.

-
So, what structure would be required for this to be possible, for those of us without a familiarity/working installation of $\LaTeX$? –  Oleksandr R. Jan 4 at 0:12
The "CSV" export of Mathematica creates exactly the structure you like, where one line is exported as 9/50,5.00251e9,3.02252e9,2.17472e9,1.97472e9. In you pgfplots code, you used data.dat which should be data.csv with your current approach. What exactly is the issue? –  halirutan Jan 4 at 13:37
@halirutan: It was a stupid error that occured because I did not choose the correct file (I still used the data.dat file. You are completely right. +1 I will rephrase an answer out of my question for other interested people (as I invested too much time in that issue). –  strpeter Jan 4 at 16:46
@strpeter No problem. This happens to everyone once in a while. The good thing is, that it works now. –  halirutan Jan 4 at 16:47

## Mathematica export

Just export the data such that in one column you have the x-coordinates and in another you have the y-coordinates.

DataTable = {{9/50, 5.00251*10^9, 3.02252*10^9, 2.17472*10^9, 1.97472*10^9},<<1000>>};
DataTableWithHeader = Flatten[{{{"ColumnX", "ColumnY1", "ColumnY2", "ColumnY3", "ColumnY4"}}, DataTable}, 1];


## $\LaTeX$ import and compiling

You now can use a compiler like pdflatex to create your plots from a $\LaTeX$-file like the folowing - like you desire.

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
xlabel={xlabel},
ylabel={ylabel}]