Positioning arrows in commutative diagrams using TikZ-cd (or otherwise)?

1,390

Solution 1

As far as tikz-cd is concerned, there is no way to point to a particular character in a cell, because all the commands refer to a cell. So there are two ways I can think of:

  • use the option shift left=<dimension> (or shift right) to manually adjust the arrows position. This will work for you because the characters in your example case are on top of each other but it's manual and it might not work for other cases. So I think in your case the best solution is the second one.

  • draw the diagram and the use tikzmark and TikZ do draw the arrows. I haven't tested this, but tikzmark has worked before inside a math environment, so it should work.

I don't know the other package so I can't speak on that. If I come up with a better solution, I'll edit the answer.

Solution 2

If you use tikzmark, you will want to use \subnode[]{}{}, I think, because you are using it inside, I believe, a node and PGF doesn't know anything about the contents of nodes but just treats them as boxes. \subnode boxes the content and keeps track of where it is. You can use this in the same picture environment as you would a normal node. Here, however, you probably want to overlay the arrows you need to add. For example:

\usepackage{tikz-cd,amssymb}
\usetikzlibrary{tikzmark,quotes}
\begin{document}
\begin{tikzcd}
  x \in \subnode{u1}{$U$} \subseteq M \arrow{r}{f} & f(x) \in V \subseteq \tikzmark{n}{N} \arrow{d}{\psi} \\
  \phi(x) \in \subnode{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}

subnodes

Share:
1,390

Related videos on Youtube

udit.m
Author by

udit.m

Graduate student at the University of Western Ontario, Canada.

Updated on January 15, 2020

Comments

  • udit.m
    udit.m almost 4 years

    I'm trying to make commutative diagrams to go with my math notes.
    So far, I've experimented with AMScd and TikZ-cd. But here's the problem: I need to align the arrows so that I can control where their tails and heads go. For instance, I have the following diagram:

    enter image description here

    I need to align the ϕ arrow's tail to be directly below U and its head to be directly above ϕ(U). Similarly, for the ψ arrow.

    Ideally, I'm looking for something simple (like the ampersand in the matrix, align, or eqnarray environments) because I'll need to make such diagrams throughout the document. Also, there will be more complicated diagrams with diagonal arrows!

    enter image description here

    I've experimented so far, unsuccessfully, with AMScd and TikZ-cd. I'm not really familiar with the TikZ package -- so I haven't tried using its matrix, or graph, or whatever it is. Here is the AMScd and TikZ-cd code that generatse the first diagram (I'm not really worried about aligning the diagonal arrow):

    Using AMScd:

    \[
    \begin{CD}
    x \in U \subseteq M @>f>> f\left(x\right) \in V \subseteq N \\
    @V\phi VV @VV\psi V \\
    \phi\left(x\right) \in \phi\left(U\right) \subseteq \mathbb{R}^{m} @>>\psi f \phi^{-1}> \psi\left(f\left(x\right)\right) \in \psi\left(V\right) \subseteq \mathbb{R}^{n}
    \end{CD}
    \]
    

    Using TikZ-cd:

    \[
    \begin{tikzcd}
    x \in U \subseteq M \arrow{r}{f}
    \arrow{d}{\phi} &
    f\left(x\right) \in V \subseteq N
    \arrow{d}{\psi} \\
    \phi\left(x\right) \in \phi\left(U\right) \subseteq \mathbb{R}^{m}
    \arrow{r}{\psi f \phi^{-1}} &
    \psi\left(f\left(x\right)\right) \in \psi\left(V\right) \subseteq \mathbb{R}^{n}
    \end{tikzcd}
    \]
    

    Any help would be welcome. Thanks in advance!

    • cfr
      cfr almost 8 years
      Could you please complete the code fragments so we have compilable code?
  • cfr
    cfr almost 8 years
    Maybe mention \subnode? (I assume tikz-cd uses nodes inside a tikzpicture so it isn't just a maths environment but also inside a node. Or am I wrong about that?)
  • Alenanno
    Alenanno almost 8 years
    @cfr I have no way to test that at the moment. But even then, how would you draw the arrow?
  • cfr
    cfr almost 8 years
    Just as usual. The point is you can't use a regular \tikzmark inside a node, I don't think.
  • Alenanno
    Alenanno almost 8 years
    @cfr I understand what you mean.
  • cfr
    cfr almost 8 years
    I added an example. They're easier to use in a way, I guess, as they are more like regular nodes... but different. At least, you don't have to remember whether it is pic cs or cs pic which I always get wrong.