Giter VIP home page Giter VIP logo

csv-to-tex-table's Introduction

csv-to-tex-table

Instruction for converting csv-files, produced here by NumPy, to a format required in TeX-tables.

What to do in Python?

We will start at the point, where all data is calculated.

The line in python should be according to

numpy.savetxt('output.csv',
    numpy.column_stack([data1, data2, ...]),
    fmt='%.2f, %.0f, ...',
    header='data1, data2, ...')

The option fmt let you decide how many decimal places should be written. In this example 2 for data1 and 0 for data2.

IMPORTANT:

  • Watch that you set a format for each column!
  • Use the , between each of the numbers (as shown)

What next?

If you try to change the format simply from .csv to .tex it would not work. I tried to get things done with the end-of-line character option in numpy-savetxt but it did not satisfy me.

Instead we can use the two commands

sed '/^#/ d' < output.csv > output-new.csv

cat output-new.csv | sed \
    -e 's:,:\t\&\t:g' \
    -e 's:$:\t\\\\:g' > output.tex

With those command we can prepare the output.csv-file in a way, that we only need to import it into LaTeX.

The first sed command removes the header-line and writes it into a new file. (For some reason it does not work for me with the same file; if you do not use a header, you can remove the line)

Quick rundown of the arguments from second sed:

  • Replacing the , as a delimiter with the & used by TeX (one could use the & already in the fmt-option)
  • Replacing the end of line, withe the double backslash \\

The next part is bigger and needs to be adapted for each table, but i did not fin a way without it.

ed output.tex << END
1i
\begin{tabular}{c c ...}
\toprule
{Data 1} & {Data 2} & ... \\\\
\midrule
.
\$a
\bottomrule
\end{tabular}
.
wq
END

These lines put the previous prepared data into the tabular structure. For each table, change

  • the number of columns and the way to display them (c or S)
  • the title of each column

What to do in TeX?

\begin{table}
  \input{output.tex}
\end{table}

Of course you should add things as centering, caption and label as needed.

You may also take a look into the example.

csv-to-tex-table's People

Contributors

chrbeckm avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.