File: texmathp.el.html
This code provides a function to determine if point in a buffer is
inside a (La)TeX math environment. This is not trivial since many
different ways are used to switch between the two. Examples:
\begin{equation} ... \end{equation}
$ ... $
$$ ... $$
M-x ... \ ( ... \)
\ensuremath{...}
\mbox{...}
To install, put this file on your load-path and compile it.
To use this in a Lisp program, do
(require 'texmathp)
You can then write code like this:
(if (texmathp) ...)
The call to texmathp leaves some extra information in the
variable texmathp-why. It's value is a cons cell (MATCH . POSITION),
specifying which command at what position is responsible for math
mode being on or off.
To configure which macros and environments influence LaTeX math
mode, customize the variable texmathp-tex-commands. By default
it recognizes the plain TeX and LaTeX core as well as AMS-LaTeX
and packages mathtools, empheq and breqn (see the variable
texmathp-tex-commands-default, also as an example).
To try out the code interactively, use M-x texmathp RET.
Of course, in order to work this function has to assume that the
LaTeX above point is syntactically correct. In particular:
o The different math delimiters are paired correctly. Thus if
o However, texmathp will correctly work with nested delimiters.
Something like the following will be parsed correctly at any point:
\begin{equation}
x = y \mbox{abc \ensuremath{\alpha} cba $2^3$}
\end{equation}
o texmathp is somewhat forgiving if you have an empty line inside
the current math environment, which is not legal in TeX but may
easily happen during editing. Depending upon the variable
`texmathp-search-n-paragraphs' several paragraphs are checked
backwards, by default 2. Paragraph here means something limited
by an empty line.
--------------------------------------------------------------------------
BUGS:
o If any of the the special macros like \mbox or \ensuremath has
optional arguments, math mode inside these optional arguments is
*not* influenced by the macro.
o Nested \(\) and \[\ o However, texmathp will correctly work with nested delimiters.
Something like the following will be parsed correctly at any point:
\begin{equation}
x = y \mbox{abc \ensuremath{\alpha} cba $2^3$}
\end{equation}
o texmathp is somewhat forgiving if you have an empty line inside
the current math environment, which is not legal in TeX but may
easily happen during editing. Depending upon the variable
`texmathp-search-n-paragraphs' several paragraphs are checked
backwards, by default 2. Paragraph here means something limited
by an empty line.
--------------------------------------------------------------------------
BUGS:
o If any of the the special macros like \mbox or \ensuremath has
optional arguments, math mode inside these optional arguments is
*not* influenced by the macro.
o Nested \(\) and \[\ o However, texmathp will correctly work with nested delimiters.
Something like the following will be parsed correctly at any point:
\begin{equation}
x = y \mbox{abc \ensuremath{\alpha} cba $2^3$}
\end{equation}
o texmathp is somewhat forgiving if you have an empty line inside
the current math environment, which is not legal in TeX but may
easily happen during editing. Depending upon the variable
`texmathp-search-n-paragraphs' several paragraphs are checked
backwards, by default 2. Paragraph here means something limited
by an empty line.
--------------------------------------------------------------------------
BUGS:
o If any of the the special macros like \mbox or \ensuremath has
optional arguments, math mode inside these optional arguments is
*not* influenced by the macro.
o Nested \(\) and \[\ o However, texmathp will correctly work with nested delimiters.
Something like the following will be parsed correctly at any point:
\begin{equation}
x = y \mbox{abc \ensuremath{\alpha} cba $2^3$}
\end{equation}
o texmathp is somewhat forgiving if you have an empty line inside
the current math environment, which is not legal in TeX but may
easily happen during editing. Depending upon the variable
`texmathp-search-n-paragraphs' several paragraphs are checked
backwards, by default 2. Paragraph here means something limited
by an empty line.
--------------------------------------------------------------------------
BUGS:
o If any of the the special macros like \mbox or \ensuremath has
optional arguments, math mode inside these optional arguments is
*not* influenced by the macro.
o Nested \(\) and \[\ o However, texmathp will correctly work with nested delimiters.
Something like the following will be parsed correctly at any point:
\begin{equation}
x = y \mbox{abc \ensuremath{\alpha} cba $2^3$}
\end{equation}
o texmathp is somewhat forgiving if you have an empty line inside
the current math environment, which is not legal in TeX but may
easily happen during editing. Depending upon the variable
`texmathp-search-n-paragraphs' several paragraphs are checked
backwards, by default 2. Paragraph here means something limited
by an empty line.
--------------------------------------------------------------------------
BUGS:
o If any of the the special macros like \mbox or \ensuremath has
optional arguments, math mode inside these optional arguments is
*not* influenced by the macro.
o Nested \(\) and \[\ o However, texmathp will correctly work with nested delimiters.
Something like the following will be parsed correctly at any point:
\begin{equation}
x = y \mbox{abc \ensuremath{\alpha} cba $2^3$}
\end{equation}
o texmathp is somewhat forgiving if you have an empty line inside
the current math environment, which is not legal in TeX but may
easily happen during editing. Depending upon the variable
`texmathp-search-n-paragraphs' several paragraphs are checked
backwards, by default 2. Paragraph here means something limited
by an empty line.
--------------------------------------------------------------------------
BUGS:
o If any of the the special macros like \mbox or \ensuremath has
optional arguments, math mode inside these optional arguments is
*not* influenced by the macro.
o Nested \(\) and \[\ o However, texmathp will correctly work with nested delimiters.
Something like the following will be parsed correctly at any point:
\begin{equation}
x = y \mbox{abc \ensuremath{\alpha} cba $2^3$}
\end{equation}
o texmathp is somewhat forgiving if you have an empty line inside
the current math environment, which is not legal in TeX but may
easily happen during editing. Depending upon the variable
`texmathp-search-n-paragraphs' several paragraphs are checked
backwards, by default 2. Paragraph here means something limited
by an empty line.
--------------------------------------------------------------------------
BUGS:
o If any of the the special macros like \mbox or \ensuremath has
optional arguments, math mode inside these optional arguments is
*not* influenced by the macro.
o Nested \(\) and \[\ o However, texmathp will correctly work with nested delimiters.
Something like the following will be parsed correctly at any point:
\begin{equation}
x = y \mbox{abc \ensuremath{\alpha} cba $2^3$}
\end{equation}
o texmathp is somewhat forgiving if you have an empty line inside
the current math environment, which is not legal in TeX but may
easily happen during editing. Depending upon the variable
`texmathp-search-n-paragraphs' several paragraphs are checked
backwards, by default 2. Paragraph here means something limited
by an empty line.
--------------------------------------------------------------------------
BUGS:
o If any of the the special macros like \mbox or \ensuremath has
optional arguments, math mode inside these optional arguments is
*not* influenced by the macro.
o Nested \(\) and \[\ o However, texmathp will correctly work with nested delimiters.
Something like the following will be parsed correctly at any point:
\begin{equation}
x = y \mbox{abc \ensuremath{\alpha} cba $2^3$}
\end{equation}
o texmathp is somewhat forgiving if you have an empty line inside
the current math environment, which is not legal in TeX but may
easily happen during editing. Depending upon the variable
`texmathp-search-n-paragraphs' several paragraphs are checked
backwards, by default 2. Paragraph here means something limited
by an empty line.
--------------------------------------------------------------------------
BUGS:
o If any of the the special macros like \mbox or \ensuremath has
optional arguments, math mode inside these optional arguments is
*not* influenced by the macro.
o Nested \(\) and \[\ o However, texmathp will correctly work with nested delimiters.
Something like the following will be parsed correctly at any point:
\begin{equation}
x = y \mbox{abc \ensuremath{\alpha} cba $2^3$}
\end{equation}
o texmathp is somewhat forgiving if you have an empty line inside
the current math environment, which is not legal in TeX but may
easily happen during editing. Depending upon the variable
`texmathp-search-n-paragraphs' several paragraphs are checked
backwards, by default 2. Paragraph here means something limited
by an empty line.
--------------------------------------------------------------------------
BUGS:
o If any of the the special macros like \mbox or \ensuremath has
optional arguments, math mode inside these optional arguments is
*not* influenced by the macro.
o Nested \(\) and \[\ o However, texmathp will correctly work with nested delimiters.
Something like the following will be parsed correctly at any point:
\begin{equation}
x = y \mbox{abc \ensuremath{\alpha} cba $2^3$}
\end{equation}
o texmathp is somewhat forgiving if you have an empty line inside
the current math environment, which is not legal in TeX but may
easily happen during editing. Depending upon the variable
`texmathp-search-n-paragraphs' several paragraphs are checked
backwards, by default 2. Paragraph here means something limited
by an empty line.
--------------------------------------------------------------------------
BUGS:
o If any of the the special macros like \mbox or \ensuremath has
optional arguments, math mode inside these optional arguments is
*not* influenced by the macro.
o Nested \(\) and \[\ o However, texmathp will correctly work with nested delimiters.
Something like the following will be parsed correctly at any point:
\begin{equation}
x = y \mbox{abc \ensuremath{\alpha} cba $2^3$}
\end{equation}
o texmathp is somewhat forgiving if you have an empty line inside
the current math environment, which is not legal in TeX but may
easily happen during editing. Depending upon the variable
`texmathp-search-n-paragraphs' several paragraphs are checked
backwards, by default 2. Paragraph here means something limited
by an empty line.
--------------------------------------------------------------------------
BUGS:
o If any of the the special macros like \mbox or \ensuremath has
optional arguments, math mode inside these optional arguments is
*not* influenced by the macro.
o Nested \(\) and \[\ o However, texmathp will correctly work with nested delimiters.
Something like the following will be parsed correctly at any point:
\begin{equation}
x = y \mbox{abc \ensuremath{\alpha} cba $2^3$}
\end{equation}
o texmathp is somewhat forgiving if you have an empty line inside
the current math environment, which is not legal in TeX but may
easily happen during editing. Depending upon the variable
`texmathp-search-n-paragraphs' several paragraphs are checked
backwards, by default 2. Paragraph here means something limited
by an empty line.
--------------------------------------------------------------------------
BUGS:
o If any of the the special macros like \mbox or \ensuremath has
optional arguments, math mode inside these optional arguments is
*not* influenced by the macro.
o Nested \(\) and \[\ o However, texmathp will correctly work with nested delimiters.
Something like the following will be parsed correctly at any point:
\begin{equation}
x = y \mbox{abc \ensuremath{\alpha} cba $2^3$}
\end{equation}
o texmathp is somewhat forgiving if you have an empty line inside
the current math environment, which is not legal in TeX but may
easily happen during editing. Depending upon the variable
`texmathp-search-n-paragraphs' several paragraphs are checked
backwards, by default 2. Paragraph here means something limited
by an empty line.
--------------------------------------------------------------------------
BUGS:
o If any of the the special macros like \mbox or \ensuremath has
optional arguments, math mode inside these optional arguments is
*not* influenced by the macro.
o Nested \(\) and \[\ o However, texmathp will correctly work with nested delimiters.
Something like the following will be parsed correctly at any point:
\begin{equation}
x = y \mbox{abc \ensuremath{\alpha} cba $2^3$}
\end{equation}
o texmathp is somewhat forgiving if you have an empty line inside
the current math environment, which is not legal in TeX but may
easily happen during editing. Depending upon the variable
`texmathp-search-n-paragraphs' several paragraphs are checked
backwards, by default 2. Paragraph here means something limited
by an empty line.
--------------------------------------------------------------------------
BUGS:
o If any of the the special macros like \mbox or \ensuremath has
optional arguments, math mode inside these optional arguments is
*not* influenced by the macro.
o Nested \(\) and \[\ o However, texmathp will correctly work with nested delimiters.
Something like the following will be parsed correctly at any point:
\begin{equation}
x = y \mbox{abc \ensuremath{\alpha} cba $2^3$}
\end{equation}
o texmathp is somewhat forgiving if you have an empty line inside
the current math environment, which is not legal in TeX but may
easily happen during editing. Depending upon the variable
`texmathp-search-n-paragraphs' several paragraphs are checked
backwards, by default 2. Paragraph here means something limited
by an empty line.
--------------------------------------------------------------------------
BUGS:
o If any of the the special macros like \mbox or \ensuremath has
optional arguments, math mode inside these optional arguments is
*not* influenced by the macro.
o Nested \(\) and \[\ o However, texmathp will correctly work with nested delimiters.
Something like the following will be parsed correctly at any point:
\begin{equation}
x = y \mbox{abc \ensuremath{\alpha} cba $2^3$}
\end{equation}
o texmathp is somewhat forgiving if you have an empty line inside
the current math environment, which is not legal in TeX but may
easily happen during editing. Depending upon the variable
`texmathp-search-n-paragraphs' several paragraphs are checked
backwards, by default 2. Paragraph here means something limited
by an empty line.
--------------------------------------------------------------------------
BUGS:
o If any of the the special macros like \mbox or \ensuremath has
optional arguments, math mode inside these optional arguments is
*not* influenced by the macro.
o Nested \(\) and \[\ o However, texmathp will correctly work with nested delimiters.
Something like the following will be parsed correctly at any point:
\begin{equation}
x = y \mbox{abc \ensuremath{\alpha} cba $2^3$}
\end{equation}
o texmathp is somewhat forgiving if you have an empty line inside
the current math environment, which is not legal in TeX but may
easily happen during editing. Depending upon the variable
`texmathp-search-n-paragraphs' several paragraphs are checked
backwards, by default 2. Paragraph here means something limited
by an empty line.
--------------------------------------------------------------------------
BUGS:
o If any of the the special macros like \mbox or \ensuremath has
optional arguments, math mode inside these optional arguments is
*not* influenced by the macro.
o Nested \(\) and \[\ o However, texmathp will correctly work with nested delimiters.
Something like the following will be parsed correctly at any point:
\begin{equation}
x = y \mbox{abc \ensuremath{\alpha} cba $2^3$}
\end{equation}
o texmathp is somewhat forgiving if you have an empty line inside
the current math environment, which is not legal in TeX but may
easily happen during editing. Depending upon the variable
`texmathp-search-n-paragraphs' several paragraphs are checked
backwards, by default 2. Paragraph here means something limited
by an empty line.
--------------------------------------------------------------------------
BUGS:
o If any of the the special macros like \mbox or \ensuremath has
optional arguments, math mode inside these optional arguments is
*not* influenced by the macro.
o Nested \(\) and \[\
you do things like "\\begin{equation} $" or "\\[ ... \\)"
the result of (texmathp) is undefined. It is in fact possible
in LaTeX to pair M-x with $$ and \( with $, but this will confuse
( with $$ and \( with $, but this will confuse
texmathp (and human readers as well).
)
texmathp (and human readers as well).
( with $$ and \( with $, but this will confuse
texmathp (and human readers as well).
)
o However, texmathp will correctly work with nested delimiters.
( with $$ and \( with $, but this will confuse
texmathp (and human readers as well).
)
Something like the following will be parsed correctly at any point:
( with $$ and \( with $, but this will confuse
texmathp (and human readers as well).
)
\begin{equation}
( with $$ and \( with $, but this will confuse
texmathp (and human readers as well).
)
x = y \mbox{abc \ensuremath{\alpha} cba $2^3$}
( with $$ and \( with $, but this will confuse
texmathp (and human readers as well).
)
\end{equation}
( with $$ and \( with $, but this will confuse
texmathp (and human readers as well).
)
o texmathp is somewhat forgiving if you have an empty line inside
( with $$ and \( with $, but this will confuse
texmathp (and human readers as well).
)
the current math environment, which is not legal in TeX but may
( with $$ and \( with $, but this will confuse
texmathp (and human readers as well).
)
easily happen during editing. Depending upon the variable
( with $$ and \( with $, but this will confuse
texmathp (and human readers as well).
)
`texmathp-search-n-paragraphs' several paragraphs are checked
( with $$ and \( with $, but this will confuse
texmathp (and human readers as well).
)
backwards, by default 2. Paragraph here means something limited
( with $$ and \( with $, but this will confuse
texmathp (and human readers as well).
)
by an empty line.
( with $$ and \( with $, but this will confuse
texmathp (and human readers as well).
)
--------------------------------------------------------------------------
( with $$ and \( with $, but this will confuse
texmathp (and human readers as well).
)
BUGS:
( with $$ and \( with $, but this will confuse
texmathp (and human readers as well).
)
o If any of the the special macros like \mbox or \ensuremath has
( with $$ and \( with $, but this will confuse
texmathp (and human readers as well).
)
optional arguments, math mode inside these optional arguments is
( with $$ and \( with $, but this will confuse
texmathp (and human readers as well).
)
*not* influenced by the macro.
( with $$ and \( with $, but this will confuse
texmathp (and human readers as well).
)
o Nested \(\) and \[\ ( with $$ and \( with $, but this will confuse
texmathp (and human readers as well).
) can confuse texmathp. It returns nil at AAA
in the following examples:
M-x x=y \mbox{abc \(\alpha\) cba} AAA \ ( x=y \mbox{abc \(\alpha\) cba} AAA \)
M-x x=y \begin{minipage}{3cm} abc \[\alpha\ ( x=y \begin{minipage}{3cm} abc \[\alpha\) cba \end{minipage} AAA \]
o In the "text column" of cases* environment, texmathp doesn't
consider it's non-math mode. The same applies for variants of
cases* environents, both provided by mathtools package.
--------------------------------------------------------------------------
Defined variables (6)
texmathp-allow-detached-args | Non-nil means, allow arguments of macros to be detached by whitespace. |
texmathp-search-n-paragraphs | Number of paragraphs to check before point. |
texmathp-syntax-table | Syntax table used while texmathp is parsing. |
texmathp-tex-commands | List of environments and macros influencing (La)TeX math mode. |
texmathp-tex-commands-default | The default entries for ‘texmathp-tex-commands’, which see. |
texmathp-why | After a call to ‘texmathp’ this variable shows why math-mode is on or off. |
Defined functions (5)
texmathp | () |
texmathp-compile | () |
texmathp-match-environment | (BOUND) |
texmathp-match-macro | (BOUND) |
texmathp-match-switch | (BOUND) |