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