* TAB4TEX
* Guido Milanese 2007 <[email protected]>
*
* Usage:  tab2tex.sno rows, align, vertical_bar, hline, complete
* First and second argument are compulsory, 3rd, 4th and 5th optional (simple y/n choice)
* Example: snobol4 -b tab4tex.sno r=7 a=c b=y h=y c=y <tab.txt >tab.tex
* outputs a table planned for 7 rows, centered, with '|' and hline, as a simple complete LaTeX document

* Sets digits, newline and tabulator
       digits = '1234567890'
-include "newline.inc"
       (~(nl  = newline()) (terminal = "Cannot set newline. Abort"))   :s(end)
       tab = char(9)
*****************************************
* Internal functions
*
* -- FORMAT LINES - Changes '*' and '!' to Bold and Emphasized
       Define("format(datum)")         :(format_end)
format
* Bold values
       ( (datum ? pos(0) '*' = ) (datum ? rpos(1)  '*' = )
+               (datum = "\textbf{" datum '}'))         :s(format_ret)
* Emphasized
       ( (datum ? pos(0) '!' = ) (datum ? rpos(1)  '!' = )
+               (datum = "\emph{"   datum '}'))         :s(format_ret)
* Small caps
       ( (datum ? pos(0) '^' = ) (datum ? rpos(1)  '^' = )
+               (datum = "\textsc{"   datum '}'))       :s(format_ret)

format_ret      format = datum  :(return)
format_end

****************************************

* Error messages
       Em = "snobol4 -b tab4tex.sno r=7 a=c b=y h=y c=y <tab.txt >tab.tex"
       Optmust = "You must set at least (1) number of rows and (2) alignment"
+                 nl "e.g.: r=4 a=c. 'r' must be a number and 'a' one of l,r,c (= left, right, centre)"
* Options:
* Rows, alignment, separator '|' or empty, hlines or empty, complete LaTeX or not
       Options = host(0)
       s_Options = size(Options)
       (~(gt(s_Options,0)) (terminal = Optmust)) :s(end)
* Rows
       (~(Options ? "r=" span(digits) . nrows) (terminal = Optmust))   :s(end)
* Align
       (~(Options ? "a=" any("lrcLRC") . align)        (terminal = Optmust))   :s(end)
* Vertical bars (separator)
       (~((Options ? "b=" any("yY") . sep) (sep = '|') ) (terminal = "No vertical bar required") )
* Hlines
       (~((Options ? "h=" any("yY") . hline) (hline = "\hline") (hline = nl hline)) (terminal = "No hline required") )
* Complete LaTeX?
       Complete = 'N'
       (~((Options ? "c=" any("yY") . complete) ) (terminal = "Complete LaTeX document not required") )
       Complete = replace(Complete,&ucase,&lcase)

* Builds data
       data1  = sep align                              ;* separator and alignment
       data   = dupl(data1,nrows)              ;* data (e.g. |c|c|c| )

* Complete LaTeX document required?
       (leq(complete,('y')))
+       (output = "\documentclass{article}" nl "\usepackage[T1]{fontenc}" nl "\begin{document}" nl)
* Table header
       output = "\begin{tabular}" '{' data sep '}' tab nl hline

* Main
MAIN
read    line = trim(input)              :f(endenv)
               line ? (tab | "<M")             :f(read)
               line = line tab
split   line ? break(tab) . cell len(1) rem . line      :f(outrow)

* Multicolumn cells (uses function Format)
       (
+       (cell ? pos(0) ( "<M"  (span(digits) . number) '>' ) =  )
+       (cell = format(cell))
+       (cell = "\multicolumn" '{' number '}' '{' sep align sep "}{" cell '}')
+       )
* Normal cells (uses function Format)
       cell = format(cell)

* Builds row
       (row = row cell tab '&' tab)            :(split)
* Writes row
outrow
       row ? rpos(3) (tab '&' tab) =
clean   row "<E>" = ' ' :s(clean)
       (output = row tab "\\" tab hline) (row = )      :(read)
       terminal = row :(read)
endenv
       output = "\end{tabular}"
       (leq(complete,'y')) (output = nl "\end{document}")

end