Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A Ruby TextExpander snippet that takes data copied to the clipboard from Numbers and pastes LaTeX source for a table environment. Discussed here:
class Line
@@max_length = nil
@@eol = ' \\\\'
def initialize( data )
@contents = data
@@max_length = [0]*@contents.size if @@max_length.nil? # initialize array if this is the first Line instance
@contents.each_with_index{ |element, i| @@max_length[i] = [element.length, @@max_length[i]].max } # update column widths
def to_tex @@max_length ).collect { |entry, length| entry.ljust(length).sub(/\|/,' ').gsub('&','\\\&') }.join(' & ') + @@eol
def define_format
@contents.inject('|'){ |string, entry| /\|/.match(entry) ? string + 'c|' : string + 'c' } + '|'
table_header = <<END
\\begin{tabular}{FORMAT} \\hline
table_footer = <<END
contents = "%clipboard".split( /[\n\r]/ ).collect{ |line| line.split( /\t/ ) )}
puts table_header.sub( 'FORMAT', contents[0].define_format )
puts contents.collect{ |line| line.to_tex }.join("\n")
puts table_footer

This comment has been minimized.

Copy link

commented Mar 7, 2013

I'm having issues getting this script going. I think I have it setup correctly in TextExpander: .

I have these numbers in Numbers: . When I copy them to the clipboard and use ':testable' as my snippet call, nothing is output. Any help would be greatly appreciated. Thanks.


This comment has been minimized.

Copy link
Owner Author

commented Jun 21, 2013

@mreiach: Looks okay to me. What do you get as the output if you just run the snippet as a normal ruby executable? I get

\begin{tabular}{|c|} \aline
%clipboard \\
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.