我正在尝试使用 绘制 (交换) 图tikz-cd
。包手册的第 3.2 节描述了如何使用to path
来细粒度控制箭头的形状,但很少提到如何为这些箭头添加标签。
我目前有下图:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{cd}
\begin{document}
\begin{tikzcd}
& K^n \arrow[r, "L^A_B(F)"] \arrow[dd, rounded corners, to path={ -- ([xshift=-10ex]\tikztostart.west) -- ([xshift=-10ex]\tikztotarget.west) -- (\tikztotarget.west)}] & K^m \arrow[dd, rounded corners, to path={ -- ([xshift=10ex, "L^B_D"]\tikztostart.east) -- ([xshift=10ex]\tikztotarget.east) -- (\tikztotarget.east)}] & \\
V \arrow[rrr, "F"] \arrow[ru, "C_A"] \arrow[rd, "C_C"'] & & & W \arrow[lu, "C_B"'] \arrow[ld, "C_D"] \\
& K^n \arrow[r, "L^C_D(F)"] & K^m &
\end{tikzcd}
\end{document}
生成结果:
我想添加标签L^A_C
和L^B_D
外部“弯曲”箭头,但我不知道该怎么做。显而易见的直接方法(向路径添加标签)不会产生任何结果,而添加幻影箭头来创建标签放置位置的坐标(如文档中所述tikz-cd
)会产生错误消息:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{cd}
\begin{document}
\begin{tikzcd}
\arrow[dd, phantom, ""{coordinate, name=Z} && K^n \arrow[r, "L^A_B(F)"] \arrow[dd, rounded corners, to path={ -- ([xshift=-10ex]\tikztostart.west) -- ([xshift=-10ex]\tikztotarget.west) -- (\tikztotarget.west)}] & K^m \arrow[dd, rounded corners, to path={ -- ([xshift=10ex, "L^B_D"]\tikztostart.east) -- ([xshift=10ex]\tikztotarget.east) -- (\tikztotarget.east)}] & \\
&V \arrow[rrr, "F"] \arrow[ru, "C_A"] \arrow[rd, "C_C"'] & & & W \arrow[lu, "C_B"'] \arrow[ld, "C_D"] \\
&& K^n \arrow[r, "L^C_D(F)"] & K^m &
\end{tikzcd}
\end{document}
产量
! Package pgf Error: No shape named `tikz@f@1-3-1' is known.
See the pgf package documentation for explanation.
Type H <return> for immediate help
...
l.6 I think the culprit is a tikzcd arrow in cell 1-1.
以及其他几个类似的错误。
答案1
您可以在路径的第二段添加一个节点。
\begin{tikzcd}
& K^n
\arrow[r, "L^A_B(F)"]
\arrow[dd, rounded corners,
to path={
-- ([xshift=-10ex]\tikztostart.west)
--node[left]{\scriptsize$L_C^A$} ([xshift=-10ex]\tikztotarget.west)
-- (\tikztotarget.west)}
]
& K^m
\arrow[dd, rounded corners,
to path={
-- ([xshift=10ex, "L^B_D"]\tikztostart.east)
--node[right]{\scriptsize$L_D^B$} ([xshift=10ex]\tikztotarget.east)
-- (\tikztotarget.east)}]
& \\
V
\arrow[rrr, "F"]
\arrow[ru, "C_A"]
\arrow[rd, "C_C"']
& & & W
\arrow[lu, "C_B"']
\arrow[ld, "C_D"] \\
& K^n
\arrow[r, "L^C_D(F)"]
& K^m &
\end{tikzcd}
答案2
在“薛定谔的猫”的鼓励下,我制作了一个通用钥匙来实现这一点。我把它称为“中继箭头”,但如果有人有更好的名字,请告诉我。它的工作原理是说,例如\ar[ur, relay arrow=10pt]
。
严格来说,这与所问的问题无关,但我希望它可能对某些人有用。
代码如下:
\documentclass{article}
\usepackage{tikz-cd}
\tikzcdset{
relay arrow/.default = 10pt,
relay arrow/.style = {
rounded corners,
to path = {
\pgfextra{
\def\sourcecoordinate{\pgfpointanchor{\tikztostart}{center}}
\def\targetcoordinate{\pgfpointanchor{\tikztotarget}{center}}
\pgfmathanglebetweenpoints{\sourcecoordinate}{\targetcoordinate}
\edef\tempangle{\pgfmathresult}
\pgftransformrotate{\tempangle}
\pgfmathifthenelse{#1>0}{\tempangle+90}{\tempangle-90}
\pgfcoordinate{tempcoord}{\pgfpointanchor{\tikztostart}{\pgfmathresult}}
}
(tempcoord)
-- ([yshift=#1]tempcoord)
-- ([yshift=#1]tempcoord-|\tikztotarget.center)\tikztonodes
--(\tikztotarget)
}
}
}
\begin{document}
\begin{tikzcd}
& B\\
A \urar[relay arrow,"1"]\urar["2"]\urar[relay arrow=-25pt,"3"']&
\end{tikzcd}
\begin{tikzcd}
&& K^n \arrow[r, "L^A_B(F)"]
\arrow[dd, "L_A^C"', relay arrow=-10ex]
& K^m
\arrow[dd,"L_D^B", relay arrow=10ex] &
\\
&V \arrow[rrr, "F"] \arrow[ru, "C_A"] \arrow[rd, "C_C"'] & & & W \arrow[lu, "C_B"'] \arrow[ld, "C_D"] \\
&& K^n \arrow[r, "L^C_D(F)"] & K^m &
\end{tikzcd}
\end{document}
答案3
另一个选项是使用\tikztonodes
in to path
,然后使用引号指定标签。使用引号指定的标签将存储在 中\tikztonodes
。如果您想使用相同的“tikz to path”绘制多个路径,这会更有用。
\documentclass{article}
\usepackage{tikz-cd}
\begin{document}
\begin{tikzcd}
&& K^n \arrow[r, "L^A_B(F)"]
\arrow[dd, rounded corners, "L_A^C"',
to path={
-- ([xshift=-10ex]\tikztostart.west)
-- ([xshift=-10ex]\tikztotarget.west)\tikztonodes % added \tikztonodes here
-- (\tikztotarget.west)
}]
& K^m
\arrow[dd,"L_D^B", rounded corners,
to path={
-- ([xshift=10ex]\tikztostart.east)
-- ([xshift=10ex]\tikztotarget.east)\tikztonodes
-- (\tikztotarget.east)
}] &
\\
&V \arrow[rrr, "F"] \arrow[ru, "C_A"] \arrow[rd, "C_C"'] & & & W \arrow[lu, "C_B"'] \arrow[ld, "C_D"] \\
&& K^n \arrow[r, "L^C_D(F)"] & K^m &
\end{tikzcd}
\end{document}
答案4
这使用了普通的 TikZ。pos=.25
是由于操作++
更新了计算的新位置。
\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}
\def\a{2.5} \def\b{1.3}
\path
(0,0) node[above]{$F$}
(\a,0) node (W) {$W$}
(-\a,0) node (V) {$V$}
(.4*\a,\b) node (Kmt) {$K^m$}
(-.4*\a,\b) node (Knt) {$K^n$}
(.4*\a,-\b) node (Kmb) {$K^m$}
(-.4*\a,-\b) node (Knb) {$K^n$};
\begin{scope}[every node/.style={midway,scale=.7}]
\draw[->] (V)--(W);
\draw[->] (Knt)--(Kmt) node[above]{$L_B^A(F)$};
\draw[->] (Knb)--(Kmb) node[below]{$L_D^C(F)$};
\draw[->] (V)--(Knt) node[above left]{$C_A$};
\draw[->] (W)--(Kmt) node[above right]{$C_B$};
\draw[->] (V)--(Knb) node[below left]{$C_C$};
\draw[->] (W)--(Kmb) node[below right]{$C_D$};
\draw[->,rounded corners] (Kmt)--++(0:.8*\a)|-(Kmb) node[pos=.25,right]{$L_D^B(F)$};
\draw[->,rounded corners] (Knt)--++(180:.8*\a)|-(Knb) node[pos=.25,left]{$L_A^C(F)$};
\end{scope}
\end{tikzpicture}
\end{document}