7

I have the following document:

\documentclass[a4paper,10pt]{article}
\usepackage{pgfplotstable, booktabs}

\pgfplotsset{compat=1.14}
\pagenumbering{gobble}

\begin{document}
\begin{center}

\pgfplotstabletypeset [
    col sep = comma,
    every head row/.style={before row=\toprule, after row=\midrule},
    every last row/.style={after row=\bottomrule},
    display columns/0/.style={string type}
    ]{csv1.csv}

\end{center}
\end{document}

It uses a separate file, csv1.csv, as input to produce a table:

Name,Number
Daniel,1
Mary,2
Sarah,3

Suppose I have hundreds of CSV-files and I want to compile one PDF for each of them. The only thing I would need to change in my document is csv1.csv.

  • I think it'd be easier to compile one table per page (assuming they all fit), then split the resulting PDF in single pages with an external tool. – Alenanno Aug 23 '16 at 12:54
  • @Alenanno - I could do that, but then I would need to duplicate 99% of the code one time per CSV. I would prefer to duplicate as little as possible for easier maintenance. – Leif Aug 23 '16 at 12:56
  • You wouldn't need to duplicate anything, actually. – Alenanno Aug 23 '16 at 13:01
  • Oh, cool - I'm pretty new to LaTeX, so I must have misunderstood something in that case. Perhaps you could post it as an answer with an example? – Leif Aug 23 '16 at 13:02
9

Assuming that your .tex file is called mytable.tex, and looks like this:

\ifdefined\mycsvfile
\else
    \def\mycsvfile{csv1.csv}
\fi
\documentclass[a4paper,10pt]{article}
\usepackage{pgfplotstable, booktabs}

\begin{document}
\begin{center}

\pgfplotstabletypeset [
    col sep = comma,
    every head row/.style={before row=\toprule, after row=\midrule},
    every last row/.style={after row=\bottomrule},
    display columns/0/.style={string type}
    ]{\mycsvfile}

\end{center}
\end{document}

then you could use the following (Windows) batch file, bulkpdfs.bat

echo off

echo "batch compiling pdf files"
for %%f in (*.csv) do (
    echo compiling %%f ...
    pdflatex \def\mycsvfile{%%f}\input{mytable.tex} & copy mytable.pdf mytable%%~nf.pdf
)

pause

This will produce mytablecsv1.pdf, mytablecsv2.pdf, ...

For reference, see Two pdf versions from one single .TEX file?

  • Gosh, now I feel like an idiot. I was so stuck on trying to find a solution within LaTeX that I neglected to consider the command-line. I'm on OS X, so I guess I can do the same with xargs. – Leif Aug 23 '16 at 13:46
  • 1
    @Leif yes, indeed; I'm working on Windows at the moment, and can't test an xargs solution, but it would be something like find -name *.csv -print0|xargs -0 pdflatex.... – cmhughes Aug 23 '16 at 14:00
5

Not sure if the code above is a short example or the actual document (from your wording I assume it's the actual one).

If you have multiple csv files — which for this solution must be named with the format csv#.csv, where # is a number — and if the tables all fit one single page without any particular problems, then you can use a loop to typeset them one per page. This answer shows an example of that, but of course, actual application might vary depending on the size of the tables.

Output (single pages)

1 enter image description here    2 enter image description here

Code

\documentclass[a4paper,10pt]{article}
\usepackage{pgfplotstable, booktabs}
\usepackage{filecontents}% just for this example
\usepackage{geometry}

\pgfplotsset{compat=1.13}
\pagenumbering{gobble}

% These two are just for this example. You should have external files anyway.
\begin{filecontents*}{csv1.csv}
Name,Number
Daniel,1
Mary,2
Sarah,3
\end{filecontents*}

\begin{filecontents*}{csv2.csv}
Name,Number
Jack,24
John,5
Matthew,32
\end{filecontents*}
%%%
\begin{document}
\foreach \x in {1,2}{% <-- if you have many csv, you can say e.g. {1,...,45}
    \pgfplotstabletypeset [
        col sep = comma,
        every head row/.style={before row=\toprule, after row=\midrule},
        every last row/.style={after row=\bottomrule},
        display columns/0/.style={string type}
        ]{csv\x.csv}%
    \newpage
}%
\end{document}
  • Thanks for taking the time to write this answer. I didn't know it was possible to create foreach looks in LaTeX like that. I accepted the other answer because it outputs separate PDFs and fits better to the problem at hand, but I learned something valuable from your answer also, so thank you! – Leif Aug 23 '16 at 15:04
5

Here one way how you can do it:

\documentclass[a4paper,10pt]{article}
\usepackage{pgfplotstable, booktabs}

\pgfplotsset{compat=1.14}
\pagenumbering{gobble}

% Create your own command 
\newcommand\MyTable[1]{\pgfplotstabletypeset[
    col sep = comma,
    every head row/.style={before row=\toprule, after row=\midrule},
    every last row/.style={after row=\bottomrule},
    display columns/0/.style={string type}
    ]{#1.csv}\newpage
}

\begin{document}
\begin{center}

\MyTable{csv1}
\MyTable{csv2}

\end{center}
\end{document}
  • This is beautiful - I didn't know it was possible to create new commands with parameters like that. I accepted the other answer because it fits better what I'm trying to achieve right now, but I learned a lot from this answer also, so thank you. – Leif Aug 23 '16 at 15:03

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.