3

Is it possible to change default column separator character (&) for tabular environment ? I find it convenient to edit content of table in LibreOffice Calc and save it as csv file. This csv file I include in tex code like this:

\begin{table}[H]
    \begin{tabulary}{\textwidth}{*{6}{L}}
        \toprule
        \input{data.csv}
    \end{tabulary} \hfill \
\end{table}

It is possible to change column separator when first saving csv file so this is not problem. But when opening already created csv file, editing it and saving, LibreOffice without asking uses default column separator which is semicolon (;) and rewrites previous separator (&). So for me it would be better to switch latex to using semicolon as column separator character instead of ampersand.

  • You can use pgfplotstable which comes with pgfplots. – Ignasi Apr 12 '15 at 18:09
  • \catcode`\;=4 – David Carlisle Apr 12 '15 at 18:42
  • what is \hfill \ doing here? – David Carlisle Apr 12 '15 at 18:44
  • @David: "\hfil \" align table to the left instead of centering it. – truthseeker Apr 13 '15 at 6:12
  • Left align is the default, and the table is full width so ..} \hfill\ just makes a spurious white line under the table looking like \vspace{\baselineskip} – David Carlisle Apr 13 '15 at 7:15
3

enter image description here

\begin{filecontents}{data.csv}
11.0; 12.3; one; two
1.2; 33.11; five; six
\end{filecontents}
\documentclass{article}
\usepackage{booktabs}
\begin{document}

\begin{table}
\catcode`\;=4
\catcode`\^^M=13 %
\def^^M{\\}%
    \begin{tabular}{*{4}{l}}%
        \toprule%
        \input{data.csv}%
    \end{tabular}%
\end{table}%

\end{document}
  • Thanks for your solution which is simple and clean, but unfortunately in real-world document it doesnt function. It generates tons of errors like for example "! Missing number, treated as zero." or "! Missing $ inserted." on line with \catcode\^^M=13. Its hard to extract essence of problem into small MWE because there are interdependencies between various commands which I am unable to comprehend without some debugging tools. – truthseeker Apr 13 '15 at 7:45
  • @truthseeker did you forget the ` before \^^M ? that would make a missing $ error. – David Carlisle Apr 13 '15 at 8:17
  • No, I copy-pasted it. – truthseeker Apr 13 '15 at 8:20
  • @truthseeker well something else is wrong then:-) – David Carlisle Apr 13 '15 at 8:21
2

A solution with cvssimple:

\begin{filecontents*}{\jobname.csv}
11.0; 12.3; one; two
1.2; 33.11; five; six
\end{filecontents*}

\documentclass{article}
\usepackage{booktabs,tabulary}
\usepackage{csvsimple}

\begin{document}
\begin{tabulary}{\textwidth}{*{4}{L}}
\toprule
1 & 2 & 3 & 4 \\
\midrule
\csvreader[
  separator=semicolon,
  no head,
  late after line=\\,
]{\jobname.csv}{}{%
  \csvcoli & \csvcolii & \csvcoliii & \csvcoliv
}
\bottomrule
\end{tabulary}

\bigskip

\end{document}

enter image description here

1

Here is my alternative using datatool.

\begin{filecontents*}{data.csv}
11.0; 12.3; one; two
1.2; 33.11; five; six
\end{filecontents*}
\documentclass{article}
\usepackage{booktabs}
\usepackage{datatool}
\begin{document}
\DTLsetseparator{;}
\DTLloadrawdb[noheader,keys={ca,cb,cc,cd}]{ctext}{data.csv}
\begin{table}
    \begin{tabular}{*{4}{l}}%
        \toprule%
    \DTLforeach{ctext}{\ca=ca, \cb=cb, \cc=cc,\cd=cd}%
        {%
        \DTLiffirstrow{}%
            {%
            \\%
            }%
            \ca&%
            \cb&%
            \cc&%
            \cd%
        }%
    \end{tabular}%
\end{table}%
\end{document}

Its more generic because you can fully seperate design and content.

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.