[OS X TeX] Insert a column

Alain Schremmer Schremmer.Alain at gmail.com
Thu Dec 21 15:32:46 EST 2006


Herbert Schulz wrote:

>
> On Dec 21, 2006, at 12:51 PM, Alain Schremmer wrote:
>
>> Is there a way (other than by hand) to insert a new column in a 9  
>> column table (with a couple of hundred rows) that is already partly  
>> filled-in?
>>
>> Hopeful regards
>> --schremmer
>
>
> Howdy,
>
> There are a set of TeXShop macros (written in Applescript) that  
> create tables or arrays and can add/delete columns. I'm not sure if  
> they came with TeXShop or I added them later and I don't have the  
> name of the author either. Sigh...
>
> To add a column select the rows of the tabular and use the Add Column  
> macro. It will ask for the number of the column to add before/after  
> and also whether it is before/after. There will be bullets as marks  
> in the column contents so you can use the macros for finding bullets  
> (these come with the Command Completion files I put together) to go  
> to and select each mark so it is easy to add the new data.
>
> New Tabular-------------
> --Applescript direct
>
> -- CUSTOMISE TO YOUR LIKING:
> property indent : "  " -- edit this if you want other than 2 spaces  
> for your indent
> property cell_text : "• " -- edit this if you want a different text blob
>
> tell application "TeXShop"
>     set q1 to display dialog "New tabular" & return & return & "Enter  
> the number of columns you'd like and then select the type of tabular  
> you want." default answer "3" buttons {"Cancel", "Plain", "Booktabs"}  
> default button "Booktabs"
>     set NCOLS to the text returned of q1
>     set tabtype to the button returned of q1
>     
>     set q2 to display dialog "Enter the number of rows you'd like 
> (not  including a header row) and select whether you'd like a floating 
> or  simply centred table." default answer "3" buttons {"Cancel",  
> "Floating", "Centered"} default button "Centered"
>     set NROWS to the text returned of q2
>     set floattype to the button returned of q2
>     
>     if the tabtype = "Booktabs" then
>         set the toprule to "\\toprule"
>         set the midrule to "\\midrule"
>         set the bottomrule to "\\bottomrule"
>     else
>         set the toprule to "\\hline"
>         set the midrule to "\\hline"
>         set the bottomrule to "\\hline"
>     end if
>     
>     if the floattype = "Floating" then
>         set the beforetext to ¬
>             "\\begin{table}[htbp]" & return & ¬
>             indent & "\\centering" & return & ¬
>             indent & "\\begin{tabular}" -- note no carriage return to 
> allow  space for the column preamble
>         set the aftertext to ¬
>             indent & indent & bottomrule & return & ¬
>             indent & "\\end{tabular}" & return & ¬
>             indent & "\\caption{TableCaption}" & return & ¬
>             indent & "\\label{tab:label}" & return & ¬
>             "\\end{table}"
>     else
>         set the beforetext to ¬
>             "\\begin{center}" & return & ¬
>             indent & "\\begin{tabular}"
>         set the aftertext to ¬
>             indent & indent & bottomrule & return & ¬
>             indent & "\\end{tabular}" & return & ¬
>             "\\end{center}"
>     end if
>     
>     set the column_preamble to "{@{} c"
>     set the one_row_text to indent & indent & cell_text
>     
>     if NCOLS > 1 then
>         repeat with c from 2 to NCOLS
>             set the column_preamble to column_preamble & "c"
>             set the one_row_text to one_row_text & "& " & cell_text
>         end repeat
>     end if
>     
>     set the column_preamble to column_preamble & " @{}}" & return
>     set the one_row_text to one_row_text & "\\\\ "
>     
>     -- start off with a header row between two rules:
>     set the midtext to ¬
>         indent & indent & toprule & return & ¬
>         one_row_text & return & ¬
>         indent & indent & midrule & return
>     
>     repeat with r from 1 to NROWS
>         set the midtext to midtext & one_row_text & return
>     end repeat
>     
>     set alltext to beforetext & column_preamble & midtext & aftertext
>     
>     tell application "TeXShop" to set the content of the selection of  
> the front document to alltext
>     
> end tell
> End New Tabular------------------
> New Array----------------------
> --Applescript direct
>
> -- CUSTOMISE TO YOUR LIKING:
> property indent : "  " -- edit this if you want other than 2 spaces  
> for your indent
> property cell_text : "• " -- edit this if you want a different text blob
>
> tell application "TeXShop"
>     set q2 to display dialog "New maths array." & return & return &  
> "Enter the number of rows you'd like." default answer "3"
>     set NROWS to the text returned of q2
>     
>     set q1 to display dialog "How many columns would you like? Would 
> you  like me to indent the array?" default answer "3" buttons 
> {"Cancel",  "Indent", "Flush left"} default button "Flush left"
>     set NCOLS to the text returned of q1
>     set indentyesno to the button returned of q1
>     
>     if the indentyesno = "Indent" then
>         set the preindent to indent
>     else
>         set the preindent to ""
>     end if
>     
>     set the column_preamble to "{c"
>     set the one_row_text to preindent & indent & cell_text
>     
>     if NCOLS > 1 then
>         repeat with c from 2 to NCOLS
>             set the column_preamble to column_preamble & "c"
>             set the one_row_text to one_row_text & "& " & cell_text
>         end repeat
>     end if
>     
>     set the column_preamble to column_preamble & "}" & return
>     set the one_row_text to one_row_text & "\\\\ " & return
>     
>     set midtext to ""
>     repeat with r from 1 to NROWS
>         set the midtext to midtext & one_row_text
>     end repeat
>     
>     set the beforetext to preindent & "\\begin{array}"
>     set the aftertext to preindent & "\\end{array}"
>     
>     set alltext to beforetext & column_preamble & midtext & aftertext
>     
>     tell application "TeXShop" to set the content of the selection of  
> the front document to alltext
> end tell
> End New Array---------------------
> New Column----------------------
> --Applescript direct
>
> -- CUSTOMISE AS YOU LIKE:
> property indent : "  "
> property default_button : "After" -- or "Before"
> property cell_blob : "•"
>
> --BEGIN:
>
> tell application "TeXShop"
>     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 column." 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
>     
>     tell application "TeXShop" to set tabular to the content of the  
> selection of the front document
>     
>     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
>            
>             --Transform "    a & b & c \\" to "a & b & c":
>             set trim_line to do shell script ¬
>                 "echo " & the quoted form of this_line & ¬
>                 " | sed 's/^[ ^t]*//' | sed 's/\\\\\\\\//'"
>             -- "\\\\\\\\" is a TeX newline escaped in both Applescript 
> and the  shell
>            
>            
>             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 cell_blob & " & "
>                     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 indent & indent & 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
>     
>     tell application "TeXShop" to set the selection of the front  
> document to new_tabular
>     
>     display dialog "Don't forget to change the column header!" 
> buttons  {"•"} giving up after 4
>     
> end tell
> End New Column----------------
> Delete Column-----------------
> --Applescript direct
>
> property indent : "  "
>
> --BEGIN:
> tell application "TeXShop"
>     
>     set tabular to the content of the selection of the front document
>     
>     if tabular = "" then
>         display dialog "Nothing selected. Please select the 
> tabular/array  rows you wish to edit." buttons "Cancel" default button 1
>     end if
>     
>     set q to display dialog "Please enter the number of the column 
> you  would like to delete." default answer "1" buttons {"Cancel",  
> "Delete"} default button "Delete"
>     
>     set col_num to the text returned of q
>     
>     
>     set sample_value to ""
>     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
>             -- note "\\\\" means \\ because the backslashes are escaped
>            
>             --Transform "    a & b & c \\" to "a & b & c":
>             set trim_line to do shell script ¬
>                 "echo " & the quoted form of this_line & ¬
>                 " | sed 's/^[ ^t]*//' | sed 's/\\\\\\\\//'"
>             -- "\\\\\\\\" is a TeX newline escaped in both Applescript 
> and the  shell
>            
>             set new_col to "&"
>             if sample_value = "" then
>                 set sample_value to do shell script "echo " & the 
> quoted form of  trim_line & " | cut -f " & col_num & "-" & col_num & " 
> -d '&'"
>             end if
>             if col_num = "1" then
>                 set line_start to ""
>                 set line_end to do shell script "echo " & the quoted 
> form of  trim_line & " | cut -f " & col_num + 1 & "- -d '&'"
>                 set new_col to ""
>             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 + 1 & "- -d '&'"
>                 if line_end = "" then
>                     set new_col to ""
>                 end if
>             end if
>            
>            
>             set new_line to indent & indent & 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
>     
>     display dialog "Are you sure you want to delete the column that  
> begins with “" & sample_value & "”?" buttons {"Cancel", "Delete"}  
> default button "Delete"
>     
>     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
>     
>     tell application "TeXShop" to set the selection of the front  
> document to new_tabular
>     
>     display dialog "Don't forget to delete columns from the column  
> header!" buttons {"•"} giving up after 4
>     
> end tell
> End Delete Column-------------------
>
> Will the person who wrote these macros please sand up! :-)
> Hope I have permission to put them in this message.

I thought I remembered having seen or read about them but couldn't find 
them so I had assumed something along the line of wishful tinking.

If if everybody muds down, I will try tonight.
If somebody sands up, I will try Goedde's solution—with NeoOffice of 
course.
In no case shall I even think of trying regex as I am truly afraid of 
these things.

Grateful regards to Goedde, Schulz and Munn.
--schremmer

------------------------- Helpful Info -------------------------
Mac-TeX Website: http://www.esm.psu.edu/mac-tex/
TeX FAQ: http://www.tex.ac.uk/faq
List Archive: http://tug.org/pipermail/macostex-archives/
List Reminders & Etiquette: http://www.esm.psu.edu/mac-tex/list/





More information about the MacOSX-TeX mailing list