# Why can't I type a dollar sign in this latex equation?

Consider the following latex document.

\documentclass{article}

\usepackage{tikz}

\begin{document}
Consider the following picture.
$\begin{tikzpicture} \node (X) {X}; % this X should be enclosed in a dollar sign to be in math mode \node[right of= X] (Y) {Y}; % this Y should be enclosed in a dollar sign to be in math mode \draw[->] (X.east) -- (Y.west); \end{tikzpicture}$
\end{document}


I want to enclose the X and Y in the tikzpicture environment with dollar signs (because I want them typeset in math mode). However, when I attempt to do so, emacs throws an error in the minibuffer reading Math mode started with '$' cannot be closed with dollar. What's wrong here? I'm using GNU Emacs 27.1 with AUCTeX-version 12.3.1. Note that this is not a latex question since my desired code (below) compiles fine in latex. \documentclass{article} \usepackage{tikz} \begin{document} Consider the following picture. \[ \begin{tikzpicture} \node (X) {X}; \node[right of= X] (Y) {Y}; \draw[->] (X.east) -- (Y.west); \end{tikzpicture}$
\end{document}


AucTeX redefines the binding for $ to be the command TeX-insert-dollar. That command checks whether you are in a math environment already and complains if you try to close it the "wrong" way. Doing C-h f TeX-insert-dollar shows you the doc string of the function: Insert dollar sign. If current math mode was not entered with a dollar, refuse to insert one. Show matching dollar sign if this dollar sign ends the TeX math mode and ‘blink-matching-paren’ is non-nil. When outside math mode, the behavior is controlled by the variable ‘TeX-electric-math’. With raw C-u prefix, insert exactly one dollar sign. With optional ARG, insert that many dollar signs.  Note the part that says: If current math mode was not entered with a dollar, refuse to insert one. BTW, it's not clear to me what the $...$ delimiters are buying you. If you get rid of them, then AucTeX will not complain about the $ signs, and you still have a centered display. CORRECTION: It's not centered but you can do that with \begin{center}...\end{center} and you'll have to add some space before and after it perhaps.
$\begin{tikzpicture}[every text node part/.style={font=\itshape}] \node (X) {X}; \node[right of= X] (Y) {Y}; \draw[->] (X.east) -- (Y.west); \end{tikzpicture}$

And yet another way, closer in spirit to the $...$ method but without resorting to \$ signs:
$\begin{tikzpicture} \node (X) {\ensuremath{X}}; \node[right of= X] (Y) {\ensuremath{Y}}; \draw[->] (X.east) -- (Y.west); \end{tikzpicture}$