Updated 2013-12-12 03:05:41 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
         append buf [string map -nocase $map [join $args ""]]
     }

     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.

AMG: Here's an incompatible, bare-bones version:
proc >> {varName args} {
    upvar 1 $varName var
    append var \n[string map\
        {`` ` `< < `> > `( ( `) ) `@ @ @ \\ < \[ > \] ( \{ ) \}} [join $args]]
}
proc >>q {args} {
    string map {
        ` `` < `< > `> ( `( ) `) @ `@
        $ @$ # @# % @% _ @_ & @& \{ @\{ \} @\} ^ @^()
    } [join $args]
}

Instead of using XML-like &entity; notation to quote the specials, I use backtick, which resembles backslash.

Single-argument [join] is used because LaTeX doesn't care about extra whitespace in the places where I'd expect to see separate arguments. Also I put an extra newline at the beginning because LaTeX lets me be lazy.

The name of the buffer variable is explicitly specified as the first argument. To get the contents of the buffer, just read the variable.

The [>>q] procedure quotes not only characters special to [>>] but also characters special to LaTeX. This makes it suitable for quoting arbitrary display text.

Real-life example snippet:
    # Define page layout.
    >> latex @documentclass<letterpaper>(report)
    >> latex @usepackage<landscape,left=1cm,top=1cm,right=1cm,bottom=1cm>\
             (geometry)

    # Customize section headers.
    >> latex @usepackage(titlesec)
    >> latex @titleformat(@section)<frame>()()(4pt)(@[email protected])
    >> latex @[email protected](@[email protected](section)`>[email protected]@fi)

    # Define \"d\" tabular column type for decimal alignment.
    >> latex @usepackage(dcolumn)
    >> latex @newcolumntype(d)(D(.)(.)(-1))

    # Remove standard header and footer.
    >> latex @pagestyle(empty)
    >> latex @begin(document)

    set latex

Produces (with a blank line at the beginning):
% Define page layout.
\documentclass[letterpaper]{report}
\usepackage[landscape,left=1cm,top=1cm,right=1cm,bottom=1cm] {geometry}
% Customize section headers.
\usepackage{titlesec}
\titleformat{\section}[frame]{}{}{4pt}{\Large\bfseries}
\newcommand\sectionbreak{\ifnum\value{section}>1\clearpage\fi}
% Define "d" tabular column type for decimal alignment.
\usepackage{dcolumn}
\newcolumntype{d}{D{.}{.}{-1}}
% Remove standard header and footer.
\pagestyle{empty}
\begin{document}

Notice the need to quote the greater-than sign with a backtick.

My example doesn't show it, but [>>q] comes in handy for pasting in text which could contain specials.