001package org.cpsolver.ifs.util; 002 003import java.io.File; 004import java.io.PrintWriter; 005 006import org.cpsolver.ifs.util.CSVFile.CSVLine; 007 008 009/** 010 * A simple class converting CSV files to LaTeX tables. 011 * 012 * @version IFS 1.3 (Iterative Forward Search)<br> 013 * Copyright (C) 2006 - 2014 Tomáš Müller<br> 014 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br> 015 * <a href="http://muller.unitime.org">http://muller.unitime.org</a><br> 016 * <br> 017 * This library is free software; you can redistribute it and/or modify 018 * it under the terms of the GNU Lesser General Public License as 019 * published by the Free Software Foundation; either version 3 of the 020 * License, or (at your option) any later version. <br> 021 * <br> 022 * This library is distributed in the hope that it will be useful, but 023 * WITHOUT ANY WARRANTY; without even the implied warranty of 024 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 025 * Lesser General Public License for more details. <br> 026 * <br> 027 * You should have received a copy of the GNU Lesser General Public 028 * License along with this library; if not see 029 * <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>. 030 */ 031 032public class Csv2Tex { 033 034 public static void main(String args[]) { 035 try { 036 for (int i = 0; i < args.length; i++) { 037 File file = new File(args[i]); 038 039 String name = file.getName(); 040 if (name.contains(".")) name = name.substring(0, name.indexOf('.')); 041 042 CSVFile csv = new CSVFile(file); 043 044 PrintWriter pw = new PrintWriter(new File(file.getParentFile(), name + ".txt")); 045 046 pw.println("\\begin{table}[htb]"); 047 pw.println("\\renewcommand{\\arraystretch}{1.2}"); 048 String t = "l|"; for (int j = 1; j < csv.getHeader().size(); j++) t += "c"; 049 pw.println("\\begin{tabular}{" + t + "} \\hline \\hline"); 050 051 for (int j = 0; j < csv.getHeader().size(); j++) { 052 if (j > 0) pw.print(" \\ & \\ "); 053 pw.print(csv.getHeader().getField(j).toString()); 054 } 055 pw.println(" \\ \\\\ \\hline"); 056 057 for (CSVLine line: csv.getLines()) { 058 for (int j = 0; j < line.size(); j++) { 059 if (j == 0) 060 pw.print(line.getField(j)); 061 else 062 pw.print(" & " + (line.getField(j) == null || line.getField(j).isEmpty() ? "-" : "$" + line.getField(j).toString().replace("%", "\\%") + "$")); 063 } 064 pw.println(" \\\\"); 065 } 066 067 pw.println("\\hline \\hline"); 068 pw.println("\\end{tabular}"); 069 pw.println("\\caption{FIXME: Add caption here.}"); 070 pw.println("\\label{tab:" + name.toLowerCase() + "}"); 071 pw.println("\\end{table}"); 072 073 pw.flush(); pw.close(); 074 } 075 } catch (Exception e) { 076 e.printStackTrace(); 077 } 078 } 079 080}