Excel to Latex table convertor

Excel to Latex table convertor

Revisions made by tamc up to 15:06 Wed 27 Jul 2005

Note, if you wish to edit or undo these revisions, please follow the links from here. You cannot do it from here, becuase someone may have edited the page more recently and because you may need a password to edit the page.

15:06 Wed 27 Jul 2005

  1. 45. pbcopy.puts "\\multicolumn{#{columns}}{r}{{Continued\ldots}} \\"
  2. 45. pbcopy.puts "\\multicolumn{#{columns}}{r}{{Continued\\ldots}} \\"

07:47 Tue 26 Jul 2005

  1. 37. pbpaste = IO.popen('pbcopy','w+')
  2. 37. pbcopy = IO.popen('pbcopy','w+')
  3. 39. pbpaste.puts "\\begin{center}"
  4. 40. pbpaste.puts "\\begin{longtable}{#{'l '*columns}}"
  5. 41. pbpaste.puts header.map{ |entry| escape_latex entry }.join(' & ')+' \\\\'
  6. 42. pbpaste.puts "\\endfirsthead"
  7. 43. pbpaste.puts header.map{ |entry| escape_latex entry }.join(' & ')+' \\\\'
  8. 44. pbpaste.puts "\\endhead"
  9. 45. pbpaste.puts "\\multicolumn{#{columns}}{r}{{Continued\ldots}} \\"
  10. 46. pbpaste.puts "\\endfoot"
  11. 47. pbpaste.puts "\\hline"
  12. 48. pbpaste.puts "\\endlastfoot"
  13. 49. pbpaste.puts
  14. 39. pbcopy.puts "\\begin{center}"
  15. 40. pbcopy.puts "\\begin{longtable}{#{'l '*columns}}"
  16. 41. pbcopy.puts header.map{ |entry| escape_latex entry }.join(' & ')+' \\\\'
  17. 42. pbcopy.puts "\\endfirsthead"
  18. 43. pbcopy.puts header.map{ |entry| escape_latex entry }.join(' & ')+' \\\\'
  19. 44. pbcopy.puts "\\endhead"
  20. 45. pbcopy.puts "\\multicolumn{#{columns}}{r}{{Continued\ldots}} \\"
  21. 46. pbcopy.puts "\\endfoot"
  22. 47. pbcopy.puts "\\hline"
  23. 48. pbcopy.puts "\\endlastfoot"
  24. 49. pbcopy.puts
  25. 51. pbpaste.puts row.map{ |entry| escape_latex entry }.join(' & ')+' \\\\'
  26. 51. pbcopy.puts row.map{ |entry| escape_latex entry }.join(' & ')+' \\\\'
  27. 53. pbpaste.puts
  28. 54. pbpaste.puts "\\end{longtable}"
  29. 55. pbpaste.puts "\\end{center}"
  30. 56. pbpaste.close_write
  31. 57. puts pbpaste.gets || "Ok. In clipboard"
  32. 53. pbcopy.puts
  33. 54. pbcopy.puts "\\end{longtable}"
  34. 55. pbcopy.puts "\\end{center}"
  35. 56. pbcopy.close_write
  36. 57. puts pbcopy.gets || "Ok. In clipboard"

07:42 Tue 26 Jul 2005

  1. 0. h1. Excel to Latex table convertor
  2. 1.
  3. 2. This is a quick script I hacked up to help doing tables in latex. Written for OSX 10.4 and your latex must use the longtable package.
  4. 3.
  5. 4. To use:
  6. 5. # Copy the excel table you want to convert
  7. 6. # Run this code (excel2latex.rb), it will place the latex in the clipboard
  8. 7. # Paste into your latex document
  9. 8.
  10. 9. <pre>
  11. 10. <code>
  12. 11. #!/usr/bin/ruby
  13. 12. # (c) 2005 Tom Counsell tom@counsell.org
  14. 13. # Licenced under the GPL
  15. 14. #
  16. 15. # Converts excel tables into latex tables
  17. 16. #
  18. 17. # Requires Mac OSX 10.4, although could be adapted for other platoforms
  19. 18. #
  20. 19. # To use, copy the excel table you want to convert to the clipboard
  21. 20. # Run this code
  22. 21. # The latex code will be in the clipboard ready to paste
  23. 22. #
  24. 23. # Let me know of any bugfixes or suggestions
  25. 24.
  26. 25. require 'csv'
  27. 26.
  28. 27. def escape_latex( string )
  29. 28. string.gsub( %r{([&$#%])} ) { '\\' + $1 }
  30. 29. end
  31. 30.
  32. 31. text = `pbpaste`
  33. 32. lines = text.split("\r")
  34. 33. lines = lines.map { |line| line.split("\t") }
  35. 34. columns = lines.map { |line| line.size }.max
  36. 35. header = lines.shift
  37. 36.
  38. 37. pbpaste = IO.popen('pbcopy','w+')
  39. 38.
  40. 39. pbpaste.puts "\\begin{center}"
  41. 40. pbpaste.puts "\\begin{longtable}{#{'l '*columns}}"
  42. 41. pbpaste.puts header.map{ |entry| escape_latex entry }.join(' & ')+' \\\\'
  43. 42. pbpaste.puts "\\endfirsthead"
  44. 43. pbpaste.puts header.map{ |entry| escape_latex entry }.join(' & ')+' \\\\'
  45. 44. pbpaste.puts "\\endhead"
  46. 45. pbpaste.puts "\\multicolumn{#{columns}}{r}{{Continued\ldots}} \\"
  47. 46. pbpaste.puts "\\endfoot"
  48. 47. pbpaste.puts "\\hline"
  49. 48. pbpaste.puts "\\endlastfoot"
  50. 49. pbpaste.puts
  51. 50. lines.each do |row|
  52. 51. pbpaste.puts row.map{ |entry| escape_latex entry }.join(' & ')+' \\\\'
  53. 52. end
  54. 53. pbpaste.puts
  55. 54. pbpaste.puts "\\end{longtable}"
  56. 55. pbpaste.puts "\\end{center}"
  57. 56. pbpaste.close_write
  58. 57. puts pbpaste.gets || "Ok. In clipboard"
  59. 58. </code>
  60. 59. </pre>
View, Edit or see all changes to this page.