4

I have many tab separated csv files which I would like to convert into tables automatically in tabular environment of latex. Tables are named: log_% where % can be one of the symbols \in {at,be,fr,....}. For example each file looks like below:

 
accuracy    timepred    timelearn   timelearn2  timelearn4  rmsetrain
0.987747    0.532   2.44    nan nan 0.987676
0.987747    0.528   2.396   nan nan 0.987676
0.987747    0.544   2.408   nan nan 0.987676
0.987747    0.496   2.388   nan nan 0.987676
0.987747    0.436   2.432   nan nan 0.987676
0.987747    0.428   2.444   nan nan 0.987676
0.987747    0.464   2.452   nan nan 0.987676
0.987747    0.428   2.452   nan nan 0.987676
0.987747    0.464   2.46    nan nan 0.987676
0.987747    0.484   2.448   nan nan 0.987676

I tried the below code which unfortunately does not work!

\documentclass{article}
\usepackage{xparse}% http://ctan.org/pkg/xparse
\usepackage{csvsimple}
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\usepackage{array}
\usepackage{pgfplotstable}
\newcounter{listtotal}\newcounter{listcntr}%
\NewDocumentCommand{\names}{o}{%
  \setcounter{listtotal}{0}\setcounter{listcntr}{-1}%
  \renewcommand*{\do}[1]{\stepcounter{listtotal}}%
  \expandafter\docsvlist\expandafter{\namesarray}%
  \IfNoValueTF{#1}
    {\namesarray}% \names
    {% \names[]
     \renewcommand*{\do}[1]{\stepcounter{listcntr}\ifnum\value{listcntr}=#1\relax##1\fi}%
     \expandafter\docsvlist\expandafter{\namesarray}}%
}

\begin{document} \newcommand{\namesarray}{at,br,cz,dk,fi,ie,nb,no,pt,se,be,ch,de,es,fr,it,nl,pl,ru,uk}% \foreach \i in {0,...,20}{ \newcommand{\name}{\names[\i]} \name \csvautotabular[separator=tab]{logs/log_\names[\i]} \par } \end{document}

By executing above code I get an error:


! Package csvsimple Error: File 'logs/log_\names [0]' not existent, not readable, or empty!.

The same example works by hard coding the symbol as follows:


\csvautotabular[separator=tab]{logs/log_at} \par

in place of


\csvautotabular[separator=tab]{logs/log_\names[\i]} \par

I uploaded the working solution by hardcoding on the following link: goo.gl/nbI5TQ
I believe the problem is that macro \i gets expanded and tabular environment fails to expand \names. But I am not sure. Is it possible to expand the macro \names[\i] as a group and not just \i?
I would appreciate any kind of minimal help to solve my problem.

Thanks :)

  • What do you mean that you uploaded the working solution? If you've solved the problem, what are you asking exactly? – cfr Nov 14 '16 at 0:22
  • Working solution is the one I obtained by hardcoding the symbol at in place of \names[\i]. What I wanted was a generalized solution with symbols - at,br,...,etc. expanded in for loop. – Sumit Sidana Nov 14 '16 at 0:26
  • Your code does not give the error you report - it doesn't get that far. Do you need to load pgffor? – cfr Nov 14 '16 at 0:28
  • well actually, pgfplotstable. I will edit. – Sumit Sidana Nov 14 '16 at 0:31
  • This seems excessively complicated given that all you seem to want to do is to loop over the files. I don't see the point of most of it at all. – cfr Nov 14 '16 at 1:05
4

The code seems overly complex. If you just want to loop over the list of files, I think you just need a loop from etoolbox, say, and the relevant command from csvsimple.

For example with logs/log_at:

accuracy        timepred        timelearn       timelearn2      timelearn4      rmsetrain                                                           
0.987747        0.532   2.44    nan     nan     0.987676                                                                                            
0.987747        0.528   2.396   nan     nan     0.987676                                                                                            
0.987747        0.544   2.408   nan     nan     0.987676                                                                                            
0.987747        0.496   2.388   nan     nan     0.987676                                                                                            
0.987747        0.436   2.432   nan     nan     0.987676                                                                                            
0.987747        0.428   2.444   nan     nan     0.987676                                                                                            
0.987747        0.464   2.452   nan     nan     0.987676                                                                                            
0.987747        0.428   2.452   nan     nan     0.987676                                                                                            
0.987747        0.464   2.46    nan     nan     0.987676                                                                                            
0.987747        0.484   2.448   nan     nan     0.987676                                                                          

and logs/log_br:

accuracy        timepred        timelearn       timelearn2      timelearn4      rmsetrain                                                           
0.987747        0.532   2.44    nan     nan     0.987676                                                                                            
0.987747        0.528   2.396   nan     nan     0.987676                                                                                            
0.987747        0.544   2.408   nan     nan     0.987676                                                                                            
0.987747        0.496   2.388   nan     nan     0.987676                                                                                            
0.987747        0.436   2.432   nan     nan     0.987676                                                                                            
0.987747        0.428   2.444   nan     nan     0.987676                                                                                            
0.987747        0.464   2.452   nan     nan     0.987676                                                                                            
0.987747        0.428   2.452   nan     nan     0.987676
0.987747        0.464   2.46    nan     nan     0.987676
0.987747        0.484   2.448   nan     nan     0.987676

The .tex code

\documentclass{article}
\usepackage{csvsimple}
\usepackage{etoolbox}
\providecommand*\do{}
\begin{document}
\renewcommand*\do[1]{%
  #1
  \csvautotabular[separator=tab]{logs/log_#1}\par
}
\docsvlist{at,br}%,cz,dk,fi,ie,nb,no,pt,se,be,ch,de,es,fr,it,nl,pl,ru,uk}%
\end{document}

produces the following output

working output

Note that this code assumes files are called logs/log_<xx>.tex or that the files are logs/log_<xx>. If they have a different extension, you need to specify it.

  • Well yes files are logs/log_<xx> except the fact that they do not have any extension. – Sumit Sidana Nov 14 '16 at 1:11
  • I get an error missing $ inserted if I ignore the created files. – Sumit Sidana Nov 14 '16 at 1:14
  • I ran it without the code to create the files and moved those I made earlier to logs/, deleting the extensions. So I have logs/log_at and logs/log_br. I then recompiled the code and it works fine for me. Apparently, it will check for the file name without extension, as well as with the default extension .tex. So it should work fine for your case. Please check with a minimal set up using my code i.e. make a clean directory and create logs/log_at and logs/log_br with the specified contents. Then put my code (without the file creation code) in a .tex file and compile. – cfr Nov 14 '16 at 1:23
  • @Sumit If you still get an error in that case, please add \listfiles as the first line and post the log file in your question. If not, the problem must lie elsewhere. – cfr Nov 14 '16 at 1:24
  • It works with a clean directory and I could reproduce the output you produced, but is not working with my files for some reason – Sumit Sidana Nov 14 '16 at 1:38
2

Be aware that

  1. \csvautotabular[separator=tab] means that you need to use tabs instead of spaces for separating items within a line of the csv file.

  2. the way in which TeX does process filenames is not the same on all platforms.
    E.g., when using MiKTeX 2.9 under Windows 7, TeX always assumes the filename-extension ".tex" if no filename-extension is provided. If the filename in question does not have an extension at all, you need to provide the dot (with no extension) anyway.
    E.g. if you wish to specify the file "log_at" in the current directory's sub-directory "logs", you need to write ./logs/log_at. . (If you specify ./logs/log_at (without trailing dot), (MiK)TeX will seek for the file "log_at.tex" in the current directory's sub-directory "logs".

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.