# LaTeXMathPreview

## Commentary

latex-math-preview creates images of particular region in LaTeX file and display them in emacs. latex-math-preview has the following main commands.

M-x ‘latex-math-preview-expression’ previews mathematical expressions pointed by cursor in LaTeX files or strings of selected region with transient-mark on. The result of this command is shown as image in new buffer.

M-x ‘latex-math-preview-insert-symbol’ displays the list of LaTeX symbols. Selecting a LaTeX symbol from it, you can insert it. Depending on whether cursor is in mathematical expression or not, this commands choose the symbol list. If you don’t want to use the automatic mode selection, alternatively you may use M-x ‘latex-math-preview-insert-mathematical-symbol’ and M-x ‘latex-math-preview-insert-text-symbol’ for mathematical symbol and normal text symbol, respectively.

M-x ‘latex-math-preview-save-image-file’ makes an image for the same object as ‘latex-math-preview-expression’ and save it as a file which is png or eps. When making an image, this command may remove number of mathematical formulas.

Buffer local variable ‘latex-math-preview-usepackage-cache’ has the values of \usepackage for previewing. If you want to reload this variable, you use M-x ‘latex-math-preview-reload-usepackage’.

M-x ‘latex-math-preview-beamer-frame’ makes an image of one frame of beamer, which is LaTeX style for presentation. This feature is experimental for now and it is possible not to work on your environment.

latex-math-preview.el is a modified version which is based on tex-math-preview.el and has been created at July 2009. This emacs lisp is made by reducing some features of tex-math-preview.el and adjusting it to LaTeX files. tex-math-preview.el is made by Kevin Ryde and has some features which latex-math-preview does not have. Please see http://user42.tuxfamily.org/tex-math-preview/index.html for details of tex-math-preview.el.

## Requirements

Because latex-math-preview displays images in emacs, it is not work in emacs on terminal.

### Version of Emacs

The version of emacs is 22 or 23 on Linux. latex-math-preview is tested on Ubuntu 10.04. But on Meadow 3 on Windows latex-math-preview probably works.

### Image Conversion

latex-math-preview uses some commands to convert tex to png, tex to eps, and so on. Only for previewing mathematical expressions, latex-math-preview requires latex and dvipng commands. According to your environment and settings of latex-math-preview, latex-math-preview creates preview images by combining the following commands.

• dvipng
• dvips
• latex
• platex
• pdflatex
• dvipdf
• dvipdfm
• dvipdfmx
• gs

## Install of Emacs Lisp

Put latex-math-preview.el to your load-path of Emacs and write the following code in ~/.emacs.el.

