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

Bernhard Barkow bb at creativeeyes.at
Tue Dec 6 06:51:45 EST 2005


Hi,

On 06.12.2005, at 09:19, Jan Hegewald wrote:
> Am 06.12.2005 um 08:29 schrieb Bernhard Barkow:
>
>> Wouldn't it be relatively easy (for an AppleScript expert, I mean)  
>> to write an import macro for TeXShop which parses the available  
>> clipboard data and creates a LaTeX table from it, with &s and \\s  
>> etc.? I don't know if that's possible at all, just had the idea...
>
> Some time ago I wrote an AppleScript which parses the clipboard to  
> insert the required & and \\ into a table data copied from Excel.  
> It only works for the rows. So you have to write the \tabular  
> header for yourself. If anyone is interested, I can see if I can  
> find it. Currently I use a ruby script to do this converting.

I played around a little and came up with the script below. It takes  
tabular data from the clipboard, tries to find out how it is  
structured (tab- or comma-delimited etc.), asks for some options and  
then creates the tabular environment and inserts it into the document  
(replacing a current selection, if present).
The result will probably still need some editing by the user. I don't  
know if it works with Excel (I don't have Excel), but it works from  
an AppleWorks spreadsheet, at least.
I didn't have time to check extensively for bugs, and there are  
certainly lots of possible improvements (especially the handling of  
white space is not very sophisticated yet), but maybe it helps;  
although it won't be as powerful as an external ruby or python  
script, I suppose.
Actually, I don't know why I didn't have that idea before, given my  
previous frustrations about importing tables into LaTeX...

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