[OS X TeX] How to copy and paste tables into LaTeX (from Excel or other programs)?

Bernhard Barkow bb at creativeeyes.at
Wed Dec 7 05:40:44 EST 2005


Hi,

On 07.12.2005, at 01:13, Fredrik Wallenberg wrote:
> I get an error from the applescript (expected ", " but found end of  
> line)
>
> On 12/6/05, Bernhard Barkow <bb at creativeeyes.at> wrote:

[...]

I'm sorry, this seems to be an error caused by the mail program  
breaking the overlong lines. Either remove the linebreaks in the  
macro editor (the ones that are not indented, except for those with  
"end ..."), or download the script source from

http://www.creativeeyes.at/tools/ltxtabimp
( the file itself is at http://www.creativeeyes.at/tools/sw/ 
ltxtabimport.zip )

It's just 2kB or so, but I cannot attach it when posting to the list.

Bernhard

>>
>> /* AppleScript: copy the code below and paste it into a new TeXShop
>> macro (Macros / Open Macro Editor… / New Item) */
>>
>> --AppleScript direct
>> -- converts tabular clipboard data to LaTeX
>> -- Bernhard Barkow 2005
>> property kColSep : " & " as string
>> property kSeparators : {tab, ";", space}
>> property kDlgTitle : "Tabular data import"
>> property kHline : "\\hline"
>>
>> tell application "TeXShop"
>>         -- prepare the clipboard data
>>         set cbd to the clipboard as string
>>         set cbl to paragraphs of cbd
>>         set tmpStr to the first item in cbl
>>         set sepChar to my getColSepChar(tmpStr)
>>
>>         -- create the  table header
>>         set outText to "\\begin{tabular}{"
>>         set nColumns to my countSubstr(tmpStr, sepChar)
>>         set colDefText to ""
>>         -- get user input
>>         set colJust to text returned of (display dialog "Default  
>> column
>> justification (l,c,r,c|,…):" default answer "c" with title kDlgTitle
>> buttons {"OK"} default button 1)
>>         if (colJust = "") then set colJust to "c"
>>         set doHline to (button returned of (display dialog "Add \ 
>> \hline?"
>> with title kDlgTitle buttons {"Yes", "No"} default button 2) = "Yes")
>>         set tabLineBreak to " \\\\"
>>         set daLineSpc to (display dialog "Extra line spacing:"  
>> default
>> answer "0pt" with title kDlgTitle buttons {"Ok", "None"} default
>> button 1)
>>         -- append vertical spacing if required:
>>         if (button returned of daLineSpc = "Ok") then set  
>> tabLineBreak to
>> tabLineBreak & "[" & (text returned of daLineSpc) & "]"
>>         if (doHline) then set tabLineBreak to tabLineBreak &  
>> return & kHline
>>         repeat with i from 1 to nColumns
>>                 --if (i > 1) then set colDefText to colDefText & "|"
>>                 set colDefText to colDefText & colJust
>>         end repeat
>>         set colDefText to colDefText & "}"
>>         set outText to outText & colDefText
>>         if (doHline) then set outText to outText & return & kHline
>>
>>         -- split lines into columns
>>         set nLines to count (cbl)
>>         repeat with i from 1 to nLines
>>                 set tmpStr to (item i of cbl)
>>                 if (my countSubstr(tmpStr, sepChar) > 1) then
>>                         set tableLine to return & my searchReplace 
>> (tmpStr, sepChar, kColSep)
>>                         if (i < nLines or doHline) then
>>                                 set tableLine to tableLine &  
>> tabLineBreak
>>                         end if
>>                         set outText to outText & tableLine
>>                 end if
>>         end repeat
>>
>>         -- finalize the table
>>         set outText to outText & return & "\\end{tabular}"
>>         -- insert the converted tabular data int the document
>>         set selection of front document to outText
>> end tell
>>
>> -- try to find the proper column separator
>> on getColSepChar(txtStr)
>>         repeat with ch in kSeparators
>>                 set sepChar to contents of ch
>>                 if (txtStr contains sepChar) then exit repeat
>>         end repeat
>>         return sepChar
>> end getColSepChar
>>
>> -- replace searchStr by replStr in txtStr
>> on searchReplace(txtStr, searchStr, replStr)
>>         set {oldDelims, text item delimiters} to {text item  
>> delimiters,
>> searchStr}
>>         set newText to text items of txtStr
>>         set text item delimiters to replStr
>>         set newText to newText as text
>>         set text item delimiters to oldDelims
>>         return newText
>> end searchReplace
>>
>> -- count the occurrences of searchStr in txtStr
>> on countSubstr(txtStr, searchStr)
>>         set {oldDelims, text item delimiters} to {text item  
>> delimiters,
>> searchStr}
>>         set newText to text items of txtStr
>>         set n to count newText
>>         set text item delimiters to oldDelims
>>         return n
>> end countSubstr

------------------------- Info --------------------------
Mac-TeX Website: http://www.esm.psu.edu/mac-tex/
          & FAQ: http://latex.yauh.de/faq/
TeX FAQ: http://www.tex.ac.uk/faq
List Archive: http://tug.org/pipermail/macostex-archives/




More information about the MacOSX-TeX mailing list