# Reformatting LaTeX code after excel2latex

I use excel2latex to generate tables from Excel. The issue is that excel2latex generates really horribly formatted code with extra spaces, misaligned, etc. Does anyone have another solution to creating tables from excel? Alternatively from R? It seems that typing the actual table out could be pretty cumbersome. Another solution might be to have a piece of software that would format the code that excel2latex generates... does something like this exist? I use notepad++ and TeXWorks to edit the code.

EDIT This is the error I get from running that script:

C:\Users\alchernyakov\Dropbox\beta_var\paper_sys_idio_vol\table_tex_files>perl indent.plx test.tex > formatted_test.tex
Bareword found where operator expected at indent.plx line 9, near "<title>latexindent"
(Missing operator before latexindent?) syntax error at indent.plx line 5, near "html>"
Unrecognized character \xC2; marked by <-- HERE after at master <-- HERE near column 53 at indent.plx line 9.

-
try @cmhughes's indent.plx link: tex.blogoverflow.com/2012/08/… –  Harish Kumar Nov 6 '12 at 1:50
related latex-code-sniffer-and-or-beautifier (thanks for the plug @HarishKumar :) ) –  cmhughes Nov 6 '12 at 4:19
–  krlmlr Nov 6 '12 at 8:40
@Alex: doesn't work is not good enough to get help? Please give some more details. It works for me. Please see my long comment as answer. Pl. try that way. I also used active perl. Please provide one of your tables so that we can try on that also. –  Harish Kumar Nov 6 '12 at 22:31
@Alex: You must not download directly github.com/cmhughes/latexindent.plx ! Go on it, then click on “indent.plx” and then download by clicking on the “Raw” button! Your error message comes from the HTML file you actually downloaded. –  Speravir Nov 7 '12 at 22:30

This is not an exact answer but a reply to one of the comments above. (Can't have formatted code in comment)

the file suggested doesn't work on windows with ActivePerl... i end up writing my papers on a windows machine.. any ideas what the problem is?

First, we have to install perl -- either strawberry or active and the perl should be included in system path (important). For some more details pl refer to this question and answer

Then if your .tex file is in a folder c:\myfolder\myfile.tex (say), open command prompt and navigate to this folder typing

cd\
cd myfolder


If you are on a different drive, other than C: as suggested by Speravir you can also type (instead of above lines)

cd /d c:\myfolder


to reach myfolder.

Ensure that the file indent.plx is also in the same folder (or include its path to the system variable). Now type

perl indent.plx myfile.tex > myfile-formatted.tex


and press Enter.

This will generate a new file called myfile-formatted.tex (which is formatted) in the same folder. A little example:

Before formatting:

\documentclass{article}

\begin{document}
% Table generated by Excel2LaTeX from sheet 'Sheet1'
\begin{tabular}{rrr}

1 &    1000000 &       This \\

2 &        200 &       That \\

3 &     300000 &       Here \\

4 &     500000 &    My name \\

5 &      20000 & Is it so? Then OK \\

6 &         10 & This works. \\

\end{tabular}

% Table generated by Excel2LaTeX from sheet 'Sheet1' and misaligned by me
\begin{tabular}{rrr}

1      &    1000000             &       This \\

2  &        200    &       That \\

3 &300000 & Here \\

4              &     500000                                 &    My name \\

5 &      20000             & Is it so? Then OK \\

6 &         10     & This works. \\

\end{tabular}

\end{document}


After formatting:

\documentclass{article}

\begin{document}
% Table generated by Excel2LaTeX from sheet 'Sheet1'
\begin{tabular}{rrr}

1 & 1000000 & This              \\

2 & 200     & That              \\

3 & 300000  & Here              \\

4 & 500000  & My name           \\

5 & 20000   & Is it so? Then OK \\

6 & 10      & This works.       \\

\end{tabular}

% Table generated by Excel2LaTeX from sheet 'Sheet1' and misaligned by me
\begin{tabular}{rrr}

1 & 1000000 & This              \\

2 & 200     & That              \\

3 & 300000  & Here              \\

4 & 500000  & My name           \\

5 & 20000   & Is it so? Then OK \\

6 & 10      & This works.       \\

\end{tabular}

\end{document}


Note:

As noted by @speravir:

Harish Making one line from two: Replace cd\ <linebreak>cd c:\myfolder with cd /d c:\myfolder (in one line, as I said). “My” version works also, if one is one a different drive first, “your” version not. –  Speravir Nov 6 '12 at 22:39
@Speravir: Thanks, added. :-) –  Harish Kumar Nov 6 '12 at 22:43
@Speravir: You are right. In fact I hate the command prompt. Don't tell anybody ;-) –  Harish Kumar Nov 6 '12 at 23:17