Draw an ellipse in Tikz-cd around diagram

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:

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:

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!

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.

\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}

• 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 '17 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.) – Torbjørn T. Oct 9 '17 at 16:10
• Is it possible to use this to draw ellipses around 3 or 4 nodes? – Math Student 020 Jun 13 '18 at 16:32
• @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)]{}; – Torbjørn T. Jun 13 '18 at 17:42

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={(tikz@f@1-1-2) (tikz@f@1-1-3)}]{}; \node[mynode, fit={(tikz@f@1-2-1) (tikz@f@1-2-2)}]{}; \node[mynode, fit={(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}


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.

• 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 '17 at 16:01
• @D.Davis Is the matrix the same of my mwe? Or have you used yours? – CarLaTeX Oct 9 '17 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 '17 at 16:11