[OS X TeX] Re: Tables

Hamner, Jesse Harrison jhamner at emory.edu
Tue Aug 21 10:37:10 EDT 2007


All,

On 8/21/07 10:24 AM, "Nathan Paxton" <napaxton at fas.harvard.edu> wrote:

>         Where would I save this, and how would I add it so the command line
> "sees" it?

I just tried this. There are a few line breaks that need to be
eliminated (80-column wrap issues in the email) but I am very impressed at
how well it works.

Put the script in some directory, say, /Users/nathan.

Copy some cells from an excel table.

open Terminal

type

ruby scriptname.rb (in this case, I called it xl2latex.rb as suggested)

And it should answer "Ok. In clipboard"

then paste the contents of the clipboard into your LaTeX document.

Jesse

>> 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"
>>
>> *************************************



--
Jesse Hamner
Coordinator, Social Sciences Computing Center
Emory University




------------------------- 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