NB. Cliff's First J to TeX hack.        
NB. reiterc@lafayette.edu,  May 27, 2000
NB. Run this script, put J text unto clipboard,
NB.   Press F2 to convert clipboard to J text for embedding in a paragraph
NB.   Press F3 to convert clipboard to J text for a display
NB.   Press F4 to create a stand-alone Plain TeX document from the J text
NB. Note: Assumes ISIJ font is available to TeX
NB. I have problems with reading the clipboard on an NT 1G machine 
NB. Work around: copy text to clipboard in one J session, hit function 
NB. keys in another J session (this is ordinarily not usually necessary)


NB. Tex begin/end commands for the three function keys
f2beg=:'{\jfont',CRLF
f2end=:'}'
jparbeg=:'{\jfont',CRLF
f3beg=:'\bigskip',jparbeg
f3end=:'}',CRLF,'\bigskip',CRLF
f4beg=:'\font\jfont=ISIJ at 12pt',CRLF,jparbeg   NB. adjust font size here
f4end=:'}',CRLF,'\bye'

NB. define the three function keys as J tools
wd 'smsetcmd 2 1 "&Copy to Embedded Tex',TAB,'F2" "f2beg ] clip_to_tex f2end";'
wd 'smsetcmd 3 1 "&Copy to Displayed Tex',TAB,'F3" "f3beg par clip_to_tex f3end";'
wd 'smsetcmd 4 1 "&Copy to Complete Tex Doc',TAB,'F4" "f4beg par clip_to_tex f4end";'

clip_to_tex=:1 : 0      NB. copies j text from clipboad, applies j_to_tex and u.
:
y=.wd 'clippaste'
z=.u. j_to_tex y
EAV=.{:a.
wd 'clipcopy ',EAV,x.,z,y.,EAV
)

j_to_tex=:3 : 0        NB. basic j text to tex conversion
tex=.<"0 a.
tex=.('\char32 ';'\#';'\$';'\%';'\&';'\char147 ')32 35 36 37 38 92}tex
tex=.('\char136 ';'\char95 ';'\char145') 94 95 96}tex
tex=.('\char150 ';'\char151 ';'\char148 ';'\char152 ')123 124 125 126}tex
tex=.('\char146 ';'\char125 ';'\char62 ';'\char161 ';'\char191 ')39 34 191 60 62}tex
;(a.i.y.){tex
)

par=:3 : 0            NB. marks end of paragraphs and tightens box display
lines=.}:<@}:;._2 y.,CRLF
boxq=.(196&e. +. 179&e.)&(a.&i.@>) lines
p=.boxq{('\par',CRLF);('\par\nointerlineskip',CRLF)
; lines ,&.> p
)

NB. end of j_to_tex script