10

Problem statement:

I'd like to put a box around a tikz-cd diagram, without needing to use ampersand replacement. Using \fbox seems to require ampersand replacement.

Demonstration:

The following gives an error:

\documentclass{article}
\usepackage{tikz-cd}

\begin{document}

\[
\fbox{
  \begin{tikzcd}       %error: must use ampersand replacement
    A \ar[r, "f"] & B
  \end{tikzcd}
}
\]

\end{document}

The following works:

\documentclass{article}
\usepackage{tikz-cd}

\begin{document}

\[
\fbox{
  \begin{tikzcd}[ampersand replacement = \&]
    A \ar[r, "f"] \& B
  \end{tikzcd}
}
\]

\end{document}

Complaint:

It is annoying to have to use ampersand replacement every time I want a box!

Question: Is there another way to draw a box around a tikz-cd diagram that does not cause the ampersand problem? More generally, is there a way to embed a tikzcd diagram as a node in a tikz picture?

Thanks!

David Spivak
  • 358
  • 1
  • 9

4 Answers4

9

You can try the following:

\documentclass{article}
\usepackage{tikz-cd}

\begin{document}

\begin{tikzpicture}[mybox/.style={draw, inner sep=5pt}]
\node[mybox] (box){%
  \begin{tikzcd}
    A \ar[r, "f"] & B
  \end{tikzcd}
};
\end{tikzpicture}

\end{document}

enter image description here

You can adjust the inner sep in mybox and/or add options such as thick, rounded corners, fill=, draw= etc. for different style boxes. For example,

[mybox/.style={draw=blue, fill=yellow!50, very thick, rounded corners,
    inner sep=5pt}]

will produce

enter image description here

Sandy G
  • 42,558
7

The tikz-cd environment actually is a tikzpicture with a matrix inside, which it's itself a node, you could append a style and tell tikz to draw its border.

Moreover, for the same reason (tikz-cd environment is a tikzpicture), be careful to inserting it in another tikzpicture (nesting tikzpictures is not recommended). You could use execute at end picture or remember picture and overlay instead.

If you want to use the same formatting for more than one commutative diagram of yours, you could create a style, possibly with some parameters.

\documentclass{article} 
\usepackage{tikz-cd} 
\tikzcdset{
    boxedcd/.style={
        every matrix/.append style={
            draw=red,
            thick,
            fill=yellow!50!white,
            rounded corners,
            #1
        },
    },
}

\begin{document} 
    \[ 
    \begin{tikzcd}[every matrix/.append style={draw, inner ysep=4pt}]
        A \ar[r, "f"] & B 
    \end{tikzcd} 
    \] 
    Of course, you can set the node as you like:
    \[ 
    \begin{tikzcd}[every matrix/.append style={draw=red, 
        fill=yellow, inner ysep=4pt, rounded corners}]
        A \ar[r, "f"] & B 
    \end{tikzcd} 
    \]
    Example of \texttt{execute at end picture}:
    \[ 
    \begin{tikzcd}[every matrix/.append style={name=mymatr},
        execute at end picture={
            \draw[red,rounded corners] (mymatr.south west) -- (mymatr.south east) -- ([yshift=4pt]mymatr.north east) -- ([yshift=4pt]mymatr.north west) -- cycle;
        }]
        A \ar[r, "f"] & B 
    \end{tikzcd} 
    \]
    Example of use of \texttt{overlay}:
    \[ 
    \begin{tikzcd}[remember picture,
        every matrix/.append style={name=mymatrix}]
        A \ar[r, "f"] & B 
    \end{tikzcd} 
    \]
    \begin{tikzpicture}[remember picture, overlay]
        \draw[blue,rounded corners] (mymatrix.south west) -- (mymatrix.south east) -- ([yshift=4pt]mymatrix.north east) -- ([yshift=4pt]mymatrix.north west) -- cycle; 
    \end{tikzpicture}

    \noindent Example of use of a \texttt{stlye}:
    \[
    \begin{tikzcd}[boxedcd={inner sep=1pt}]
        A \arrow[r] & B
    \end{tikzcd}\quad
    \begin{tikzcd}[boxedcd={inner ysep=4pt}]
        A \arrow[r, "f"] & B
    \end{tikzcd}\quad
    \begin{tikzcd}[boxedcd={inner xsep=4pt, inner ysep=2pt}]
        A \arrow[r] & B\\
        C \arrow[u, "g"]
    \end{tikzcd}
    \]
\end{document}

enter image description here

CarLaTeX
  • 62,716
4

Sandy G scooped me. Here is my proposal:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{cd}
\begin{document}
\[
  \begin{tabular}{|c|}
  \hline
  \begin{tikzcd}    
   A \ar[r, "f"] & B
  \end{tikzcd}\\
  \hline
  \end{tabular}
\]
\end{document}

enter image description here

3

If you're satisfied with the standard \fbox:

\documentclass{article}
\usepackage{tikz-cd}

\newenvironment{boxedtikzcd}
 {\begin{lrbox}{\boxedtikzcdbox}\begin{tikzcd}}
 {\end{tikzcd}\end{lrbox}\fbox{\usebox{\boxedtikzcdbox}}}
\newsavebox{\boxedtikzcdbox}

\begin{document}

\[
\begin{boxedtikzcd}
A \arrow[r, "f"] & B
\end{boxedtikzcd}
\]

\end{document}

enter image description here

egreg
  • 1,121,712