# Detection inside general LaTeX (not necessarily Math) commands

This question is related to how to detect if inside a LaTeX math environment?.

How to do the correct settings in .emacs in such a way that the variable texmathp takes the value t (like in math formulae) when the cursor is at the following commands?

\newpage ;command for creating a new page
\label{blabla}
\cite{blabla}
\ref{blabla}
\footnote{blabla}
\section{blabla}
\chapter{blabla}
\\ ;command for creating a new line


In the answer it was mentioned that one can use texmathp-tex-commands to achieve such things. Let me ask how to do it for the above formulae.

• TeX-current-macro returns the name of the current macro point is on, is it what you need? – giordano Jan 18 '15 at 12:08
• @giordano Let me say no, I just want a Boolean variable which tells me if I am at a LaTeX command (like texmathp for math formulae). T. Verron says in that answer that one can do such things with texmathp by adding new values to texmathp-tex-commands. I just want to see how to do it. – Name Jan 18 '15 at 12:15
• Why do you want to abuse texmathp? TeX-current-macro is there for that reason, returns the name of the macro if there is one at point, nil otherwise, so it can be used as a boolean. – giordano Jan 18 '15 at 20:17
• Thanks @giordano I am now persuaded that your approach is far better than what I had in mind. Could you please convert your comment to an answer in order that I may be able to mark it as accepted (for example printing the message in the echo area "Inside LaTeX environment" if TeX-current-macro is non-nil otherwise printing "Outside LaTeX environment"). – Name Jan 21 '15 at 13:21
• Uh, did you mean "macro" instead of "environment"? – giordano Jan 22 '15 at 5:04

The function you are looking for is TeX-current-macro. Here is an example of how you can use it to print a message depending on whether point is inside or outside a TeX macro:

(defun mg-TeX-current-macro-p ()
(interactive)
(if (TeX-current-macro)
(message "Inside a LaTeX macro")
(message "Outside a LaTeX macro")))