问题陈述:
我想在 tikz-cd 图周围放置一个框,而无需使用 & 符号替换。使用 \fbox 似乎需要 & 符号替换。
示范:
以下给出一个错误:
\documentclass{article}
\usepackage{tikz-cd}
\begin{document}
\[
\fbox{
\begin{tikzcd} %error: must use ampersand replacement
A \ar[r, "f"] & B
\end{tikzcd}
}
\]
\end{document}
以下工作:
\documentclass{article}
\usepackage{tikz-cd}
\begin{document}
\[
\fbox{
\begin{tikzcd}[ampersand replacement = \&]
A \ar[r, "f"] \& B
\end{tikzcd}
}
\]
\end{document}
抱怨:
每次我想要一个盒子时都必须使用 & 符号替换,这很烦人!
问题:有没有其他方法可以在 tikz-cd 图周围绘制一个不会引起“&”符号问题的框?更一般地说,有没有办法将 tikzcd 图作为节点嵌入到 tikz 图片中?
谢谢!
答案1
您可以尝试以下操作:
\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}
您可以调整inner sep
和/或添加选项,mybox
例如thick
、、等,以适应不同风格的框。例如,rounded corners
fill=
draw=
[mybox/.style={draw=blue, fill=yellow!50, very thick, rounded corners,
inner sep=5pt}]
将产生
答案2
环境tikz-cd
实际上是一个tikzpicture
内部环境matrix
,它本身就是一个节点,您可以附加一个样式并告诉tikz
绘制它的边框。
此外,出于同样的原因(tikz-cd
环境是 a tikzpicture
),请小心将其插入另一个中tikzpicture
(tikzpicture
不建议嵌套 s )。您可以改用execute at end picture
或remember picture
和overlay
。
如果您想对多个交换图使用相同的格式,您可以创建一种样式,可能还带有一些参数。
\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}
答案3
答案4
如果您对标准感到满意\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}