2

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}
1
  • +1 for the question — this is a horrible “feature”! A warning for dollar signs inside displaymath would be great, but simply blocking the user from typing it is horribly disruptive in the (atypical, but not terribly rare) situations where it’s actually desired. There’s a good reason why mismatched bracket detection and other comparable features just issue warnings, and don’t outright block typing. Commented May 25, 2022 at 0:06

1 Answer 1

2

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.

Alternatively, keep the display but change the font of the text of the nodes:

\[
  \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}
\]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.