14

I have recently been trying to use the breqn package to automatically break up my equations. However, I use emacs (and/or Aquamacs, depending on the machine I'm on) with Auctex, and it does not recognize these environments as math environments which is a bit of a headache.

Is there a list somewhere or a lisp command I can put in a .emacs file that tells auctex to recognize the math environments introduced by breqn as math environments?

3 Answers 3

12

You can tell auctex about additional math environments using LaTeX-add-environments. For example, I sometimes use math environments named thm, prop, lem, cor, defn, not, rem, ex, notation, and equation*, and I tell auctex about them with the following lines in my .xemacs/init.el file:

(add-hook 'LaTeX-mode-hook 'add-my-latex-environments)
(defun add-my-latex-environments ()
  (LaTeX-add-environments
   '("thm" LaTeX-env-label)
   '("prop" LaTeX-env-label)
   '("lem" LaTeX-env-label)
   '("cor" LaTeX-env-label)
   '("defn" LaTeX-env-label)
   '("not" LaTeX-env-label)
   '("rem" LaTeX-env-label)
   '("ex" LaTeX-env-label)
   '("notation" LaTeX-env-label)
   '("equation*" LaTeX-env-label)))

Edit: In addition, here's some code suggested by thequark to have proper syntax highlighting for the dmath environment under auctex, plus a couple of other improvements:

(add-hook 'LaTeX-mode-hook 'add-my-latex-environments) 
(defun add-my-latex-environments () 
  (LaTeX-add-environments 
     '("dmath" LaTeX-env-label))) 

;; Code I added to make syntax highlighting work in Auctex 

(custom-set-variables 
 '(font-latex-math-environments (quote  
     ("display" "displaymath" "equation" "eqnarray" "gather" "multline"  
      "align" "alignat" "xalignat" "dmath"))) 
  '(TeX-insert-braces nil)) ;;Stops putting {} on argumentless commands to "save" whitespace 

;; Additionally, reftex code to recognize this environment as an equation 
(setq reftex-label-alist 
  '(("dmath" ?e nil nil t))) 
4
  • Thanks! This is definitely a step in the right direction; Now LaTeX recognizes the environments, but they still don't show up highlighted for math (and backtick-a still outputs \alpha{} not \alpha, so auctex thinks I'm inserting text, not math). Is there a way to have auctex recognize these environments as math?
    – qgp07
    Sep 5, 2011 at 17:09
  • I had to had a few more lines to get this to work as a math environment. If you edit your answer to include these lines of code, I'd be happy to accept it. (This example is for dmath in particular.) gist.github.com/1195737
    – qgp07
    Sep 5, 2011 at 19:37
  • Thanks a lot, guys! One more thing though: how can I make preview (C-c C-p C-b) to recognise the environment and show the typeset text over the environment, not below it? Aug 1, 2017 at 22:53
  • Using font-latex-math-environments is deprecated: gnu.org/software/auctex/manual/auctex/…
    – pavel
    Oct 27, 2020 at 17:59
2

If you want the environment to be recognized by texmathp (used for instance by company-auctex), you can do it the following way.

(add-to-list 'texmathp-tex-commands '("breqn" env-on))
(texmathp-compile)
1
  • Thanks for this. I needed to modify your code a bit to get it to work: (add-hook 'LaTeX-mode-hook #'(lambda () (texmathp-compile))) (custom-set-variables '(texmathp-tex-commands (append texmathp-tex-commands-default '(("breqn" env-on))))).
    – Edward H
    Sep 17, 2017 at 17:46
0

In theory, you don't have to do anything to make it work. You should have included a minimal example providing info on how your document is structured. Suppose your document is structured as follows.

\documentclass[a4paper]{article}

\usepackage{breqn}

\begin{document}
\begin{dmath}
  f(x)=\frac{1}{x} \condition{for \(x\neq 0\)}
\end{dmath}
\end{document}

Then all you need to do is to execute TeX-normal-mode (C-c C-n) to force the generation of automatic local styles in the auto/ directory relative to the current directory. The auto local style is only needed to be generated once if no further package is to be added to your preamble. The following is the generated file.

(TeX-add-style-hook
 "tmp"
 (lambda ()
   (TeX-add-to-alist 'LaTeX-provided-class-options
                     '(("article" "a4paper")))
   (TeX-run-style-hooks
    "latex2e"
    "article"
    "art10"
    "breqn"))
 :latex)

You can have a very good guess on what the above code mean. If your document is structered in the following way instead, you might have to do more to make it work.

\documentclass[a4paper]{article}

\input{my-custom-preamble}

\begin{document}
\begin{dmath}
  f(x)=\frac{1}{x} \condition{for \(x\neq 0\)}
\end{dmath}
\end{document}

Assume my-custom-preamble.tex in the current directory is a preamble file you maintained for your own use across many files and the breqn package is loaded via this file instead of the document you are editing. Then the automatically generated local style would be as follows.

(TeX-add-style-hook
 "tmp"
 (lambda ()
   (TeX-add-to-alist 'LaTeX-provided-class-options
                     '(("article" "a4paper")))
   (TeX-run-style-hooks
    "latex2e"
    "my-custom-preamble"
    "article"
    "art10"))
 :latex)

It tries to load the my-custom-preamble.el style file but this is unknown to AUCTeX. In this case, create a my-custom-preamble.el with the following content, either in the style/ directory (TeX-style-local) relative to the current directory or in ~/.emacs.d/auctex/style/ directory (TeX-style-private).

(TeX-add-style-hook
 "my-custom-preamble"
 (lambda ()
   (TeX-run-style-hooks
    "breqn"))
 :latex)

You get the idea behind it. And this is the way to do it for most common packages since the styles for them are predefined in the ~/.emacs.d/elpa/auctex-/style/ directory (TeX-style-global). If you use some packages for which there are no predefeind correspondent style files in that directory, then you have two ways. You can run M-x TeX-auto-generate-global to generate automatic global styles for all the packages found in the TeX distribution installed on your computer. This is time-consuming and generate style files with byte-compiled files totaling around 100 MiB in the TeX-auto-global directory. Or you can generate the file on a package-by-package basis with the TeX-auto-generate command.

Finally, your setup might be different from mine resulting in different directories being used. Check your TeX-style-path, TeX-style-local, and relevant variables.

As a gentle reminder, you should never do the following. Most functionality of AUCTeX will fail for such documents without \documentclass{...}.

\input{my-custom-preamble}

\begin{document}
<The content of your document>
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .