3

So I've drawn this diagram

enter image description here

 \begin{tikzcd}
 & & & 0 &  \\

0\arrow[r] & \ker{d^i}\arrow[d]\arrow[r] &
 A^i\arrow[d]\arrow[r] & \Ima{d^{i-1}}\arrow[r]\arrow[u] & 0\\

0 \arrow[r] &  H^i(A^*)\arrow[r] & A^i/ \Ima{d^{i-1}}\arrow[ur] & & 
\end{tikzcd}

I'd like to box the short exact sequence that is curved, in order to get something like this (which I made with Paint)

enter image description here

Is there any non-extremely complicated way to do this? I don't mind if the solution implies using tikzpicture or something instead of tikzcd.

Javi
  • 213

1 Answers1

2

It is certainly possible using the methods of this answer, i.e. by remembering the nodes and placing an overlay.

\documentclass{article}
\usepackage{tikz-cd}
\usepackage{amsmath}
\DeclareMathOperator*{\im}{Im}
\begin{document}
\[
\begin{tikzcd}[remember picture] % see https://tex.stackexchange.com/a/256562/121799
 & & & 0 &  \\
0\arrow[r] & \ker{d^i}\arrow[d]\arrow[r] &
 A^i\arrow[d]\arrow[r] & \im a{d^{i-1}}\arrow[r]\arrow[u] & 0\\
0 \arrow[r] &  H^i(A^*)\arrow[r] & A^i/ \im a{d^{i-1}}\arrow[ur] & & 
\end{tikzcd}
\]
\tikz[remember picture,overlay]{
\draw[-] (tikz@f@1-3-1.south west|-tikz@f@1-3-3.south east)
--(tikz@f@1-3-1.north west|-tikz@f@1-3-3.north)
--(tikz@f@1-3-3.north) --(tikz@f@1-2-4.west)
--(tikz@f@1-2-4.west|-tikz@f@1-1-4.north west)
--(tikz@f@1-2-4.south east|-tikz@f@1-1-4.north east)
--(tikz@f@1-2-4.south east)
--(tikz@f@1-3-3.south east)--cycle;
}
\end{document}

enter image description here

As you see, the contour tends to interfere with the arrows. One can cure this by adding some small shifts here and there. But I'd like to argue that an even better way of highlighting the contour is to shade it.

\documentclass{article}
\usepackage{tikz-cd}
\usepackage{amsmath}
\DeclareMathOperator{\Ima}{Im}
\begin{document}
\[
\begin{tikzcd}[remember picture] % see https://tex.stackexchange.com/a/256562/121799
 & & & 0 &  \\
0\arrow[r] & \ker{d^i}\arrow[d]\arrow[r] &
 A^i\arrow[d]\arrow[r] & \Ima a{d^{i-1}}\arrow[r]\arrow[u] & 0\\
0 \arrow[r] &  H^i(A^*)\arrow[r] & A^i/ \Ima a{d^{i-1}}\arrow[ur] & & 
\end{tikzcd}
\]
\tikz[remember picture,overlay]{
\fill[blue,opacity=0.2] (\tikzcdmatrixname-3-1.south west|-\tikzcdmatrixname-3-3.south east)
--(\tikzcdmatrixname-3-1.north west|-\tikzcdmatrixname-3-3.north)
--(\tikzcdmatrixname-3-3.north) --(\tikzcdmatrixname-2-4.west)
--(\tikzcdmatrixname-2-4.west|-\tikzcdmatrixname-1-4.north west)
--(\tikzcdmatrixname-2-4.south east|-\tikzcdmatrixname-1-4.north east)
--(\tikzcdmatrixname-2-4.south east)
--(\tikzcdmatrixname-3-3.south east)--cycle;
}

\[
\begin{tikzcd}[remember picture] % see https://tex.stackexchange.com/a/256562/121799
 & & & 0 &  \\
0\arrow[r] & \ker{d^i}\arrow[d]\arrow[r] &
 A^i\arrow[d]\arrow[r] & \Ima a{d^{i-1}}\arrow[r]\arrow[u] & 0\\
0 \arrow[r] &  H^i(A^*)\arrow[r] & A^i/ \Ima a{d^{i-1}}\arrow[ur] & & 
\end{tikzcd}
\]
\tikz[remember picture,overlay]{
\fill[red,opacity=0.2] (\tikzcdmatrixname-3-1.south west|-\tikzcdmatrixname-3-3.south east)
--(\tikzcdmatrixname-3-1.north west|-\tikzcdmatrixname-3-3.north)
--(\tikzcdmatrixname-3-3.north) --(\tikzcdmatrixname-2-4.west)
--(\tikzcdmatrixname-2-4.west|-\tikzcdmatrixname-1-4.north west)
--(\tikzcdmatrixname-2-4.south east|-\tikzcdmatrixname-1-4.north east)
--(\tikzcdmatrixname-2-4.south east)
--(\tikzcdmatrixname-3-3.south east)--cycle;
}
\end{document}

enter image description here

In this case, you do not have to mess around with these shifts.

NOTE: In the first snippet, the first number, 1, in tikz@f@1-3-3 (say) refers to the diagram. If you have more than one commutative diagrams (as in the second snippet), one needs to to take care that. I didn't find a documentation on that, but in tikzlibrarycd.code.tex one finds

\def\tikzcd@ar@target{\tikzcdmatrixname-\tikzcd@currentrow-\tikzcd@currentcolumn} 

And this seems to do the trick.

  • Hi, I know it's been a while, but I've recently tried to make this work and I get the error message 'Package pgf Error: No shape named tikz@f@1-3-1 is known'.

    I find this weird because my diagram has 3 rows. When I change the number 3 to 2 it doesn't produce any error but it draws something different in a different place (not even close to where my diagram is but in a corner of the page)

    – Javi Apr 19 '18 at 12:05
  • @Javi I do not get this error. Do you really get it when compiling one of the examples? (I'll also decouple now for a few hours.) –  Apr 19 '18 at 12:09
  • Yes, I get it with both examples indeed. I literally copy-pasted what you wrote. Just changed \im to my command \Ima. And I've got the packages you used. – Javi Apr 19 '18 at 12:18
  • @Javi Can you please tell me precisely what you changed? Does it run through if you do not change anything? (Redefining \im does not cause these issues unless you overwrite an existing command.) –  Apr 19 '18 at 15:09
  • @marmot I've got \DeclareMathOperator{\Ima}{Im} instead of \DeclareMathOperator*{\im}{Im} in my prelude. I use \documentclass[twoside]{article} instead of just article, which I think it won't cause any effects. Anything else is changed. – Javi Apr 19 '18 at 15:21
  • @Javi Please please check if my answer compiles on your system. –  Apr 19 '18 at 15:24
  • @marmot I've copied your exact code in a new file and it works after loading it twice (the first time the lines appear in a different place). But in my original file it gives the same error over and over even using your \im , so I guess I've got some interferences with something in my prelude – Javi Apr 19 '18 at 15:30
  • @Javi I'm not sure about the interference. Does your code run through if you comment out all the \tikz[remember picture,overlay]{...}? –  Apr 19 '18 at 15:31
  • @marmot yes it does, I get the original diagram – Javi Apr 19 '18 at 15:33