Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
LaTeX tables in Google Drive

LaTeX tables in Google Drive

Date: 2013-08-20

Here are two nice ways of generating LaTeX code for tables in Google Drive. I haven't tested them very much, but they seem to do a great job:

  1. Using http://www.tablesgenerator.com/. You just copy-and-paste your table from GDrive to the grid shown in the website, you can easily change the style of the table.

  2. Using the following GDrive script created by Dave Rim.

function latexify() {  
  var range = SpreadsheetApp.getActiveRange();  
  var numRows = range.getNumRows();  
  var numCols = range.getNumColumns();  
  var values = range.getValues();  
  var cs = new Array(numCols);  
  var strRows = "";   
    
  for(var k = 0; k < numCols; k++){  
   cs[k] = 'c';  
  }  
  strRows = "\\begin{table}\n";  
  strRows += "\\centering\n";  
  strRows += "\\begin{tabular}";  
  strRows += "{|" + cs.join("|") + "|}\n"  
  strRows += "\\hline\n";  
    
  for (var i = 0; i <= numRows - 1; i++) {  
   var row = values[i];  
   for (var j = 0; j <= numCols - 1; j++){  
    var cell = row[j];  
    strRows = strRows + cell;  
    if(j < numCols-1)  
     strRows = strRows + " & ";  
   }  
   strRows += "\\\\ \n\\hline\n";  
   }  
  strRows += "\\hline";  
  strRows += "\\end{tabular}\n";  
  strRows += "\\label{table:table}\n";  
  strRows += "\\caption{\\small{}} \n";  
  strRows += "\\end{table}\n";  

	  Logger.log(strRows);  
 };

To use it:

  • Open your spreadsheet.
  • Go to "Tools" > "Script Editor".
  • Click on "Create a new project".
  • Paste the script on the editor that opened up. Replace the example code already there.
  • Go back to spreadsheet and select the cells you want to use. Go to the editor and run the script.
  • In the editor, go to "View" > "Logs..." and your LaTeX code will be there.

Enjoy!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment