Moritz' Weblog

nothing to see here

Emacs Lisp Hacking, embedding in TeX
18 June 2010

Especially when you are using a lot of LaTeX mathematical notation in e-Mails, a convenient mechanism for displaying these LaTeX fragments rendered is more than welcome.

I am reading my e-Mails in Gnus, which makes it very easy to add an according function. Here is my solution:

The following shell script:

  #!/bin/bash
  set -e
  tmpdir=$(mktemp -d)
  tmpfile="${tmpdir}/file.tex"
  output="${tmpdir}/file.dvi"
  cat > "${tmpfile}" <<EOF
  \documentclass[a4paper,12pt]{article}
  \usepackage[utf8]{inputenc}
  \usepackage{amsmath}
  \usepackage{amssymb}
  \usepackage{bbm}
  \usepackage{ngerman}
  \begin{document}
  EOF
  cat >> "${tmpfile}"
  cat >> "${tmpfile}" <<EOF
  \end{document}
  EOF
  cd "$tmpdir"
  latex "${tmpfile}" && evince "${output}"

Plus the following hack for Emacs:

  (defun region-tex-view ()
    "Compile current region with LaTeX and display result"
(interactive)
(let* ((buf "*Tex Embed Output*")
       (cmd "~/bin/tex-embed")
           (ret (shell-command-on-region (region-beginning) (region-end)
                         cmd buf nil nil nil)))
      (and (= ret 0)
  (kill-buffer buf))))
  (global-set-key (kbd "<f9>") 'region-tex-view)

Then I only need to select the according region containing TeX fragments and press F9.

Page last updated: 18 June 2010