namespace eval TeXutil { variable buf "" variable map {@ \\ & & < < > > &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.0So the script:
namespace import TeXutil::* @ setuppapersize <A6> @ starttext >> "Hello world" \n @ stoptext puts [<<]Would produce:
\setuppapersize[A6] \starttext Hello world \stoptextAMG: 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)(@Large@bfseries) >> latex @newcommand@sectionbreak(@ifnum@value(section)`>1@clearpage@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 latexProduces (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.