Optimize TeX/PDF exports

Replaced looped queries with joins, per 4c8649814 (XML export)