Skip to content

Instantly share code, notes, and snippets.

@shhyou
Last active March 21, 2021 12:01
Show Gist options
  • Save shhyou/21e4f264a541a0848ad329bc22d2236e to your computer and use it in GitHub Desktop.
Save shhyou/21e4f264a541a0848ad329bc22d2236e to your computer and use it in GitHub Desktop.
#lang pollen
◊(define lam "\\lambda")
$◊lam_k\\nabla g_k(x)$
#lang racket/base
(module setup racket/base
(require (for-template "tex-top.rkt"))
(provide allow-unbound-ids?)
(define allow-unbound-ids? #'tex-top)
)
\documentclass{article}
\usepackage{amsmath}
\begin{document}
% from Pollen tutorial
◊(require racket/list)
◊(apply string-append (filter string? (flatten doc)))
\end{document}
#lang racket/base
(require (for-syntax racket/base racket/syntax racket/string)
(only-in txexpr [txexpr tex-wrapper-txexpr])
pollen/tag)
(provide tex-top
(for-syntax tex-top-wrapper))
(define-syntax (tex-top stx)
(syntax-case stx ()
[(_ . ID)
(tex-top-wrapper #'ID)]))
(define-for-syntax (tex-top-wrapper stx)
(define id (symbol->string (syntax-e stx)))
(define has-underscore-or-caret?
(for/first ([i (in-naturals)]
[c (in-string id)]
#:when (or (char=? #\_ c) (char=? #\^ c) (char=? #\. c)))
i))
(cond
[(and has-underscore-or-caret? (< 0 has-underscore-or-caret?))
(define prefix
(format-id stx "~a" (substring id 0 has-underscore-or-caret?)))
(define suffix
(substring id has-underscore-or-caret?))
#`(tex-wrapper-txexpr '@ '()
(list (def/c . #,prefix)
'#,suffix))]
[else
#`(default-tag-function '#,stx)]))
(define-syntax (def/c stx)
(syntax-case stx ()
[(_ . ID)
(identifier-binding #'ID)
#'ID]
[(_ . ID)
#'(#%top . ID)]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment