Version 4 of texutil

Updated 2013-12-11 23:25:23 by AMG

MC: The various TeX variants are very powerful ways to typeset documents. (My favorites are ConTeXt & XeTeX .) Most of my projects involve typesetting data that is retrieved out of a database, so Tcl is my go to language. I can write a script that reads the data and marks it up, then render it to PDF, see if I like it, and tweak as necessary until I have something I'm happy with.

Since TeX commands look like \command[argument][arg2...] the backslash and brackets would need to be escaped for the Tcl interpreter, and that gets tiresome rather quickly. Hence the following small package that allows me to write TeX in a Tcl-ish way: @ command <argument> <arg2>.

 namespace eval TeXutil {
     variable buf ""
     variable map {@ \\ &amp; & &lt; < &gt; > &at; @ < \[ > \] ( \{ ) \}}

     proc @ {cmd args} {
         >> @$cmd[join $args ""]\n
     }

     proc %@ {cmd args} {
         % @$cmd {*}$args
     }

     proc >> {args} {
         variable buf
         variable map
         foreach datum $args {
             append buf [string map -nocase $map $datum]
         }
     }

     proc << {} {
         variable buf
         set out $buf
         set buf ""
         return $out
     }

     proc % {args} {
         >> %[join [split [join $args " "] \n] \n%]\n
     }

     namespace export << %@ @ % >>
     namespace ensemble create
 }

 package provide TeXutil 1.0

So the script:

 namespace import TeXutil::*
 @ setuppapersize <A6>
 @ starttext
 >> "Hello world" \n
 @ stoptext

 puts [<<]

Would produce:

 \setuppapersize[A6]
 \starttext
 Hello world
 \stoptext

AMG: I added () -> {} mapping for the sake of LaTeX.