Sample demo.tex
file that has tikzpicture
within a equation
% Created 2022-10-14 Fri 14:48
% Intended LaTeX compiler: pdflatex
\documentclass[11pt]{article}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{hyperref}
\usepackage{tikz}
\date{\today}
\title{}
\hypersetup{
pdftitle={},
pdfkeywords={},
pdfsubject={},
pdfcreator={Emacs 29.0.50 (Org mode 9.5.5)},
pdflang={English}}
\begin{document}
\tableofcontents
\begin{equation}
\begin{tikzpicture}[baseline=(current bounding box.center)]
\node {sqrt} % root
child { node {sqrt-iter}
child[sibling distance=5cm] { node{ good-enough }
child[sibling distance=2cm] { node { square } }
child[sibling distance=2cm] { node { abs } } }
child { node{ improve }
child { node { average } } } };
\end{tikzpicture}
\end{equation}
\begin{equation}
x=\sqrt{b}
\end{equation}
If \(a^2=b\) and \(b=2\), then the solution must be
either $$ a=+\sqrt{2} $$ or \[ a=-\sqrt{2} \].
\end{document}
How the above tex
file is rendered

How the buffer looks like in emacs -Q
tikzpicture
within equation
environment gets a font-latex-math-face

Copy the following snippet to *scratch*
, and do M-x eval-buffer
.
(add-hook 'LaTeX-mode-hook
(defun my-LaTeX-mode-hook ()
(hi-lock-mode 1)
(highlight-regexp
(rx (seq
"\\begin{tikzpicture}"
"[" (group (one-or-more any)) "]"
(group (minimal-match (one-or-more (or any "\n"))))
"\\end{tikzpicture}"))
'hi-yellow 2)
(font-lock-flush))
t)
Visit the above demo.tex
and do M-x revert-buffer
. This is how it looks
tikzpicture
gets its own face (which is hi-yellow
)

To highlight tikzpicture
in default
face, change 'hi-yellow
in the snippet to 'default
.
When I open the .tex
file, and I do C-h m
I get
The major mode is LaTeX/P
mode defined in tex-mode.el
:
This function has :override
advice: TeX-latex-mode
.
This is an :override advice
, which means that latex-mode
isn’t
run at all, and the documentation below may be irrelevant.
I also see
TeX-latex-mode
is an autoloaded interactive byte-compiled Lisp
function in latex.el
.
(TeX-latex-mode)
Major mode in AUCTeX for editing LaTeX files.
See info under AUCTeX for full documentation.
I had trouble finding the relevant hook
. You may have to find what hook
is run in your case. See https://git.savannah.gnu.org/cgit/emacs/elpa.git/tree/latex.el?h=b91f15b3a375445985143ed1d6e41490ea62780a#n7161. As the screenshots, there is no problem with the snippet. May be our running environments are different. May be try putting it on TeX-mode-hook
.
To test the snippet, you don't have to have to put it on a hook
. Just add an interactive
spec to my-LaTeX-mode-hook
, and call the function with M-x ...
.
(I am not a tex
user, auctex
is one of the big old packages which was outside of Emacs
for a long time. So, it has plenty of "hacks")