loopspace / tikzmark

The dread tikzmark arrives on github
14 stars 3 forks source link

In v1.7, \subnode and \tikzmarknode does not work within the inline math #8

Closed davidhjp closed 3 years ago

davidhjp commented 5 years ago

The following code from https://tex.stackexchange.com/questions/459497/tikzmark-subnode-in-math-changes-font-size generates an error with version 1.7:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\begin{document}

\begin{tikzpicture}[remember picture]
\node[align=left] {$A_A$ (no subnode) \\
                   $\tikzmarknode{n1}{A}_{\tikzmarknode{n2}{A}}$ (with tikzmarknode)};
\draw[<-] (n2) -- ++(0,-5mm);
\end{tikzpicture}

\end{document}

Generates error during the first compilation:

Package pgf Error: No shape named n2 is known. \draw[<-] (n2)
Package pgf Error: No shape named n2 is known. \draw[<-] (n2) -- ++(0,-5mm)

Generates pdf correctly from 2nd compilation and onwards.

No error is generated with the release ctan-2018-10-18.

loopspace commented 5 years ago

The mechanism for math-awareness of \subnode and \tikzmarknode is to use \mathchoice which typesets each of the four possibilities and then throws away the ones that aren't used. So when a node is defined, all four versions are actually created and we only know when it is typeset which one was actually used. By that time, it is too late to actually do anything other than write something to the auxfile about which one was used which can be picked up at the next compilation.

In the previous version of tikzmark then I defined a fall-back so that if we couldn't figure out which version was used (ie, when that information isn't yet in the auxfile) then it defaulted to the fall-back. That way, the node was always defined regardless. Unfortunately, this meant that if a math environment was inside another math environment (for example, inside \text, but also some more complicated cases) then the wrong version was being used. So I removed the fall-back.

I'll have a look to see if there's another way to implement the fall-back. I used a naive method originally but something more sophisticated might solve this.

Incidentally, the \tikzmarknode command shouldn't be used inside a math environment. I've added an answer at the linked question using \subnode instead.

loopspace commented 5 years ago

Try with https://github.com/loopspace/tikzmark/commit/05e58d010b324ad3f371b4c645f95828c0081524

davidhjp commented 5 years ago

The code from the link now works with 05e58d010b324ad3f371b4c645f95828c0081524, but still does not work with the following code, which I originally wanted to do:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{amsmath}
\usetikzlibrary{tikzmark}
\begin{document}
\begin{tikzpicture}[remember picture]
\node[] (eq) {
\parbox{\linewidth}{
    \begin{equation*}
        \begin{bmatrix}
        \subnode{f1}{$a$}
        \end{bmatrix}=
    \end{equation*}
}};
\draw[->] (1,1) -- (f1);
\end{tikzpicture}
\end{document}
loopspace commented 5 years ago

Whoops! I missed out some braces in the new code. Try with https://github.com/loopspace/tikzmark/commit/86f4bd718573cf08ddbc9297a113e7bc9278d79c . That works for me with your code.

Incidentally, the dollars inside the \subnode command shouldn't be there. They are actually switching out of math mode since the \subnode now detects if it is within a math environment and sets the inner environment accordingly.

Thanks for testing this, by the way.