4

I drew a diagram in tikz-cd using the following

\begin{tikzcd}
    & A \arrow[r]  & B\arrow[loop above] & C\arrow[l]\\
    D\arrow[r] & E\arrow[loop above] &  & F\arrow[ul]\\
     & G\arrow[u] & H\arrow[ul] & \\
     & J\arrow[u] & & \\
\end{tikzcd}

However I want to draw a curve separating the two disconnected diagrams. For this, I wanted to use options of tikz-picture like declaring coordinates and drawing curves, in the same picture drawn by tikz-cd. Is that possible? I tried embedding tikz-cd inside tikz-picture

\begin{tikzpicture}
\begin{tikzcd}
    & A \arrow[r]  & B\arrow[loop above] & C\arrow[l]\\
    D\arrow[r] & E\arrow[loop above] &  & F\arrow[ul]\\
     & G\arrow[u] & H\arrow[ul] & \\
     & J\arrow[u] & & \\
\end{tikzcd}
\end{tikzpicture}

but that didn't work.

1 Answer 1

3

As for your suggestion to wrap the tikzcd environment in a tikzpicture. Please don't! This amounts to nesting tikzpicture, which is not supported and can lead to all sorts of complications.

I will assume that you are not looking for something like the example on the top of p. 13 of the tikz-cd manual. This allows you to embed matrices in the tikzpicture but it won't support the \arrow command (without further ado). That this, the following assumes that you do want to use the \arrow command. Then there are at least two ways to add plain TikZ code:

  1. execute at end picture, see here for an original post on this.
  2. remember picture.

Both allow you to draw stuff, and to access nodes from the diagram. You may use either alias or \tikzcdmatrixname-<row>-<column>, which is the predefined node name.

\documentclass{article}
\usepackage{tikz-cd}
\begin{document}
\subsection*{Method 1: \texttt{execute at end picture}}
\begin{tikzcd}[execute at end picture={
\draw[-latex,red] ([yshift=1cm]A) to[out=45,in=170] ([yshift=1cm]C)
to[out=-10,in=45] (F);
\draw[blue] ([yshift=0.5em]D.north west) to[out=20,in=180] 
([yshift=1.5em]E.north) to[out=0,in=160] ([yshift=-0.5em]F.south east);}]
    & |[alias=A]|A \arrow[r]  & B\arrow[loop above] & |[alias=C]| C\arrow[l]\\
    |[alias=D]|D\arrow[r] & |[alias=E]| E\arrow[loop above] &  & |[alias=F]| F\arrow[ul]\\
     & G\arrow[u] & H\arrow[ul] & \\
     & J\arrow[u] & & \\
\end{tikzcd}

\subsection*{Method 2: \texttt{remember picture}}
\begin{tikzcd}[remember picture]
    & |[alias=A]|A \arrow[r]  & B\arrow[loop above] & |[alias=C]| C\arrow[l]\\
    |[alias=D]| D\arrow[r] & |[alias=E]| E\arrow[loop above] &  & |[alias=F]| F\arrow[ul]\\
     & G\arrow[u] & H\arrow[ul] & \\
     & J\arrow[u] & & \\
\end{tikzcd}

\begin{tikzpicture}[overlay,remember picture]
\draw[-latex,red] ([yshift=1cm]A) to[out=45,in=170] ([yshift=1cm]C)
to[out=-10,in=45] (F);
\draw[blue] ([yshift=0.5em]D.north west) to[out=20,in=180] 
([yshift=1.5em]E.north) to[out=0,in=160] ([yshift=-0.5em]F.south east);
\end{tikzpicture}
\end{document}

enter image description here

If you are more familiar with calc (instead of the yshifts) this will also work provided you load the library. As you can see, overlay has the slight disadvantage that the bounding box does not get adjusted, but may be argued to be less "hacky".

1
  • thanks. Can I draw a curve between, let's say at some unmarked point right above D and some unmarked point below F, so that two disconnected diagrams are separated by the curve? In tikz-picture, I could specify the coordinates of those points and draw a bent curve between the points. Can you do that on top of the matrix-based picture created by tikz-cd?
    – user25957
    Jun 6, 2019 at 4:08

You must log in to answer this question.

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