[OS X TeX] TeXShop macro: add column to tabular

Will Robertson will at mecheng.adelaide.edu.au
Sat Jul 17 02:46:26 EDT 2004


On 17 Jul 2004, at 4:10 PM, Will Robertson wrote:

> I've updated my "Add column" script. It now works in both TeXShop & 
> iTeXMac --- change the first line of the applescript to specify which 
> one you're using.

Oops, forgot to attach it.
Copy/paste the following into a new item in the macro editor of either 
TeXShop or iTeXMac:

--Applescript

-- Which TeX app?
property texapp : "TeXShop" -- or "iTeXMac"

-- CUSTOMISE AS YOU LIKE:
property default_button : "After"
--property default_button : "Before"
property cell_blob : "•"

--BEGIN:
set q to display dialog "This macro adds a new column to the selected 
tabular. Please enter the column number before or after which you would 
like to add a new colum." default answer "1" buttons {"Before", 
"After"} default button default_button

set add_where to the button returned of q
set col_num to the text returned of q

if texapp = "TeXShop" then
	tell application "TeXShop" to set tabular to the content of the 
selection of the front document
else if texapp = "iTeXMac" then
	tell application "iTeXMac" to set tabular to (the selection of the 
text of the front document)
end if

set new_tabular to ""


repeat with ii from 1 to the count of the paragraphs of tabular
	
	set this_line to paragraph ii of tabular
	
	if this_line contains "\\\\" and this_line does not contain 
"\\multicolumn" then
		
		set trim_line to do shell script "echo " & the quoted form of 
this_line & " | cut -f 1-1 -d '\\\\'"
		
		
		set new_col to "& " & cell_blob & " &"
		if add_where = "After" then
			set line_start to do shell script "echo " & the quoted form of 
trim_line & " | cut -f -" & col_num & " -d '&'"
			set line_end to do shell script "echo " & the quoted form of 
trim_line & " | cut -f " & col_num + 1 & "- -d '&'"
			if line_end = "" then
				set new_col to "& " & cell_blob & " "
			end if
		else if add_where = "Before" then
			if col_num = "1" then
				set line_start to ""
				set new_col to "• &"
				set line_end to trim_line
			else
				set line_start to do shell script "echo " & the quoted form of 
trim_line & " | cut -f -" & col_num - 1 & " -d '&'"
				set line_end to do shell script "echo " & the quoted form of 
trim_line & " | cut -f " & col_num & "- -d '&'"
				if line_end = "" then
					set new_col to "& " & cell_blob & " "
				end if
			end if
		end if
		
		set new_line to line_start & new_col & line_end & "\\\\"
		if new_tabular = "" then
			set new_tabular to new_line
		else
			set new_tabular to new_tabular & return & new_line
		end if
	else
		if new_tabular = "" then
			set new_tabular to this_line
		else
			set new_tabular to new_tabular & return & this_line
		end if
	end if
	
end repeat

if tabular contains "\\multicolumn" then
	display dialog "I cannot parse lines with “\\multicolumn” in them. 
You'll have to do them by hand. Sorry." buttons {"Cancel", "Proceed"} 
default button "Proceed" with icon caution
end if

if texapp = "TeXShop" then
	tell application "TeXShop" to set the selection of the front document 
to new_tabular
else if texapp = "iTeXMac" then
	tell application "iTeXMac" to insert new_tabular in the text of the 
front document
end if

--------------------- 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 Post: <mailto:MacOSX-TeX at email.esm.psu.edu>





More information about the MacOSX-TeX mailing list