2

I want to create a table from a CSV file in LaTex. I am able to do it with numbers here but creating table for strings such as abbrevions does not work. What is the problem?

enter image description here

MVE

This example fires error "Package PGF Math Error: Could not parse input '...' as a floating point number, sorry."

\documentclass{standalone}
\usepackage{pgfplotstable,filecontents}
\pgfplotsset{compat=1.9}% supress warning

\begin{filecontents*}{test.csv}
Abbreviation, Description
ACG, Azeri Chirag Guneshli
bcm, Billion cubic meters
BTC, Baku Tbilisi Ceyhan
CIA, Central Intelligence Agency
Btu, British thermal unit
CAC, Central Asia Center
EU, European Union
LNG, Liquified Natural Gas
NATO, North Atlantic Treaty Organization
OMV, Österreichische Mineralölverwaltung
\end{filecontents*}

\begin{document}
\pgfplotstabletypeset[col sep=comma, columns={Abbreviation,Description}]{test.csv}
\end{document}
  • Why don't you use the csvsimple solution there rather than the pgf one which expects to be fed mathematical expressions? – cfr Nov 10 '14 at 2:24
  • @cfr I did not know that. pgf solution looked so elegant! – hhh Nov 10 '14 at 2:26
  • @cfr I tried csvsimple and getting other errors: tex.stackexchange.com/a/211395/2956 – hhh Nov 10 '14 at 2:31
3

Add the string type option:

enter image description here

Notes:

  • To control the alignment of each row you can specify the style for each row.

    columns/Abbreviation/.style={column type=l},
    columns/Description/.style={column type=l},
    

    Alternatively you can set the alignment for all columns via column type=l.

  • To add the horizontal lines, I used the booktabs package and the every head row and every last row keys.

Code:

\documentclass[border=5pt]{standalone}
\usepackage{booktabs}

\usepackage{pgfplotstable}
\pgfplotsset{compat=1.9}% supress warning


%\usepackage{filecontents}% <-- commented to prevent overwriting fuel
\begin{filecontents*}{test.csv}
Abbreviation, Description
ACG, Azeri Chirag Guneshli
bcm, Billion cubic meters
BTC, Baku Tbilisi Ceyhan
CIA, Central Intelligence Agency
Btu, British thermal unit
CAC, Central Asia Center
EU, European Union
LNG, Liquified Natural Gas
NATO, North Atlantic Treaty Organization
OMV, Österreichische Mineralölverwaltung
\end{filecontents*}

\begin{document}
\pgfplotstabletypeset[
    string type, 
    col sep=comma, 
    columns={Abbreviation,Description},
    columns/Abbreviation/.style={column type=l},
    columns/Description/.style={column type=l},
    every head row/.style={before row=\toprule,after row=\cmidrule(lr){1-1}\cmidrule(lr){2-2}},
    every last row/.style={after row=\bottomrule}
    ]{test.csv}
\end{document}
  • WOW! Thank you +1! How does the positioning (not to center but to right) and adding \hline work? – hhh Nov 10 '14 at 2:36
  • 1
    @hhh: See revised answer. – Peter Grill Nov 10 '14 at 4:28
  • I tried to create a large example of this here, can you say why it is not working? Perhaps I don't understand after row=\cmidrule(lr){1-1}\cmidrule(lr){2-2}? It would be super cool to generate tables fast from CSV files like above with strings, any easy way? – hhh Nov 10 '14 at 5:15
  • How to use another names for columns, different from the head of csv file? – Sigur Jul 7 at 2:04
  • @Sigur: I don't know off hand and suggest you post a new question specifically about that issue. – Peter Grill Jul 7 at 8:24
2

This is a lightly modified version of someonr's answer

\documentclass{article}
\usepackage{csvsimple}
\usepackage{booktabs}
\usepackage{filecontents}
\begin{filecontents*}{\jobname.csv}
Abbreviation, Description
ACG, Azeri Chirag Guneshli
bcm, Billion cubic meters
BTC, Baku Tbilisi Ceyhan
CIA, Central Intelligence Agency
Btu, British thermal unit
CAC, Central Asia Center
EU, European Union
LNG, Liquified Natural Gas
NATO, North Atlantic Treaty Organization
OMV, Österreichische Mineralölverwaltung
\end{filecontents*}

\begin{document}
    \begin{tabular}{ll}%
      \toprule
      \bfseries Abbreviation & \bfseries Description% specify table head
      \csvreader[%
        head to column names,
        after head=\\\midrule,
        late after line=\\,
        ]{\jobname.csv}{}% use head of csv as column names
      {\Abbreviation & \Description}% specify your coloumns here
      \bottomrule
    \end{tabular}
\end{document}
  • head to column names lets you say \Abbreviation and \Description rather than using the internal \csvcoli etc.
  • If you take the rules and new lines out of the main definition, you can tweak the formatting a little better.
  • after head sets the code for the end of the header line (\\\midrule)
  • late after line sets the code for the end of each row of the tabular (\\)
  • You can then use \toprule and \bottomrule at the head and foot of the tabular without ending up with either complaints about misplaced align characters or excessive vertical spacing between the last line of the tabular and the final rule.

tabular of abbreviations

  • I cannot understand where you specify Abbreviation and Description to be columns? What are \csvcoli and \csvcolii? +1 for beautiful format, I still find the pgf thing easier to understand even though it looks uglier... – hhh Nov 10 '14 at 3:42
  • Csvcoli possibly csv column one and ii for column 2 – Vaibhav Nov 10 '14 at 4:31
  • @hhh That is just how csvsimple refers to the columns, I think. The head to column names is, I think, telling it to match the column headings to the names of the columns. (I haven't read the documentation - I really did just modify the earlier answer.) – cfr Nov 10 '14 at 12:36
  • 1
    @hhh I've updated the answer with some bits of explanation and to use more expressive macro names. The documentation for csvsimple is pretty good and makes a lot more sense of the code ;). – cfr Nov 11 '14 at 0:48

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.