1 ## Copyright (C) 2006 Hansgeorg Schwibbe
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
16 ## Creates a latex file from a csv file. The generated latex file contains a
17 ## tabular with all values of the csv file. The tabular can be decorated with
18 ## row and column titles. The generated latex file can be inserted in any latex
19 ## document by using the '\input{latex file name without .tex}' statement.
22 ## - csv2latex(csv_file, csv_sep, latex_file)
23 ## - csv2latex(csv_file, csv_sep, latex_file, tabular_alignments)
24 ## - csv2latex(csv_file, csv_sep, latex_file, tabular_alignments, has_hline)
25 ## - csv2latex(csv_file, csv_sep, latex_file,
26 ## tabular_alignments, has_hline, column_titles)
27 ## - csv2latex(csv_file, csv_sep, latex_file, tabular_alignments,
28 ## has_hline, column_titles, row_titles)
31 ## csv_file - the path to an existing csv file
32 ## csv_sep - the seperator of the csv values
33 ## latex_file - the path of the latex file to create
34 ## tabular_alignments - the tabular alignment preamble (default = {'l','l',...})
35 ## has_hline - indicates horizontal line seperator (default = false)
36 ## column_titles - array with the column titles of the tabular (default = {})
37 ## row_titles - array with the row titles of the tabular (default = {})
40 ## # creates the latex file 'example.tex' from the csv file 'example.csv'
41 ## csv2latex("example.csv", '\t', "example.tex");
43 ## # creates the latex file with horizontal and vertical lines
44 ## csv2latex('example.csv', '\t', 'example.tex', {'|l|', 'l|'}, true);
46 ## # creates the latex file with row and column titles
47 ## csv2latex('example.csv', '\t', 'example.tex', {'|l|', 'l|'}, true,
48 ## {'Column 1', 'Column 2', 'Column 3'}, {'Row 1', 'Row 2'});
50 function csv2latex (csv_file, csv_sep, latex_file, tabular_alignments, has_hline, column_titles, row_titles)
52 ## set up the default values
63 tabular_alignments = {};
66 ## load the csv file and create the csv cell
67 [fid, msg] = fopen (csv_file, 'r'); # open the csv file to read
70 [val, count] = fread(fid); # read all data from the file
71 fclose(fid); # close the csv file after reading
76 if val(index) == csv_sep
77 csv(line_index, value_index) = csv_value;
80 elseif (val(index) == '\n' || (val(index) == '\r' && val(index+1) == '\r'))
81 csv(line_index, value_index) = csv_value;
87 csv_value = sprintf('%s%c', csv_value, val(index));
92 ## get the size and length values
93 [row_size, col_size] = size(csv);
94 alignment_size = length(tabular_alignments);
95 column_title_size = length(column_titles);
96 row_title_size = length(row_titles);
98 ## create the alignment preamble and the column titles
99 alignment_preamble = '';
100 tabular_headline = '';
101 if row_title_size != 0
102 current_size = col_size + 1;
104 current_size = col_size;
106 for col_index = 1:current_size
107 if col_index <= alignment_size
108 alignment_preamble = sprintf ('%s%s', alignment_preamble, tabular_alignments(col_index));
110 alignment_preamble = sprintf ('%sl', alignment_preamble);
112 if column_title_size != 0
113 if col_index <= column_title_size
115 tabular_headline = sprintf ('%s', column_titles(col_index));
117 tabular_headline = sprintf ('%s & %s', tabular_headline, column_titles(col_index));
120 tabular_headline = sprintf ('%s &', tabular_headline);
126 [fid, msg] = fopen (latex_file, 'w'); # open the latex file for writing
128 fprintf (fid, '\\begin{tabular}{%s}\n', alignment_preamble); # print the begin of the tabular
129 if column_title_size != 0
131 fprintf (fid, ' \\hline\n');
133 fprintf (fid, ' %s \\\\\n', tabular_headline); # print the headline of the tabular
135 for row_index = 1:row_size
137 fprintf (fid, ' \\hline\n');
139 for col_index = 1:col_size
141 if row_title_size != 0
142 if row_index <= row_title_size
143 fprintf (fid, ' %s & ', row_titles(row_index)); # print the row title
145 fprintf (fid, ' & '); # print an empty row title
148 fprintf (fid, ' %s ', csv{row_index, col_index});
150 fprintf (fid, '& %s ', csv{row_index, col_index});
153 fprintf (fid, '\\\\\n');
156 fprintf (fid, ' \\hline\n');
158 fprintf (fid, '\\end{tabular}', alignment_preamble); # print the end of the tabular
159 fclose(fid); # close the latex file after writing