1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
#!/usr/bin/ruby
# csv2latex Version 1.1 2010/01/31
#
# (c) 2005 Tom Counsell <tom@counsell.org>
# (c) 2009, 2010 Alan Munn <amunn@msu.edu>
# Licenced under the GPL
#
# Modified and renamed 2009-07-10 by Alan Munn <amunn@msu.edu>
# Modified to allow for three different tabular packages in LaTeX
# and the option of just pasting cells alone.
#
# Revision history:
# v 1.0 2009/09/13 Initial release
# v 1.1 2010/01/31 Fixed bug in Applescript and updated documentation
# Removed unneeded dependency in script
#
# Usage: csv2latex <tablestyle>
# where <tablestyle> = cells, simple, booktabs, longtable
#
# Converts spreadsheet tables into latex tables
#
# Requires Ruby 1.8, and Mac OS 10.4 although could be adapted for other platforms.
# Uses the MacOS command line utilities pbcopy and pbpaste. Under linux, these could be
# reimplemented with xsel and xclip
#
# To use, copy the spreadsheet table you want to convert to the clipboard
# Run this code.
# The latex code will be in the clipboard ready to paste
#
# To use this seamlessly with your editor, use the supplied Applescript
# which will automatically paste the result into your source:
# This assumes installation of the script in /usr/local/bin
# Change the Applescript accordingly to reflect some other location.
#
#--Applescript direct
# set mainList to {"cells","booktabs", "simple","longtable" }
# choose from list mainList with prompt "Choose a table format"
# if the result is not false then
# set tablestyle to result as text
# do shell script "/usr/local/bin/csv2latex" & " " & tablestyle
# tell application "System Events" to keystroke "v" using {command down}
# end if
#--End of Applescript
#
#
def escape_latex( string )
string.gsub( %r{([&$#%])} ) { '\\' + $1 }
end
text = `pbpaste`.gsub( /(\r?\n)|\r/, "\r" )
lines = text.split("\r")
lines = lines.map { |line| line.split("\t") }
columns = lines.map { |line| line.size }.max
header = lines.shift
if ARGV.empty?
hstyle = "simple"
else
hstyle = ARGV[0]
end
pbcopy = IO.popen('pbcopy','w+')
if hstyle == "longtable"
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
else
if hstyle != "cells"
pbcopy.puts "\\begin{tabular}{#{'l'*columns}}"
pbcopy.puts
end
end
if hstyle == "booktabs"
pbcopy.puts "\\toprule"
pbcopy.puts header.map{ |entry| escape_latex entry }.join(' & ')+' \\\\'
pbcopy.puts "\\midrule"
end
if hstyle == "simple" or hstyle == "cells"
pbcopy.puts header.map{ |entry| escape_latex entry }.join(' & ')+' \\\\'
end
lines.each do |row|
pbcopy.puts row.map{ |entry| escape_latex entry }.join(' & ')+' \\\\'
end
pbcopy.puts
if hstyle == "booktabs"
pbcopy.puts "\\bottomrule"
end
if hstyle == "longtable"
pbcopy.puts "\\end{longtable}"
else
if hstyle != "cells"
pbcopy.puts "\\end{tabular}"
end
end
pbcopy.close_write
puts pbcopy.gets || "Ok. In clipboard"
|