Box around tikz-cd diagram, without ampersand replacement

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


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


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