16

I know there are tools to export excel tables to latex, but what about the other way around? I get a lot of latex tables from functions in R (e.g.: texreg) that aren't perfectly customizable via the code, and need some editing. For example, I'll have variables like T:X and X:T as separate rows, based on how whatever model function interpreted an interaction expansion. I'd like to be able to input a tabular object and get .csv-like table, which I could then edit and re-export (using calc2latex in my case).

Anything like this exist?

2

4 Answers 4

13

Far from perfection, but for a document with a single table you can use detex and then replace & by , and delete empty lines. In Linux you can use some tools as grep and sed for this. The first line of the table is then the columns types, so you can delete manually or with some tool like tail. Example command line:

detex file.tex | grep -v '^$' | tail -n+2  | sed 's/&/,/g' > file.csv

Example input file.tex:

\documentclass{article}
\begin{document}
\begin{tabular}{llll}
    11 & 12 & 13 & 14\\
    21 & 22 & 23 & 24\\
    31 & 32 & 33 & 34\\
    41 & 42 & 43 & 44\\
\end{tabular}
\end{document}

Ouput (file.csv):

    11 , 12 , 13 , 14
    21 , 22 , 23 , 24
    31 , 32 , 33 , 34
    41 , 42 , 43 , 44
3

Here is another workaround, if you have Excel or a similar application, and you can manage to make a LaTeX file that contains only the table: convert the LaTeX file using pandoc to HTML, as follows:

pandoc -o output.html input.tex

Then, import the resulting HTML table into Excel.

1

If you are an Emacs user, you can save this function in your init file.

(defun latex-table-export-to-csv-file (&optional file)
               "Creates a file  <base file name>-<start position>-<end position>.csv from the LaTeX table elements.
                The point must be within any LaTeX table & separated.
                The filename argument is for a programmatically call.
                This function only needs the point (cursor) in a LaTeX table.
             It creates a csv file <base file name>-<start position>-<end position>.csv.
             There is no check for point not in a LaTeX table but no error."
               (interactive )
    
               (let* ((end (save-excursion (search-forward "\\end" nil t)(beginning-of-line)(point)))
                      (beg(save-excursion (search-backward "\\begin" nil t)
                                          (re-search-forward "^.*&.*" nil t)
                                          (beginning-of-line)(point)))
                      (tbl (buffer-substring beg end)))
    
                 (unless file (setq file (format "%s-%d-%d.csv" (file-name-sans-extension buffer-file-name) beg end)))
    
                 (with-temp-buffer
                   (insert tbl)
                   (org-table-convert-region (point-min) (point-max) "&\\|\\\\\\\\.*")
                   (org-table-export file "orgtbl-to-csv" ))))

It only needs the point (cursor) to be in a LaTeX table. It creates a csv file <base file name>-<start position>-<end position>.csv. there is no check for not in a LaTeX table but no error.

You can active this function with M-x latex-table-export-to-csv-file or you can define a key sequence to activate it. I chose C-c t but it's up to you to have an other choice. Save this code in your init file:

(bind-key (kbd "C-c t") #'latex-table-export-to-csv-file LaTeX-mode-map)
0

If you want to keep all the LaTeX in the table, you can get away with the following series of regular expressions. This example uses Vim's regexp, through the vims script.

cat table.tex | vims \
    '1,/begin{tabular}/d' \
    '/end{tabular}/,$d' \
    '%g/^\s*$/d' \
    '%s/^\s*//g' \
    '%s/^/"/g' \
    '%s/\s*&\s*/","/g' \
    '%s/\\\\/"/g' \
    > output.tex
  • '1,/begin{tabular}/d' Delete up to begin{tabular}
  • '/end{tabular}/,$d' Delete after to end{tabular}
  • '%g/^\s*$/d' Delete empty lines
  • '%s/^\s*//g' Delete starting indentation
  • '%s/^/"/g' Add " to the start
  • '%s/\s*&\s*/","/g' Replace & (capturing nearby white space) with ","
  • '%s/\\\\/"/g' Replace the newline char with "

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .