5

I have defined a custom macro for my equations, to simplify and decrease the amount of code I write

\newcommand{\eqn}[1]{
  \begin{equation}
    #1
  \end{equation}
}

\eqn{\alpha + \beta = \gamma}

But I don't have the syntax highlighting for math macros and sub/superscript stylings which AUCTeX provides, because it doesn't realize it is a math environment.

What should I do to enable those features inside \eqn{}?

5
  • 2
    I can't say something to the problem at hand, since i am a member of the vim-party. Please be careful with such abbreviations. Some won't even work (AFAIR align?) and it is extremely hard for a copy editor to review your paper/work. You can teach emacs how to set the outer environment and put the cursor inside, ready for your input. This would be much better.
    – Johannes_B
    Jul 12, 2014 at 14:15
  • 1
    You can invoke the whole \begin{equation}|\end{equation} with a single keystroke combination, with the cursor (which I represented by |) in the middle.
    – egreg
    Jul 12, 2014 at 14:17
  • Don't write unreadable and unmanageable, instead exploit autocompletion features: C-c C-e equation RET (the environment name can be autocompleted with TAB).
    – giordano
    Jul 12, 2014 at 14:35
  • I know those, but I still ask. I also have concerns about readibility and manageability, but what I am trying to will not necessarily cause those problems IMO. My projects will work locally, and will be more readable to me.
    – osolmaz
    Jul 12, 2014 at 14:53
  • +1 : it happens often that collaborators use custom macros and I'd like emacs to detect them and colour them appropriately (like the \be \ee for \begin{equation} \end{equation}. Barbarian, isn't it. (note : apparently gedit on ubuntu just gets it spot on with no customization.)
    – picop
    Apr 24, 2018 at 15:05

2 Answers 2

2

Note that it is very easy to introduce spurious spaces if making macros such as the one shown which can make a spurious "white" line above the equation consisting of just an indentation box and \rightskip glue. It adds less than \baselineskip of vertical space as it cause \bovedisplayshortskip to be used instead of \abovedisplayskip as the line abovethe equation is (very) short.

enter image description here

\documentclass{article}

\begin{document}

\newcommand{\eqn}[1]{
  \begin{equation}
    #1
  \end{equation}
}

\showoutput
\noindent
\begin{minipage}[t]{.5\textwidth}
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.
\eqn{x}
bbb

\hrule
\end{minipage}%
\begin{minipage}[t]{.5\textwidth}
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.
\begin{equation}
x
\end{equation}
bbb

\hrule
\end{minipage}

\end{document}

as for your original question see

texmathp-tex-commands is a variable defined in `texmathp.el'.
Its value is nil

  This variable is potentially risky when used as a file local variable.

Documentation:
List of environments and macros influencing (La)TeX math mode.
This user-defined list is used in addition to LaTeX and AMSLaTeX defaults.
The structure of each entry is (NAME TYPE)

- The first item in each entry is the name of an environment or macro.
  If it's a macro, include the backslash.

- The second item is a symbol indicating how the command works:
    `env-on'     Environment: turns math mode for its body  on
    `env-off'    Environment: turns math mode for its body  off
    `arg-on'     Command: turns math mode for its arguments on
    `arg-off'    Command: turns math mode for its arguments off
    `sw-on'      Switch: turns math-mode of following text  on
    `sw-off'     Switch: turns math-mode of following text  off
    `sw-toggle'  Switch: toggles math mode of following text

You can customize this variable.

[back]
6
  • I have tried to implement this, also by looking at this question's answer. tex.stackexchange.com/questions/66321/… . I cannot make it work, I even tried to modify auctex source, by directly adding to the list which also has \ensuremath. Anything comes up to mind, maybe post a working example?
    – osolmaz
    Jul 13, 2014 at 15:42
  • Also, would the problem with spaces still exist if I define it as: \newcommand{\eqn}[1]{\begin{equation}#1\end{equation}} in one line, or use %?
    – osolmaz
    Jul 13, 2014 at 15:45
  • 1
    @nrs either would fix the space (but still hiding the environment syntax isn't a good idea for lots of reasons, auctex isn't the only thing that then finds it harder to understand:-) Jul 13, 2014 at 15:47
  • I have long reverted back to using the proper environment :), but I still wonder why the answer posted in that question (from my first comment) does not work for me.
    – osolmaz
    Jul 13, 2014 at 15:57
  • @nrs maybe ask on an emacs forum rather than here? Jul 13, 2014 at 16:07
2

In line with what @Johannes_B says and @DavidCarlisle 's response, you would be better of creating a abbreviation within emacs. By typing something such as

/eqn

emacs will see this as an abbreviation and expand it to what you want.

There are various ways of setting up abbreviations. Here's an approach that I use:

Create an new abbreviation table

(defvar my-abbrev-table (make-abbrev-table))

Define a function

(defun my-insert-equation ()
  (insert "\\begin{equation}\n\n")
  (insert "\\end{equation}\n")
  (forward-line -2))

Then add this function to you abbreviation table

(define-abbrev my-abbrev-table "/eqn" "" 'my-insert-equation t)

Make this your local abbrev table

(setq local-abbrev-table my-abbrev-table)

The above can all go in its own .el file.

Then add to your tex-mode-hook a call to load that .el file. You should be good to go from there.

If you make sure that your function returns t, then you can control whether the expansion control character will also be used as input text.

Compare the following two:

(defun my-insert-equation ()
  (insert "\\begin{equation}\n\n")
  (insert "\\end{equation}\n")
  (forward-line -2)
  t)

(defun my-insert-junk () 
  (insert "this is just junk")
  t)

Now add the further line

(put 'my-insert-equation 'no-self-insert t)

Create the following two keys:

(define-abbrev my-abbrev-table "/eqn"  "" 'my-insert-equation t)
(define-abbrev my-abbrev-table "/junk" "" 'my-insert-junk     t)

Then by typing

/ e q n . 

emacs will expand this to

\begin{equation}
[]
\end{equation}

where [] represents the position of the cursor. Notice how the period, ., does not show up.

However by typing

/ j u n k .

emacs will expand this to

this is just junk.[]

Notice how the punctuation used to trigger the expansion is included in the expansion.

IMPORTANT POINT

Regarding the above abbreviations, I forgot that I've modified my syntax table.

Abbreviations work only on word characters. To get the above to work, you'll have to modify the syntax table as follows

(modify-syntax-entry ?/ "w" tex-mode-syntax-table)

But this isn't really necessary to do if you define your abbreviation with the usual word characters. By making / a word character I create a little inconvenience when moving forward or backward over file paths, but that's just about the only time this modification creates headaches (and not much of one at that). By adopting this syntax, I'm confident that my abbreviation won't be something I might actually want to type in without expansion.

Nevertheless,

(define-abbrev my-abbrev-table "eqn"  "" 'my-insert-equation t)
(define-abbrev my-abbrev-table "junk" "" 'my-insert-junk     t)

would be perfectly good abbreviations. In case you really just wanted, for example, junk to be input as just junk and not expanded by the next non-word character, then you could type

j u n k \c-q <non-word character>

and emacs will not expand the word. But, I'm too stupid to recognize the difference when in the heat of the moment creating a document.

Incidentally, if all your abbreviation is going to do is expand into another string, (like in the case of /junk) then there's no need to create a function corresponding to this abbreviation. Instead you can just define it as

(define-abbrev my-abbrev-table "/junk" "This is just junk.  Plain and simple" nil t)
4
  • (LaTeX-environment-menu "equation") is a much better idea for the body of your my-insert-equation function.
    – giordano
    Jul 12, 2014 at 16:45
  • @giordano I'm running on an older version of emacs for which that functionality doesn't exist. I should probably really update things.
    – A.Ellett
    Jul 12, 2014 at 16:47
  • Or switch to AUCTeX: LaTeX-environment-menu is an AUCTeX function.
    – giordano
    Jul 12, 2014 at 17:13
  • @giordano My fear with updating is the last time I updated I had a major headache reestablishing my preferences and trying to figure out how to turn off all the cute new features (such as super/subscripts) that I find more annoyances than anything else. But someone day soon, I'll just have to bite the bullet.
    – A.Ellett
    Jul 12, 2014 at 17:15

You must log in to answer this question.

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