我有两个问题。它们似乎足够相关,可以包含在同一个问题中。以下示例最容易描述这些问题。
如何将 tikz-cd 图居中,使其看起来更像图的真实中心?我尝试在标签之外添加幻影点,以便图表使用幻影点居中,但这似乎是临时的。
调整箭头长度以提供统一外观的最佳做法是什么?我知道可以使用 [sep=3em] 手动调整箭头长度。但似乎应该有更好的方法。
这是用于制作第一幅图像的最小工作示例。第二幅图像是在 Gimp 中编辑的。
\usepackage{tikz-cd}
\begin{document}
\noindent{Here is the first diagram.}
\[\begin{tikzcd}
A & B \\
C & D
\arrow[from=1-1, to=1-2]
\arrow[from=1-2, to=2-2]
\arrow[from=1-1, to=2-1]
\arrow[from=2-1, to=2-2]
\end{tikzcd}\]
Here is the second diagram. The long label throws off the centering.
\[\begin{tikzcd}
A & B \\
C & D
\arrow[from=1-1, to=1-2]
\arrow["{\text{long label}}", from=1-2, to=2-2]
\arrow[from=1-1, to=2-1]
\arrow[from=2-1, to=2-2]
\end{tikzcd}\]
Here is the third diagram. It looks too wide.
\[\begin{tikzcd}
{A+E} & {B+E} \\
{C+E} & {D+E}
\arrow[from=1-1, to=1-2]
\arrow[from=1-2, to=2-2]
\arrow[from=1-1, to=2-1]
\arrow[from=2-1, to=2-2]
\end{tikzcd}\]
\end{document}
答案1
标签long label
有助于 TikZ 图片的边界框(通常是你想要的),但是,选项overlay
您可以指示 TikZ 完全忽略边界框的标签。
由于您使用了\text
(来自amsmath
?),我还添加了一个no math
键,用于 a) 关闭该边缘标签的数学模式和 b) 将标签的字体大小设置为\scriptsize
(其中\scriptstyle
用于数学模式)。该no math
键需要用作箭头的选项,并且不是标签本身。
您还可以使用选项trim left
和trim right
(正如我在另一个答案中所做的那样),这样只有矩阵本身才能确定图表的水平大小。这是一个更通用的解决方案,但对于更宽更复杂的图表可能会有缺点?
TikZ-CD 使用的底层 TikZ 矩阵还支持列之间的距离与其原点(而不是边界)之间的距离相隔,这意味着您需要指定between origins
给出一个column sep
值时。不幸的是,默认值和 TikZ-CD 预定义的值不容易访问,但无论如何您都需要自行指定长度,因为这当然取决于您的所有图表和单元格的内容。我随机选择 4.6em,因为它看起来适合这种情况,但您当然可以更改默认值或columns from center=<distance>
手动使用。
如果这应该更自动地完成,则需要测量节点的宽度或需要用户指定最宽的节点并加以考虑,这是可能的,但有很多情况需要考虑(三列的图表应该是什么样子?)。
代码
\documentclass[varwidth,border=2mm]{standalone}
\usepackage{tikz-cd}
\tikzcdset{
center matrix/.style={
trim left=(\tikzcdmatrixname.west),
trim right=(\tikzcdmatrixname.east)},
columns from center/.default=4.6em,
columns from center/.style={column sep={#1,between origins}},
no math/.style={
every label/.append style={font=\scriptsize},
math mode=false}}
\begin{document}
Here is the first diagram.
\[\begin{tikzcd}[columns from center]
A & B \\
C & D
\arrow[from=1-1, to=1-2]
\arrow[from=1-2, to=2-2]
\arrow[from=1-1, to=2-1]
\arrow[from=2-1, to=2-2]
\end{tikzcd}\]
Here is the second diagram. The long label throws off the centering.
\textcolor{red}{→ Use \texttt{overlay} (and \texttt{no math}).}
\[\begin{tikzcd}[columns from center]
A & B \\
C & D
\arrow[from=1-1, to=1-2]
\arrow["long label" overlay, no math, from=1-2, to=2-2]
\arrow[from=1-1, to=2-1]
\arrow[from=2-1, to=2-2]
\end{tikzcd}\]
Here is the second diagram again. The long label throws off the centering.
\textcolor{red}{→ Use \texttt{center matrix} (and \texttt{no math}).}
\[\begin{tikzcd}[center matrix, columns from center]
A & B \\
C & D
\arrow[from=1-1, to=1-2]
\arrow["long label", no math, from=1-2, to=2-2]
\arrow[from=1-1, to=2-1]
\arrow[from=2-1, to=2-2]
\end{tikzcd}\]
Here is the third diagram. It looks too wide.
\textcolor{red}{→ Use \texttt{between origins} \emph{carefully} for all diagrams.}
\[\begin{tikzcd}[columns from center]
{A+E} & {B+E} \\
{C+E} & {D+E}
\arrow[from=1-1, to=1-2]
\arrow[from=1-2, to=2-2]
\arrow[from=1-1, to=2-1]
\arrow[from=2-1, to=2-2]
\end{tikzcd}\]
\end{document}
输出
答案2
对于第二张图中标签的问题,除了评论中所建议的方法之外,您还可以使用该选项text width=0pt
(您会收到警告,但这是需要的)或创建一个没有宽度的框\makebox[0pt][l]{$\text{long label}$}
。
要缩小第三张图,您可以使用 修改列之间的距离&[-18pt]
,也可以使用 减少节点的内部 x 空间[every cell/.append style={inner xsep=1pt}]
。
但是,我发现非常糟糕,最好保持原样。
\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz-cd}
\begin{document}
\noindent Here is the first diagram.
\[\begin{tikzcd}
A & B \\
C & D
\arrow[from=1-1, to=1-2]
\arrow[from=1-2, to=2-2]
\arrow[from=1-1, to=2-1]
\arrow[from=2-1, to=2-2]
\end{tikzcd}\]
For the problem of the label in the second diagram, you could also use the option \texttt{text width=0pt} or set a left aligned box of 0pt width:
\[\begin{tikzcd}
A & B \\
C & D
\arrow[from=1-1, to=1-2]
\arrow["{\text{long label}}"{text width=0pt}, from=1-2, to=2-2]
\arrow[from=1-1, to=2-1]
\arrow[from=2-1, to=2-2]
\end{tikzcd}\]
\[\begin{tikzcd}
A & B \\
C & D
\arrow[from=1-1, to=1-2]
\arrow["{\makebox[0pt][l]{$\text{long label}$}}", from=1-2, to=2-2]
\arrow[from=1-1, to=2-1]
\arrow[from=2-1, to=2-2]
\end{tikzcd}\]
To shrink the third diagram you can modify the distance between the columns with \texttt{\&[-18pt]} and also reduce the inner x-space of the nodes with \texttt{[every cell/.append style=\{inner xsep=1pt\}]}.
However, I find it \emph{very awful!} Much better to leave it as is.
\[\begin{tikzcd}[every cell/.append style={inner xsep=1pt}]
{A+E} &[-18pt] {B+E} \\
{C+E} & {D+E}
\arrow[from=1-1, to=1-2]
\arrow[from=1-2, to=2-2]
\arrow[from=1-1, to=2-1]
\arrow[from=2-1, to=2-2]
\end{tikzcd}\]
\end{document}