Tikzcd 垂直箭头上的倾斜标签:如何将标签与箭头分开?

Tikzcd 垂直箭头上的倾斜标签:如何将标签与箭头分开?

我正在尝试使用包sloped中的箭头标签选项tikzcd。使用此选项,标签大多数时候都会与箭头相邻,除非箭头垂直:

\documentclass{standalone}
\usepackage{tikz-cd}
\begin{document}
\begin{tikzcd}
UL & U & UR \\
L & C
\ar[r,"f"{sloped},"g"]
\ar[dr,"f"{sloped},"g"]
\ar[d,"f"{sloped},"g"]
\ar[dl,"f"{sloped},"g"]
\ar[l,"f"{sloped},"g"]
\ar[ul,"f"{sloped},"g"]
\ar[u,"f"{sloped},"g"]
\ar[ur,"f"{sloped},"g"]
& R \\
DL & D & DR
\end{tikzcd}
\end{document}

在此处输入图片描述

请注意,对于水平箭头,“倾斜”标签f和普通标签的放置g位置是相同的。但是当箭头不是水平时,放置位置会有所不同,特别是当箭头垂直时,“倾斜”标签f会位于箭头顶部并明显向下偏移,而不是与箭头相邻。


问题:我想获得垂直箭头的输出,其中标签与箭头对齐倾斜,但放置在箭头旁边而不是箭头上方。我该如何实现?我宁愿不要使用类似的东西rotatebox,因为那样的话,如果我向图表中添加了一些东西,现在之前的垂直箭头指向对角线,我就必须调整所有内容。


我尝试使用按键调整位置inner sep,但这似乎只能调整向下的偏移量而不能横向移动标签:

\documentclass{standalone}
\usepackage{tikz-cd}
\begin{document}
\begin{tikzcd}
UL & U & UR \\
L & C
\ar[r,"f"{sloped},"g"{sloped,inner sep=-2pt}]
\ar[dr,"f"{sloped},"g"{sloped,inner sep=-2pt}]
\ar[d,"f"{sloped},"g"{sloped,inner sep=-2pt}]
\ar[dl,"f"{sloped},"g"{sloped,inner sep=-2pt}]
\ar[l,"f"{sloped},"g"{sloped,inner sep=-2pt}]
\ar[ul,"f"{sloped},"g"{sloped,inner sep=-2pt}]
\ar[u,"f"{sloped},"g"{sloped,inner sep=-2pt}]
\ar[ur,"f"{sloped},"g"{sloped,inner sep=-2pt}]
& R \\
DL & D & DR
\end{tikzcd}
\end{document}

在此处输入图片描述

答案1

除了垂直箭头之外,其他所有箭头都可以使用 来处理auto。通过添加选项 auto,TiZ 将以这样一种方式定位曲线和直线上的节点,即它们不在曲线上,而是在曲线旁边。”(pgfmanual 第 54 页)。垂直的节点似乎需要单独处理。

\documentclass{standalone}
\usepackage{tikz-cd}
\begin{document}
\begin{tikzcd}
UL & U & UR \\
L & C
\ar[r,"f"{sloped,auto},"g"{sloped,auto,swap}]
\ar[dr,"f"{sloped,auto},"g"{sloped,auto,swap}]
\ar[d,"f"{sloped,above},"g"{sloped,below}]
\ar[dl,"f"{sloped,auto},"g"{sloped,auto,swap}]
\ar[l,"f"{sloped,auto},"g"{sloped,auto,swap}]
\ar[ul,"f"{sloped,auto},"g"{sloped,auto,swap}]
\ar[u,"f"{sloped,above},"g"{sloped,below}]
\ar[ur,"f"{sloped,auto},"g"{sloped,auto,swap}]
& R \\
DL & D & DR
\end{tikzcd}
\end{document}

在此处输入图片描述

答案2

使用xshiftyshift。您可能还想使用 来调整对角线标签的位置,以pos控制标签沿边缘的位置。这并不完美,但看起来不错。

\documentclass{standalone}
\usepackage{tikz-cd}
\begin{document}
\begin{tikzcd}
UL & U & UR \\
L & C
\ar[r,"f"{sloped}]
\ar[dr,"f"{sloped, pos=0.4}]
\ar[d,"f"{sloped, yshift = 6pt, xshift=-5pt}]
\ar[dl,"f"{sloped,pos=0.6}]
\ar[l,"f"{sloped}]
\ar[ul,"f"{sloped, pos=0.4}]
\ar[u,"f"{sloped, yshift = 6pt, xshift=5pt}]
\ar[ur,"f"{sloped, pos=0.6}]
& R \\
DL & D & DR
\end{tikzcd}\end{document} 

在此处输入图片描述

答案3

我不完全理解这一点,但这是我在发布问题后不久偶然发现的解决方案:使用键anchor。看来,如果您设置anchor=south,则标签的位置与标签不倾斜时的位置几乎相同,只是有时看起来像是swap应用了什么的。对于垂直箭头,这既解决了标签位于箭头顶部的问题,也解决了额外的偏移问题。

\documentclass{standalone}
\usepackage{tikz-cd}
\begin{document}
\begin{tikzcd}
UL & U & UR \\
L & C
\ar[r,"f","g"{sloped,anchor=south}]
\ar[dr,"f","g"{sloped,anchor=south}]
\ar[d,"f","g"{sloped,anchor=south}]
\ar[dl,"f","g"{sloped,anchor=south}]
\ar[l,"f","g"{sloped,anchor=south}]
\ar[ul,"f","g"{sloped,anchor=south}]
\ar[u,"f","g"{sloped,anchor=south}]
\ar[ur,"f","g"{sloped,anchor=south}]
& R \\
DL & D & DR
\end{tikzcd}
\end{document}

我最好的猜测是,sloped键会查看anchor键来确定要旋转的位置。但是,经过一些实验,并没有发现anchor键的值可以重现默认sloped标签的位置,所以谁知道呢。

在此处输入图片描述

相关内容