# [OS X TeX] Re: Tables

David R. Derbes loki at uchicago.edu
Mon Aug 20 23:00:46 EDT 2007

This question came up about a year ago, and some kind soul pointed to a
Ruby program (script? I don't know Ruby) that did the job almost
magically.

It is short enough to put into this message, and I will. Evidently Ruby is
built into the OS X environment. You construct your table in Excel, copy
to the clipboard (Command-C) that part you want imported into Excel, open
up a Terminal window, and type at the command prompt

ruby xl2latex

and then paste (Command-V) into the LaTeX document of your choice. And
that should do it. I had nothing to do with this program but it has
worked brilliantly for me, and my thanks to Tom Counsell for providing
it. (I may have the command wrong; I haven't used this in a while. But the
program is fine.)

David Derbes
U of Chicago Lab Schools

Here is the program (between ******; don't include the asterisks!)):

(Save as xl2latex.rb)
****************************

#!/usr/bin/ruby
# (c) 2005 Tom Counsell tom at counsell.org
# Licenced under the GPL
#
# Converts excel tables into latex tables
#
# Requires Mac OSX 10.4, although could be adapted for other platoforms
#
# To use, copy the excel table you want to convert to the clipboard
# Run this code
# The latex code will be in the clipboard ready to paste
#
# Let me know of any bugfixes or suggestions

require 'csv'

def escape_latex( string )
string.gsub( %r{([&$#%])} ) { '\\' +$1 }
end

text = pbpaste
lines = text.split("\r")
lines = lines.map { |line| line.split("\t") }
columns = lines.map { |line| line.size }.max
header = lines.shift

pbcopy = IO.popen('pbcopy','w+')

pbcopy.puts "\\begin{center}"
pbcopy.puts "\\begin{longtable}{#{'l '*columns}}"
pbcopy.puts header.map{ |entry| escape_latex entry }.join(' & ')+' \\\\'
pbcopy.puts "\\endfirsthead"
pbcopy.puts header.map{ |entry| escape_latex entry }.join(' & ')+' \\\\'
pbcopy.puts "\\endhead"
pbcopy.puts "\\multicolumn{#{columns}}{r}{{Continued\\ldots}} \\"
pbcopy.puts "\\endfoot"
pbcopy.puts "\\hline"
pbcopy.puts "\\endlastfoot"
pbcopy.puts
lines.each do |row|
pbcopy.puts row.map{ |entry| escape_latex entry }.join(' & ')+' \\\\'
end
pbcopy.puts
pbcopy.puts "\\end{longtable}"
pbcopy.puts "\\end{center}"
pbcopy.close_write
puts pbcopy.gets || "Ok. In clipboard"

*************************************

On Tue, 21 Aug 2007, John Maindonald wrote:

> Users of the R system have the xtable function in the xtable
> package, which works like a charm.  Supply the table, and
> it spits out the LaTeX code.
>
> There is a simple editor that gives a somewhat spreadsheet-
> like ability to edit R objects, including tables.  There is also
> an interface to Excel if you want to go that route.
>
> Note also the abilities in the Hmisc package.
>
> For R, go to http://cran.r-project.org
>
> John Maindonald             email: john.maindonald at anu.edu.au
> phone : +61 2 (6125)3473    fax  : +61 2(6125)5549
> Centre for Mathematics & Its Applications, Room 1194,
> John Dedman Mathematical Sciences Building (Building 27)
> Australian National University, Canberra ACT 0200.
>
>
> On 21 Aug 2007, at 10:00 AM, TeX on Mac OS X Mailing List wrote:
>
>> Subject: Tables
>> From: "Nathan Paxton" <napaxton at fas.harvard.edu>
>> Date: Mon, 20 Aug 2007 14:26:08 -0400
>>
>> 	Hi all,
>>
>> 	I'm about to make some statistical tables to include in a conference
>> article, and I wonder if there is a convenient way to do this.  I
>> know about the Tables panel in TeXShop, but I've never quite figured
>> out how to get it to the following.
>>
>> 	Say I set up a table (headers and such) but don't put the values for
>> each cell in. Then I want to fill in those values over the course of
>> a couple of days. And perhaps I need a column added later on. Etc. I
>> don't see any way to save the contents of the table so that I can re-
>> edit it with ease (and also not screw it up if I edit it by hand,
>> which inevitably seems to happen). I guess what I'd like is something
>> more spreadsheet-like, where I can enter, edit, and format the table,
>> but I can then generate at least some sort of LaTeX code to insert
>> into my document.
>>
>> 	Does anyone have a suggestion for how to go about this?
>>
>> -N
>> ----------
>> Nathan A. Paxton
>> Ph.D. Candidate
>> Dept. of Government, Harvard University
>>
>> Resident Tutor
>> John Winthrop House, Harvard University
>>
>> napaxton AT fas DOT harvard DOT edu
>> http://www.fas.harvard.edu/~napaxton
>
>
> ------------------------- Helpful Info -------------------------
> Mac-TeX Website: http://www.esm.psu.edu/mac-tex/
> TeX FAQ: http://www.tex.ac.uk/faq
> List Archive: http://tug.org/pipermail/macostex-archives/
> List Reminders & Etiquette: http://www.esm.psu.edu/mac-tex/list/
>
>
>

------------------------- Helpful Info -------------------------
Mac-TeX Website: http://www.esm.psu.edu/mac-tex/
TeX FAQ: http://www.tex.ac.uk/faq
List Archive: http://tug.org/pipermail/macostex-archives/
List Reminders & Etiquette: http://www.esm.psu.edu/mac-tex/list/



More information about the MacOSX-TeX mailing list