My question is that different cols have different widths. each row has multicolumns whose widths change. How do i have a macro which allow me to input such table from Excel to Latex. Thanks in advance.
[Tex/LaTex] How to convert a table with multicolumns from excel to latex
excelexcel2latextables
Best Answer
I will summarize the simple solutions in this answer and write a Matlab-integrated in the sequence.
This method you just built the LaTeX by typing the pieces you need inside Excel. That's was the first idea I ever had. It will be useful only if you can copy and paste the data at the exactly same template. But you will always have to copy and paste ... and it is incredible slow.
Consider the following spreadsheet
Starting to write the commands (I made some mistakes when creating it). It is easier to define the commands on separated cells.
The auto complete tool helps but not too much if you have many special cases.
Every merge must be manually correct and it is not simple to edit it correctly.
Then I noticed a error.
\hline
should be\\ \hline
and then add a\hline
only above the first row.The result must be copied into your LaTeX editor.
It is faster, just drag your
.xls
or.xlsx
file into it. But you might have to edit extra rows and lines are always inserted.The best option for few tables, it has the tools to edit the table and generate LaTeX code. It has the slow down of converting your file into
.csv
, but the quick tools to format really payoff.The syntax is presented and can be edited.
And adding borders is as easy as it is in any WYSIWYG software
Another important backslash is that Matlab is a commercial software and I cannot say my codes work properly on Octave. Some of them use syntax I have never tried in Octave.
The solution is still not perfect, I usually don't have special cases to merge using
\multicolumn{cols}{pos}{text}
or worse\multirow{number of rows}{width}{text}
. Although, I have coded a solution to the same problem in myMatlab to Word Lbrary
, I still haven't made the same at theMatlab to LaTeX library
.My Matlab-integrated solution is presented in another dedicated answer.
Edit: I decided to integrate in only one answer.
So here we are with our Matlab-integrated solution.
To perform to full task, we gonna need three of my libraries
To manage latex syntax and create it from matlab objects.
To load excel file and to create the
.tex
file directly from Matlab.This library is the master Gambiarra (workaround in Brazilian Portuguese) of all Gambiarras.
It let's you replace a given set of words for another set in as many files you want. It almost the same as
NotePad++
does with itsFind Files
command. But here you can do it programmatically and even taking advantage of the sequence of operations.The
LaTeX
MWE is indeed very simpleThe key concept is to always create external files, so that you only need to input it into the main file. It let's you modify external files as many times as you want.
input_tab_test
should look like this when we finishThe
MATLAB
code follows among the explanations.Load Excel into Matlab,
'F5:K11'
is important to avoid extra cells. The second entry,1
, is the number of the sheet.The following loop converts all entries of the cell
raw
into strings. I will probably promote it to a function soon.Then we define the format and call the first library function.
For repeated letters use:
tb_l is the very same result from the online gadgets.
Now, a function with some syntax is called to add
\begin{tabular}
,\end{tabular}
and whatever is necessary. The Matlab commandchar
is the key here.tab
should look like thisAnd finally we can save it in the disk with a function from the second library.
As I don't have a merge columns function right now, I will use the third library. This step makes the whole process less efficient, as the user still have to manually define the substitutions. You can be a little bit flexible as the example with
w1
.To merge, I will create a cell with the row number and a vector with the first and last cell of each group to merge.
But I still need to code it for the LaTeX syntax.
Finally, we call the function to search every
input
and replace the respectiveoutput
.The variable
filter
filters only files of interesting.This method works better when you don't have special cases, in the future this should not be a issue anymore, and the greatest advantage is the incredible speed to correct mistakes of to reuse with others tables with the same template. And as it saves a external file, all you need to do is to recompile the main
.tex
file.This is the result in LaTeX
Edit 2: New function to automatic merge columns.
Consider the following spreadsheet
Loading it into Matlab . The result of the command
tb_l = fc_lib_latex_cell_to_tab_line(new_raw,'\hline');
isThe new function to merge is
new_tb_l = fc_lib_latex_tab_line_colMerge(tb_l,colMerge);
wherethe first entry is the line to merge, the second has a matrix with first and last columns of each gap to merge and the third entry is the alignment. The LaTeX syntax is
and the final result is
Don't forget `\usepackage{inputenc}' to deal with accented characters.