(autoload 'latex-math-preview-expression "latex-math-preview" nil t)


### Key Bindings

Please set key bindings of TeX mode which you prefer if desired.

For example, for YaTeX mode we add the following settings to ~/.emacs.el.

(add-hook 'yatex-mode-hook
'(lambda ()
(YaTeX-define-key "p" 'latex-math-preview-expression)
(YaTeX-define-key "\C-p" 'latex-math-preview-save-image-file)
(YaTeX-define-key "j" 'latex-math-preview-insert-symbol)
(YaTeX-define-key "\C-j" 'latex-math-preview-last-symbol-again)
(YaTeX-define-key "\C-b" 'latex-math-preview-beamer-frame)))
(setq latex-math-preview-in-math-mode-p-func 'YaTeX-in-math-mode-p)


In particular case latex-math-preview-in-math-mode-p does not work well. So you may use YaTeX-in-math-mode-p alternatively.

This setting almost binds latex-math-preview-expression to “C-c p”, latex-math-preview-save-image-file to “C-c C-p”, latex-math-preview-insert-symbol to “C-c j”, latex-math-preview-last-symbol-again to “C-c C-j”, and latex-math-preview-beamer-frame to “C-c C-b”.

### Path of Programs

latex-math-preview.el uses ‘latex’, ‘dvipng’ and ‘dvips’. If the programs (dvipng, dvips, latex, and so on) are not in the load path of system or you want to use the different programs from the default, you need to change the variable ‘latex-math-preview-command-path-alist’. For example,

(setq latex-math-preview-command-path-alist
'((latex . "/usr/bin/latex") (dvipng . "/usr/bin/dvipng") (dvips . "/usr/bin/dvips")))


## Usage

### latex-math-preview-expression

If you type M-x ‘latex-math-preview-expression’ when cursor points to a mathematical expression, new buffer including an image is created. In this buffer, you check the result of LaTeX mathematical expression and type ‘q’ to exit the window. Also, you can preview the strings of selected region if transient-mark is on (type “\C-@” twice).

In preview window, the following binded key is applicable.

• q: exit preview buffer
• Q: delete preview buffer
• j: scroll up
• k: scroll down

### latex-math-preview-insert-symbol

You can browse the list of LaTeX symbols if you execute “M-x latex-math-preview-insert-symbol”. Then you select the symbol by cursor which you want to insert and may type RET to insert it. When inserting symbols, you may look at the description of key map on the head of buffer.

When you open viewing buffer, if there is not image caches then you want for a while until the program finish making images. Also, you can make all image caches by M-x ‘latex-math-preview-make-all-cache-images’.

C-u M-x ‘latex-math-preview-insert-symbol’ asks you the page that you want to open before preview buffer is created. M-x ‘latex-math-preview-last-symbol-again’ make you insert the last inserted symbol. Also, if you insert a symbol, not depending on the context, you can use M-x ‘latex-math-preview-mathematical-symbol-datasets’ or M-x ‘latex-math-preview-insert-text-symbol’.

### latex-math-preview-save-image-file

First, M-x ‘latex-math-preview-save-image-file’ asks you abount path of an outputted image. Then, you must input the path of which extention is ‘png’ or ‘eps’. The program makes an image and you may get a desired one.

### latex-math-preview-beamer-frame

If we execute M-x ‘latex-math-preview-beamer-frame’ in \frame{ … } or \begin{frame} … \end{frame}, we can preview particular one page of beamer presentation same as ‘latex-math-preview-expression’.

## Settings

### LaTeX template

latex-math-preview.el makes a temporary file and gets an image by commands ‘latex’ and ‘dvipng’. The construction of a temporary latex file is the following.

(part of latex-math-preview-latex-template-header'
the default value is the following)
\documentclass{article}
\pagestyle{empty}

(part of usepackages
the values of \usepackage searched from current buffer or
latex-math-preview-latex-usepackage-for-not-tex-file')

\begin{document}
(some mathematical expressions)
\par
\end{document}


If you can change the header in temporary latex files, you should set the customized value to ‘latex-math-preview-latex-template-header’. latex-math-preview searches ‘\\usepackage’ in current buffer and uses its value when making images. But when there is no ‘\\usepackage’ strings, alternatively ‘latex-math-preview-latex-usepackage-for-not-tex-file’ is used. So you can set a preferd value to ‘latex-math-preview-latex-usepackage-for-not-tex-file’. The following variables is prepared for making image files.

• ‘latex-math-preview-latex-make-png-file-template-header’
• ‘latex-math-preview-latex-make-eps-file-template-header’

### Conversion Process

The default value of ‘latex-math-preview-tex-to-png-for-preview’ is

  (defvar latex-math-preview-tex-to-png-for-preview
'(latex dvipng))


This means when creating png images for preview latex-math-preview uses ‘latex-math-preview-execute-latex’ (tex to dvi) and ‘latex-math-preview-execute-dvipng’ (dvi to png) in series. If you use other programs to create png images, please edit this variable. For example, to use platex (tex to dvi), dvipdfmx (dvi to pdf), and gs (pdf to png),

  (defvar latex-math-preview-tex-to-png-for-preview
'(platex dvipdfmx gs-to-png))


The variables ‘latex-math-preview-tex-to-png-for-save’, ‘latex-math-preview-tex-to-eps-for-save’, and ‘latex-math-preview-beamer-to-png’ is the same. The default functions we can uses to convert images are

• ‘latex-math-preview-execute-latex’
• ‘latex-math-preview-execute-platex’
• ‘latex-math-preview-execute-pdflatex-to-dvi’
• ‘latex-math-preview-execute-pdflatex-to-pdf’
• ‘latex-math-preview-execute-dvipdf’
• ‘latex-math-preview-execute-dvipdfm’
• ‘latex-math-preview-execute-dvipdfmx’
• ‘latex-math-preview-execute-dvipng’
• ‘latex-math-preview-execute-dvips-to-ps’
• ‘latex-math-preview-execute-dvips-to-eps’
• ‘latex-math-preview-execute-gs-to-png’

For japanese, we recommend the following settings to use platex:

   (setq latex-math-preview-tex-to-png-for-preview '(platex dvipng))
(setq latex-math-preview-tex-to-png-for-save '(platex dvipng))
(setq latex-math-preview-tex-to-eps-for-save '(platex dvips-to-eps))
(setq latex-math-preview-beamer-to-png '(platex dvipdfmx gs-to-png))


### Options of commands

The options of commands are specified by ‘latex-math-preview-command-option-alist’ and the options for triming margins of images are specified by ‘latex-math-preview-command-trim-option-alist’. If you configure the commands, please modify these variables. Because this customize is advanced, we want you to refer to source code for details.

The color options of ‘dvipng’ is determined by ‘latex-math-preview-image-foreground-color’ and ‘latex-math-preview-image-background-color’, which define the foreground and background colors of png images respectively. If these variables are nil, these colors are the same as it of the default face.

### Matching mathematical expression

When you make preview images, the default setting supports the following LaTeX mathematical expressions for “M-x latex-math-preview-expression”.

• $…$
• $$…$$
• $…$
• \begin{math} … \end{math}
• \begin{displaymath} … \end{displaymath}
• $$…$$
• \begin{gather} … \end{gather}
• \begin{align} … \end{align}
• \begin{alignat} … \end{alignat}

If you want to preview other LaTeX mathematical expressions, please customize the variable ‘latex-math-preview-match-expression’.

### List of symbols for insertion

To change symbol set, you may customize the variable ‘latex-math-preview-mathematical-symbol-datasets’. If you always open the same inital page, you set nil to ‘latex-math-preview-restore-last-page-of-symbol-list’ and string of initial page name to ‘latex-math-preview-initial-page-of-symbol-list’.

This program keep the created images as cache. These images saved in ‘latex-math-preview-cache-directory-for-insertion’ of which default value is “~/.emacs.d/latex-math-preview-cache”. If you change the cache directory, please put the following code in “~/.emacs.el”.

(setq latex-math-preview-cache-directory-for-insertion


## Extra data

You may extra data for insertion of LaTeX symbols. The file is latex-math-preview-extra-data.el. To use extra data, you put latex-math-preview-extra-data.el to load path and require latex-math-preview.el and latex-math-preview-extra-data.el and write the following in ~/.emacs.el.

(add-to-list 'latex-math-preview-text-symbol-datasets
latex-math-preview-textcomp-symbol-data)
latex-math-preview-pifont-zapf-dingbats-symbol-data)