3

Usually, when redacting math stuff, I deal many times with the following situation : I have written some formula in a displaymath environment and, when compiling, realize it should look better inline ; then maybe return to the displaymath environment if inline is not satifying.

So I have something like

The presheaf
\begin{displaymath}
    F \colon C \to \mathsf{Set}
\end{displaymath}
is a sheaf.

When compiling, I realize I would prefer it inline. So I switch to

The presheaf $F \colon C \to \mathsf{Set}$ is a sheaf.

But it requires me to : erase the \begin{...} and the line break before it, insert a $ instead, erase the \end{...} and the line break after it, and insert a $ instead. Kind of long for such a current operation, especially when I change it to the way it was just after that.

Is there some key binding in AucTeX mode to turn a selected displaymath formula into a inline one, and vice versa ? Like a C-u C-c C-e (which changes the innermost environment).


Edit. I realize this is more a Emacs questions than a *TeX one, but I couldn't think of a better stackexchange to ask it.

  • 1
    You can use \[...\] for display math and \(...\) for inline math. Then, the change is easier. (Not sure if this comment applies to AucTeX) – Steven B. Segletes May 20 '14 at 13:56
  • 1
    @StevenB.Segletes I like \begin{displaymath} ... \end{displaymath} so I can easily change from displaymath to equation or align or whatever. Plus, \(...\) is kind of less readable than $...$ in my opinion. – Pece May 20 '14 at 13:59
  • Then how about \newenvironment{textmath}{$}{$}? Actually I think the already existing \begin{math}...\end{math} will accomplish what you want. Then, just delete or add the words "display". – Steven B. Segletes May 20 '14 at 14:04
  • 1
    @StevenB.Segletes Yes, it would certainly do the job. However, if there is a emacs way to do it without changing the syntax, I would prefer it (to un-inline in the source things that are inline in the compiled file... it breaks a little the readability of the source). – Pece May 20 '14 at 14:09
  • 1
    @StevenB.Segletes Yes, this is what I ask. If this is part of AucTeX, I can't find the command. If it is not, how to define it ? (I'm not quite comfortable with emacs-lisp programming.) – Pece May 20 '14 at 14:15
5

Add the following function to your .emacs:

(defun mg-LaTeX-swap-inline-display-math ()
  "Swap between inline math and display math."
  (interactive)
  (save-excursion
    (when (texmathp)
      (cond
       ((equal (car texmathp-why) "$")
    (goto-char (cdr texmathp-why))
    (delete-char 1)
    (push-mark)
    (search-forward "$")
    (delete-char -1)
    (exchange-point-and-mark)
    (LaTeX-insert-environment "displaymath"))
       ((equal (car texmathp-why) "displaymath")
    (LaTeX-find-matching-begin)
    (re-search-forward "\\\\begin{displaymath}[ \t\n\r]*")
    (replace-match "$")
    (LaTeX-find-matching-end)
    (re-search-backward "[ \t\n\r]*\n\\\\end{displaymath}")
    (replace-match "$"))))))

With this function, you can just leave the point inside the displaymath environment and issue M-x mg-LaTeX-swap-inline-display-math RET to convert the environment to an inline math, the other way round if you are between $...$. Nothing happens in other cases.

You can also bind the function to your favorite shortcut. For example, if you want to bind it to C-c m add the following code to your .emacs:

(eval-after-load "latex"
    '(define-key LaTeX-mode-map (kbd "C-c m") 'mg-LaTeX-swap-inline-display-math))
  • When the displaymath environment is inside another environment and is indented, the command (re-search-backward "[ \t\n\r]*\n\\\\end{displaymath}") doesn't find the end of environment because of the leading whitespace. For me replacing that line with (re-search-backward "[ \t\n\r]*\n[ \t\n\r]*\\\\end{displaymath}") worked better. – Balazs Strenner Feb 28 '15 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.