3

I want to draw an ellipse around three pairs of nodes in my diagram: (32), (78) and (56).

\begin{tikzcd}
1\arrow[bend right]{r} \arrow{d} 
&3 \arrow[bend right]{l} \arrow[bend left]{r} 
&2 \arrow[bend left]{l} \arrow[bend right]{r}  \arrow{d} 
&4   \arrow[bend right]{l} \\
7\arrow[bend right]{r}
&8 \arrow[bend right]{l} \arrow[bend left]{r}  \arrow{u}
&5 \arrow[bend left]{l} \arrow[bend right]{r} 
&6  \arrow[bend right]{l} \arrow{u} 
\end{tikzcd}

Here is a picture of the diagram that the above code outputs, with the dashed ellipses I would like superimposed on top:

the dream, ovals around certain numbers

Here is the best I have been able to do so far, using answers from elsewhere:

\[\begin{tikzcd}[execute at end picture={\node[ellipse,draw,blue,fit={(tikz@f@1-3-1) (tikz@f@1-3-2)}]{};}]
1\arrow[bend right]{r} \arrow{d} 
&3 \arrow[bend right]{l} \arrow[bend left]{r} 
&2 \arrow[bend left]{l} \arrow[bend right]{r}  \arrow{d} 
&4   \arrow[bend right]{l} \\
7\arrow[bend right]{r}
&8 \arrow[bend right]{l} \arrow[bend left]{r}  \arrow{u}
&5 \arrow[bend left]{l} \arrow[bend right]{r} 
&6  \arrow[bend right]{l} \arrow{u} 
\end{tikzcd}\]

with the output:

unsatisfactory ellipse location

I cannot figure out how to get the ellipse snugly around the pairs.

It seems that perhaps a different tikz would work better, like tikz picture, but all of the diagrams in my paper are written in the same way, so I would rather not rewrite this just to get ellipses around the nodes, because then it likely will not match.

Thank you!

2 Answers 2

4

Don't ignore error messages. Your code will throw the error

! Package pgf Error: No shape named tikz@f@1-3-1 is known.

Because the second number is the row number and third the column number. You have only two rows.

output of code

\documentclass[border=5mm]{standalone}
\usepackage{tikz-cd}
\usetikzlibrary{shapes.geometric,fit}
\tikzset{
   dashellipse/.style={ellipse,draw,dashed,inner sep=0pt,blue,fit={#1}}
}
\begin{document}
\begin{tikzcd}[
   execute at end picture={
     \node[dashellipse=(tikz@f@1-1-2)(tikz@f@1-1-3)]{};
     \node[dashellipse=(tikz@f@1-2-1)(tikz@f@1-2-2)]{};
     \node[dashellipse=(tikz@f@1-2-3)(tikz@f@1-2-4)]{};
}]
1 \arrow[bend right]{r} \arrow{d} 
 & 3 \arrow[bend right]{l} \arrow[bend left]{r} 
 & 2 \arrow[bend left]{l} \arrow[bend right]{r}  \arrow{d} 
 & 4 \arrow[bend right]{l} \\
7 \arrow[bend right]{r}
 & 8 \arrow[bend right]{l} \arrow[bend left]{r}  \arrow{u}
 & 5 \arrow[bend left]{l} \arrow[bend right]{r} 
 & 6 \arrow[bend right]{l} \arrow{u} 
\end{tikzcd}
\end{document}
4
  • Thank you very much. Unfortunately, it gives the error for me: "Package pgf Error: No shape named tikz@f@1-2-4 is known." Do you know how to fix this?
    – D. Davis
    Oct 9, 2017 at 16:05
  • @D.Davis Does that exact code give you an error? It works fine here, with an up to date TeX Live 2017. Which version of TikZ and tikz-cd do you have? (Add \listfiles before \documentclass, compile, and look at the *File list* near the end of the .log file to find out.) Oct 9, 2017 at 16:10
  • Is it possible to use this to draw ellipses around 3 or 4 nodes? Jun 13, 2018 at 16:32
  • 1
    @MathStudent020 Sure, just add more nodes to the list, e.g. \node[dashellipse=(tikz@f@1-1-2)(tikz@f@1-1-3)(tikz@f@1-1-4)]{}; Jun 13, 2018 at 17:42
3

You're almost there:

\documentclass{article}
\usepackage{tikz-cd}
\usetikzlibrary{fit,shapes.geometric}
\tikzset{mynode/.style={
    ellipse,draw,dashed,
    inner xsep=-1pt, blue,
    }}
\begin{document}
\[
\begin{tikzcd}[execute at end picture={
    \node[mynode, fit={(\tikzcdmatrixname-1-2) (\tikzcdmatrixname-1-3)}]{};
    \node[mynode, fit={(\tikzcdmatrixname-2-1) (\tikzcdmatrixname-2-2)}]{};
    \node[mynode, fit={(\tikzcdmatrixname-2-3) (\tikzcdmatrixname-2-4)}]{};
    }]
1\ar[bend right]{r} \ar{d} 
&3 \ar[bend right]{l} \ar[bend left]{r} 
&2 \ar[bend left]{l} \ar[bend right]{r} \ar{d} 
&4 \ar[bend right]{l} \\
7\ar[bend right]{r}
&8 \ar[bend right]{l} \ar[bend left]{r} \ar{u}
&5 \ar[bend left]{l} \ar[bend right]{r} 
&6 \ar[bend right]{l} \ar{u}  
\end{tikzcd}
\]
\end{document}

enter image description here

P.S. = Torbjørn T. and I answered at the same moment... He explained the error but I leave my answer because with inner xsep=-1pt the ellipses look better.

5
  • Thank you very much. Unfortunately, it gives the error for me: "Package pgf Error: No shape named tikz@f@1-2-4 is known."
    – D. Davis
    Oct 9, 2017 at 16:01
  • @D.Davis Is the matrix the same of my mwe? Or have you used yours?
    – CarLaTeX
    Oct 9, 2017 at 16:06
  • @D.Davis Or have you some node empty? That error occurs: if you have only 1 row, or 3 columns, or the node at the 2nd row and 4th column is empty.
    – CarLaTeX
    Oct 9, 2017 at 16:11
  • Stumbled onto this solution while working on a different problem. The identifiers tikz@f@n-r-c should probably be replaced with \tikzcdmatrixname-r-c in case the diagram is not actually the first diagram in the document. This may be the cause of the error OP was getting. The fix comes from this answer by @Henri Menke.
    – Sandy G
    May 23, 2022 at 22:47
  • @SandyG Corrected, thanks!
    – CarLaTeX
    May 24, 2022 at 4:55

You must log in to answer this question.

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