November 16, 2007
Escaping LaTeX code in Lisp
I need to escape strings that get inserted into a LaTeX document for an upcoming project, and found this on the net:
(define latex-escape (string-escaper '((#\\ . "\\\\") (#\~ . "\\~") (#\# . "\\#") (#\$ . "\\$") (#\% . "\\%") (#\^ . "\\^") (#\& . "\\&") (#\{ . "\\{") (#\} . "\\}") (#\_ . "\\_"))))
where string-escaper obviously is a function returning a function that escapes those characters in the string. They implemented string-escaper in Scheme (probably portable) and thus got a quite large bunch of LOC; I suppose it’d be less in Lisp. especially with the help of loop constructs.
I’ll keep you updated on it, since I can’t work on it right now.