6

I want to use tikzmark in tikzcd. However, even the following sample returns errors

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz-cd} 
\usetikzlibrary{tikzmark}

\begin{document}

\begin{tikzcd}
\tikzmark{u1}A & B\\
C & \tikzmark{u2}D
\end{tikzcd}

\begin{tikzpicture}[remember picture,overlay]
\draw[->] (u1) edge (u2);
\end{tikzpicture}
\end{document}

The error report is

Package tikz Error: Cannot parse this coordinate. \tikzmark{u1}A

Package tikz Error: Cannot parse this coordinate. C & \tikzmark{u2}D

Package pgf Error: No shape named u1 is known. \draw[->] (u1)

Package pgf Error: No shape named u1 is known. \draw[->] (u1) edge (u2)

Package pgf Error: No shape named u2 is known. \draw[->] (u1) edge (u2)

Package pgf Error: No shape named u2 is known. \draw[->] (u1) edge (u2)

Package pgf Error: No shape named u1 is known. \draw[->] (u1) edge (u2)

Label(s) may have changed. Rerun to get cross-references right.

How to fix this kind of errors?

Update: I have tried this

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz-cd} 
\usetikzlibrary{tikzmark}

\begin{document}

\begin{tikzcd}[remember picture]
aaaaaa\subnode{u1}{A} & B\\
C & \subnode{u2}{D}ddddddd
\end{tikzcd}

\begin{tikzpicture}[remember picture,overlay]
\draw[->] (u1) edge (u2);
\end{tikzpicture}
\end{document}

and this

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz-cd} 
\usetikzlibrary{tikzmark}

\begin{document}

\begin{tikzcd}[remember picture]
aaaaaa\tikzmarknode{u1}{A} & B\\
C & \tikzmarknode{u2}{D}ddddddd
\end{tikzcd}

\begin{tikzpicture}[remember picture,overlay]
\draw[->] (u1) edge (u2);
\end{tikzpicture}
\end{document}

But none of them put the arrow in correct location.

Update: The \tikzmarknode command does work correctly if I use LuaLaTex instead of XeLaTeX.

3
  • You don't. Just add remember picture to the tikzcd and refer to the nodes with (\tikzcdmatrixname-1-1) and so on.
    – user121799
    Commented Nov 4, 2018 at 19:38
  • 1
    Note that xelatex has known issues with positioning, and so with tikzmark. Commented Nov 4, 2018 at 20:46
  • 1
    Thanks for accepting my answer. However, the answer by LoopSpace is clearly better, Could you please accept it instead?
    – user121799
    Commented Nov 4, 2018 at 20:50

2 Answers 2

7

(Note: I agree with marmot that for this particular case I wouldn't use tikzmark, but I get that it is a simple example and it reveals a bug in tikzmark.)

It would appear that when I modified tikzmark to make it possible to use inside tikzpictures, then I inadvertently broke the ability to use it inside a node (though I'm not sure if that was ever a recommended use!). A node is inside a tikzpicture but should be treated as if it weren't. My current test for whether the tikzmark is inside a tikzpicture doesn't pick up on this. Here's the corrected test:

\documentclass{article}
%\url{https://tex.stackexchange.com/q/458387/86}
\usepackage{amsmath}
\usepackage{tikz-cd} 
\usetikzlibrary{tikzmark}

\makeatletter

\def\tikzmark{%
  \ifx\pgfpictureid\@undefined
  \let\tikzmark@next=\tikzmark@outside
  \else
  \relax
  \ifx\scope\tikz@origscope\relax
  \let\tikzmark@next=\tikzmark@outside
  \else
  \let\tikzmark@next=\tikzmark@inside
  \fi
  \fi
  \tikzmark@next%
}
\makeatother

\begin{document}

\begin{tikzcd}
\tikzmark{u1}A & B\\
C & \tikzmark{u2}D
\end{tikzcd}

\begin{tikzpicture}[remember picture,overlay]
\draw[->] (pic cs:u1) -- (pic cs:u2);
\end{tikzpicture}
\end{document}

I've uploaded this to github and will send it off to CTAN once the amazing testing team of marmot has answered a few questions to check that I haven't broken something else!

2
  • Great! That's what I was hoping for! The marmot testing team used this great package in several answers and everything worked absolutely smoothly!
    – user121799
    Commented Nov 4, 2018 at 20:49
  • 1
    My new maxim: never trust anything until it has been stress tested by a marmot. Commented Nov 4, 2018 at 21:00
4

REVISED ANSWER: I could get tikzmark to collaborate with tikz-cd by switching to \tikzmarknode. More precisely, if I rewrite cfr's nice answer to become

\documentclass{article}
\usepackage{tikz-cd,amssymb}
\usetikzlibrary{tikzmark,quotes}
\begin{document}
\begin{tikzcd}
  x \in \tikzmarknode{u1}{U} \subseteq M \arrow{r}{f} & f(x) \in V \subseteq 
  \tikzmarknode{n}{N} \arrow{d}{\psi} \\
  \phi(x) \in \tikzmarknode{u2}{\phi(U)} \subseteq \mathbb{R}^{m} \arrow{r}{\psi f \phi^{-1}} & \psi(f(x)) \in \psi(V) \subseteq \mathbb{R}^{n}
\end{tikzcd}
\begin{tikzpicture}[overlay, remember picture]
  \draw [->] (u1) edge ["$\phi$", right] (u2.north -| u1);
\end{tikzpicture}
\end{document}

I get

enter image description here

which might be what you are after.

Let me also mention that for your above example you may not use \tikzmark since you have the objects already in TikZ nodes.

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz-cd} 

\begin{document}

\begin{tikzcd}[remember picture]
A & B\\
C & D
\end{tikzcd}

\begin{tikzpicture}[remember picture,overlay]
\draw[->] (\tikzcdmatrixname-1-1) edge (\tikzcdmatrixname-2-2);
\end{tikzpicture}
\end{document}

enter image description here

11
  • What I mean is I cannot use tikzmark commands inside a tikzcd environment: as in my sample shows the command \tikzmark just cause "Cannot parse this coordinate" error.
    – Gau-Syu
    Commented Nov 4, 2018 at 19:45
  • @Gau-Syu Yes, that's not surprising since you are essentially nesting tikzpictures by doing this, which should be avoided.
    – user121799
    Commented Nov 4, 2018 at 19:46
  • What makes me confuse is that there are some answers in Tex.SE using tikzmark inside tikzcd, for example tex.stackexchange.com/a/287727/54169 and tex.stackexchange.com/a/176572/54169
    – Gau-Syu
    Commented Nov 4, 2018 at 19:49
  • @Gau-Syu Yes, but it is clearly stated that you need to use \subnode then. I believe that the thing above is simpler. (As I said, I really love tikzmark, but would not use it here.)
    – user121799
    Commented Nov 4, 2018 at 19:53
  • 1
    @marmot See my answer. Yet again, you've tracked down a bug in tikzmark. Commented Nov 4, 2018 at 20:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .