2

This question already has an answer here:

I have a simple CSV file that looks similar to:

pens, 23
bottles, 5
spoons, 6
chairs, 5
books, 12

I want to convert this into a PDF document. I have no clue on how could I do this. I have installed mactex. It will be helpful, if you help me in getting started with this.

The problem that I am trying to solve is pdf generation. So the input will be a CSV file with around million entries and I want to generate a PDF document.

marked as duplicate by Alan Munn, CarLaTeX, Phelype Oleinik, Stefan Pinnow, user156344 Apr 24 at 14:40

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Welcome to TeX.SX! You only want to put the contents of the CSV into a PDF as it is or do you want to apply some formatting to the data? Can you please clarify what is the output you want? – Phelype Oleinik Apr 24 at 13:09
  • @PhelypeOleinik I want to put the contents of the CSV into a table. So a table could have 2 columns, first containing pens, bottles ,, and another containing corresponding numbers like 23 , 5 .. – Amanda Apr 24 at 13:12
  • Have a look at the csvsimple package. – Harald Hanche-Olsen Apr 24 at 13:15
3

Here are a few more options using csvsimple.

You can use \csvautotabular to let csvsimple build a table for you:

\csvautotabular[
  table head={%
  \hline
    \bfseries Thing & \bfseries Amount \\
  \hline}]%
  {pens_and_bottles.csv}

enter image description here

or, if you load the booktabs package then you can use \csvautobooktabular to get a prettier table:

\csvautobooktabular[
  table head={%
  \toprule
    \bfseries Thing & \bfseries Amount \\
  \midrule}]%
  {pens_and_bottles.csv}

enter image description here

or you can build your own using csvsimple's CSV processor:

\begin{tabular}{lc}
  \toprule
    \bfseries Thing & \bfseries Amount \\
  \midrule
    \csvreader[late after line=\\]{pens_and_bottles.csv}{}%
      {\csvcoli & \csvcolii}
  \bottomrule
\end{tabular}

enter image description here

If you are using a rather large CSV file this will take more than one page, then you can load the longtable package then use \csvautobooklongtable (or build your own longtable, as in the previous example):

\csvautobooklongtable[
  table head={%
  \toprule
    \bfseries Thing & \bfseries Amount \\
  \midrule},
  table foot={\\\bottomrule}]%
  {pens_and_bottles.csv}

enter image description here

Example with the four options:

\RequirePackage{filecontents}
\begin{filecontents*}{pens_and_bottles.csv}
pens, 23
bottles, 5
spoons, 6
chairs, 5
books, 12
\end{filecontents*}

\documentclass{article}
\usepackage{booktabs}
\usepackage{csvsimple}
\usepackage{longtable}
\begin{document}

\csvautotabular[
  table head={%
  \hline
    \bfseries Thing & \bfseries Amount \\
  \hline}]%
  {pens_and_bottles.csv}

\bigskip

\csvautobooktabular[
  table head={%
  \toprule
    \bfseries Thing & \bfseries Amount \\
  \midrule}]%
  {pens_and_bottles.csv}

\bigskip

\begin{tabular}{lc}
  \toprule
    \bfseries Thing & \bfseries Amount \\
  \midrule
    \csvreader[late after line=\\]{pens_and_bottles.csv}{}%
      {\csvcoli & \csvcolii}
  \bottomrule
\end{tabular}

\bigskip

\csvautobooklongtable[
  table head={%
  \toprule
    \bfseries Thing & \bfseries Amount \\
  \midrule},
  table foot={\\\bottomrule}]%
  {pens_and_bottles.csv}

\end{document}
  • I created a PDF using TeXShop. Would you know any command line utility for this? I have installed mactex though but not sure if it comes with a terminal. – Amanda Apr 24 at 14:06
  • @Amanda TeXShop is just the editor. To run from the command line you can save the code as, say, csv_to_pdf.tex and then run pdflatex csv_to_pdf.tex from the command line. – Phelype Oleinik Apr 24 at 14:09
  • Is there a way I could apply conditional colouring to the column values? Have asked a separate question though: tex.stackexchange.com/questions/488364/… – Amanda Apr 30 at 9:24
1

Here an example:

\documentclass{article}
\usepackage{booktabs}
\usepackage{csvsimple}
\usepackage{caption}

% this code is only to create myfile.csv, you don't need it:
\usepackage{filecontents}
\begin{filecontents*}{myfile.csv}
pens, 23
bottles, 5
spoons, 6
chairs, 5
books, 12
\end{filecontents*}

\begin{document}
\begin{table}[htb]\centering
    \caption{A caption}
    \csvreader[
    tabular=lr,
    table head={\toprule
    My objects & My number\\
    \midrule},
    table foot=\bottomrule,
    no head,
    ]{myfile.csv}{}{\csvcoli & \csvcolii}
\end{table}
\end{document}

enter image description here

  • Could you also add the steps to compile this? I was trying TexLiveUtility but was unable to understand how to generate the desired output from it. – Amanda Apr 24 at 13:28
  • @Amanda TeXLive Utility is a tool for updating your distribution. Typically people compile using a dedicated IDE like TeXworks, TeXShop, or emacs, vim etc. but everything can also be done from the command line if that's needed. What are good learning resources for a LaTeX beginner? – Alan Munn Apr 24 at 13:30
  • @Amanda There is no particular way to compile it, just run pdflatex. The way to do it depends on which editor you're using. Follow Alan's suggestion. – CarLaTeX Apr 24 at 13:34
  • I tried this with a bigger csv and converted it to pdf using TeXworks. Complete data is not visible. Only one page has been generated. Is this something to do with the template? – Amanda Apr 24 at 13:50
  • @Amanda If the csv is very long, you should use longtable. See Phelype's answer. – CarLaTeX Apr 24 at 13:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.