在 TikZ 中缩放 tikz-cd 交换图?

在 TikZ 中缩放 tikz-cd 交换图?

我希望能够使用密钥缩放一些相当复杂的 CD scale,例如scale=0.5。在下面的 MWE 中,scale密钥似乎不起作用。

\documentclass{amsart}

\usepackage{tikz}
\usepackage{tikz-cd}

\usetikzlibrary{matrix}

\begin{document}

%% Uncomment/comment appropriately:
%\begin{tikzpicture}[column sep=1in,row sep=1in,scale=0.5in]
\begin{tikzpicture}[column sep=1in,row sep=1in]
    \matrix (A) [matrix of math nodes]
    {
        A& B\\
        C& D\\
    };
    \draw[->] (A-1-1) -- (A-1-2);
    \draw[->] (A-1-1) -- (A-2-1);
    \draw[->] (A-2-1) -- (A-2-2);
    \draw[->] (A-2-2) -- (A-1-2);
\end{tikzpicture}

%% Uncommenting the scale option has no effect:
\begin{tikzcd}%[scale=0.5]
    A\ar{r}\ar{d}&B\\
    C\ar{r}&D\ar{u}\\
\end{tikzcd}

\end{document}

这些都是非常简单的例子,我可以轻松地改变适当的尺寸,但我正在处理的 CD 很复杂并且经过精心排列——如果可能的话,我宁愿能够缩放它们。

答案1

似乎tikzcd环境不接受 scale 参数。由于您正在加载tikz,因此您可以放入tikzcd一个节点,并且该节点接受一个scale参数。(我承认这是一种过度杀伤/丑陋的黑客行为,但有时事情就是这样的)

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

\begin{document}

\begin{tikzpicture}[baseline= (a).base]
\node[scale=.5] (a) at (0,0){
\begin{tikzcd}
    A\ar{r}\ar{d}&B\\
    C\ar{r}&D\ar{u}\\
\end{tikzcd}
};
\end{tikzpicture}
\begin{tikzpicture}[baseline= (a).base]
\node[scale=1] (a) at (0,0){
\begin{tikzcd}
    A\ar{r}\ar{d}&B\\
    C\ar{r}&D\ar{u}\\
\end{tikzcd}
};
\end{tikzpicture}
\begin{tikzpicture}[baseline= (a).base]
\node[scale=1.5] (a) at (0,0){
\begin{tikzcd}
    A\ar{r}\ar{d}&B\\
    C\ar{r}&D\ar{u}\\
\end{tikzcd}
};
\end{tikzpicture}
\begin{tikzpicture}[baseline= (a).base]
\node[scale=2] (a) at (0,0){
\begin{tikzcd}
    A\ar{r}\ar{d}&B\\
    C\ar{r}&D\ar{u}\\
\end{tikzcd}
};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

scolumn seprow seps 的可扩展性与 TikZ 中的许多其他事物不同。

但是,您可以安装一些矩阵缩放键(通过在row sepcolumn sep因子中安装比例因子来实现)。

还有其他方法,但我不会扩展节点,甚至不会使用低级扩展(这样\scalebox更好)。

代码

\documentclass[varwidth, border={0pt 100pt 0pt 0pt}]{standalone}
\usepackage{tikz-cd}
\makeatletter
\tikzset{
  column sep/.code=\def\pgfmatrixcolumnsep{\pgf@matrix@xscale*(#1)},
  row sep/.code   =\def\pgfmatrixrowsep{\pgf@matrix@yscale*(#1)},
  matrix xscale/.code=%
    \pgfmathsetmacro\pgf@matrix@xscale{\pgf@matrix@xscale*(#1)},
  matrix yscale/.code=%
    \pgfmathsetmacro\pgf@matrix@yscale{\pgf@matrix@yscale*(#1)},
  matrix scale/.style={/tikz/matrix xscale={#1},/tikz/matrix yscale={#1}}}
\def\pgf@matrix@xscale{1}
\def\pgf@matrix@yscale{1}
\makeatother
\begin{document}
Normal:\par
\begin{tikzcd}
    A \ar{r}\ar{d} & B        \\
    C \ar{r}       & D \ar{u}
\end{tikzcd}\bigskip

With \texttt{tikzcd} options (one of \texttt{tiny}, \texttt{small},
  \texttt{scriptsize}, \texttt{normal}, \texttt{large} and \texttt{huge}):\par
\begin{tikzcd}[row sep=large, column sep=small]
    A \ar{r}\ar{d} & B        \\
    C \ar{r}       & D \ar{u}
\end{tikzcd}\bigskip

Sep scaling:\par
\begin{tikzcd}[matrix scale=1.5]
    A \ar{r}\ar{d} & B        \\
    C \ar{r}       & D \ar{u}
\end{tikzcd}\bigskip

Sep scaling, with nodes:\par
\begin{tikzcd}[matrix scale=1.5, transform shape, nodes={scale=1.5}]
    A \ar{r}\ar{d} & B        \\
    C \ar{r}       & D \ar{u}
\end{tikzcd}\bigskip

Everything, including line widths (does not update the bounding box correctly):\par\centering
\begin{tikzcd}[transform canvas={scale=4}]
    A \ar{r}\ar{d} & B        \\
    C \ar{r}       & D \ar{u}
\end{tikzcd}\bigskip
\end{document}

输出

在此处输入图片描述

答案3

这个\scalebox命令对你有用吗?[但说实话,我不知道为什么ampersand replacement=\&需要这个命令]

\documentclass{amsart}

\usepackage{tikz}
\usepackage{tikz-cd}

\usetikzlibrary{matrix}

\begin{document}

\begin{tikzpicture}[column sep=1in,row sep=1in]
    \matrix (A) [matrix of math nodes]
    {
        A& B\\
        C& D\\
    };
    \draw[->] (A-1-1) -- (A-1-2);
    \draw[->] (A-1-1) -- (A-2-1);
    \draw[->] (A-2-1) -- (A-2-2);
    \draw[->] (A-2-2) -- (A-1-2);
\end{tikzpicture}

\scalebox{.5}{
    \begin{tikzpicture}[column sep=1in,row sep=1in,ampersand replacement=\&]
        \matrix (A) [matrix of math nodes]{
            A\& B\\
            C\& D\\
        };
        \draw[->] (A-1-1) -- (A-1-2);
        \draw[->] (A-1-1) -- (A-2-1);
        \draw[->] (A-2-1) -- (A-2-2);
        \draw[->] (A-2-2) -- (A-1-2);
    \end{tikzpicture}
}

\end{document}

相关内容