# Tikzcd sloped label on a vertical arrow: how do I separate the label from the arrow?

I'm trying to use the sloped option for arrow labels in the tikzcd package. With this option, the label winds up adjacent to the arrow most of the time, except when the arrow is vertical:

\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}


Notice how for horizontal arrows, the placement of the "sloped" label f and ordinary label g is the same. But when the arrow is not horizontal, the placement differs, and in particular, when the arrow is vertical, the "sloped" label f winds up on top of the arrow and significantly offset downwards, rather than adjacent to the arrow.

Question: I want to obtain an output for a vertical arrow where the label is sloped in alignment with the arrow, but placed adjacent to the arrow rather than on top of it. How do I achieve this? I would prefer not to have to use something like rotatebox, since then I would have to adjust everything if, say, I add something to the diagram and now the previously-vertical arrow is pointing diagonally.

I tried adjusting the placement using the inner sep key, but that seems to adjust the downward offset without moving the label laterally:

\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}


All but the vertical arrows can be taken care of by using auto. "By adding the option auto, TikZ will position nodes on curves and lines in such a way that they are not on the curve but next to it." (p. 54 of the pgfmanual). The vertical ones need to be treated separately, it seems.

\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}


• I thought this was the default. Why does it normally do this except for sloped vertical arrows? Also any idea why the placement along the arrow for diagonal lines looks messed up? – Hood Chatham Jun 28 '18 at 20:45
• @HoodChatham Why messed up? They are placed in such a way that they do not interfere. Is there anything wrong with that. Notice also that I used swap instead of the xshift and I did upvote your answer. – user121799 Jun 28 '18 at 20:48
• Your answer is better (though I think xshift and yshift are important to know about). I'm just confused by why "auto" does anything by itself -- I thought it was the default behavior. – Hood Chatham Jun 28 '18 at 20:53
• There's nothing really wrong with the diagonal placement, I just don't like the fact that they aren't symmetric about the edge they are placed along. – Hood Chatham Jun 28 '18 at 20:54

Use xshift and yshift. You might also want to adjust the position of the diagonal labels using pos which controls the placement of the label along the edge. This isn't perfect but looks decent.

\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}


• Thanks, Hood! I suppose this works, although it would require manual adjustment if I were to change the arrow direction. I just noticed that putting in "anchor=south" seems to solve my problem too, but I don't understand why. Do you know what the "anchor" key does and how it interacts with "sloped"? – tcamps Jun 28 '18 at 20:38
• Oh yeah that is better. Still looks to me like the diagonal arrows could use pos to be manually adjusted. The anchor key adjusts which point on the boundary of the text is used to position the text. So roughly what happens is that tikz decides calculates the coordinates to place the box and then puts the anchor point there. – Hood Chatham Jun 28 '18 at 20:43

I don't fully understand this, but here's a solution I stumbled across shortly after posting the question: use the anchor key. It appears that if you set anchor=south, then the placement of the label winds up pretty much in the same place as when the label is unsloped, except that sometimes it looks like swap was applied or something. For vertical arrows, this fixes both the issue of the label being on top of the arrow, and the extra offset.

\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}


My best guess is that the sloped key looks to the anchor key to figure out where to rotate around. But a little experimentation did not reveal a value for the anchor key which reproduced the default sloped label placement, so who knows.

• Even though I really appreciate your efforts to figure that out I am wondering if this is really meant to be an answer, or may better become an appendix of your question? – user121799 Jun 28 '18 at 21:13
• I mean, it's an acceptable solution to the problem. In fact, personally it's my favorite solution. So I think it qualifies as an answer. If it's a big faux pas to answer my own question around here, I can make it community wiki. – tcamps Jun 28 '18 at 21:14
• Answering to your own question is allowed and welcome, but it should be clearly an answer, not another question. Maybe @marmot's doubt comes out from it. – CarLaTeX Jun 29 '18 at 4:25