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

Will Robertson will at mecheng.adelaide.edu.au
Fri Jul 16 05:01:08 EDT 2004


Hi all

Just before I race off I thought I'd put up my first efforts at a new 
macro for TeXShop. Instructions are the same: copy/paste into a new 
item in the TeXShop macro editor. iTeXMac version to come.

What to do with it:

Select a tabular environment, starting the selection with 
\begin{tabular} and ending it with \end{tabular}. If your table has one 
row per line and every line finishes with "\\", this macro can insert a 
new empty column in between the already existing columns in your 
tabular.

(It should work perfectly with my other "new table" macros I've 
recently posted.)

Try it out, send comments & bug reports to the list. I'll try and make 
it more robust over the next week or so.
Hope it works!

Will

<---CUT FROM HERE DOWN--->



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

-- DON'T EDIT!
property slashslash : "\\\\"

set q to display dialog "This macro adds a new column to the selected 
tabular. Please enter the column number to which you would like to add 
a new column before or after." 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 add_where = "Before" and col_num = "1" then
	display dialog "I'm no good with spacing when inserting a new first 
column. Do you still wish to continue?" buttons {"Cancel", "Continue"} 
default button "Continue"
end if

tell application "TeXShop"
	
	
	set tabular to the content of the selection of the front document
	set new_tabular to the first paragraph of tabular
	
	set lineone to the first paragraph of tabular
	set lineend to the last paragraph of tabular
	
	if lineone does not contain "\\begin{tabular}" or lineend does not 
contain "\\end{tabular}" then
		display dialog "Please select the whole tabular environment so the 
first line contains \"\\begin{tabular}\" and the last line contains 
\"\\end{tabular}\"."
	else
		
		repeat with ii from 2 to the count of the paragraphs of tabular
			
			set this_line to paragraph ii of tabular
			
			if paragraph ii of tabular contains "\\\\" 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 & slashslash
				set new_tabular to new_tabular & return & new_line
				
			else
				set new_tabular to new_tabular & return & this_line
			end if
			
		end repeat
		
		set the selection of the front document to new_tabular
		
	end if
	
end tell
--------------------- Info ---------------------
Mac OS X TeX: http://www.esm.psu.edu/mac-tex/
              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