5

I am having trouble with the label locations on my commutative diagram.

Here is my code:

\documentclass{article}

\usepackage{tikz-cd}

\begin{document}

\begin{tikzcd}[ampersand replacement = \&]
    0 \arrow[r, "N\lambda", bend left] \& 1 \arrow[r, "(N - 1)\lambda", bend left] \arrow[l, "\lambda", bend left] \& 2 \arrow[r, "(N - 2)\lambda", bend left] \arrow[l, "2\lambda", bend left] \& \cdots \arrow[r, "2\lambda", bend left] \arrow[l, "3\lambda", bend left] \& N-1 \arrow[r, "\lambda", bend left] \arrow[l, "(N-1)\lambda", bend left] \& N \arrow[l, "N\lambda", bend left]
\end{tikzcd}

\begin{tikzcd}[ampersand replacement = \&]
    0 \arrow[r, "N\lambda", bend left] \& 1 \arrow[r, "(N - 1)\lambda", bend left] \arrow[l, "\lambda", bend left] \& 2 \arrow[r, "(N - 2)\lambda", bend left] \arrow[l, "2\lambda", bend left] \& 3 \arrow[r, "2\lambda", bend left] \arrow[l, "3\lambda", bend left] \& 4 \arrow[r, "\lambda", bend left] \arrow[l, "(N-1)\lambda", bend left] \& 5 \arrow[l, "N\lambda", bend left]
\end{tikzcd}

\end{document}

which produces

enter image description here

I would like the arrow labels to appear "centered" on the arrows, as they are in the second diagram, but with the node labels from the first diagram. How can I fix this?

Note: I have already looked at the question here and know that I could probably manually correct the spacing on every single label, but that seems way more tedious than should be necessary given that I'm pretty sure the labels should be centered on the arrows by default.

5

What happens here? auto gets screwed up.

Why does it happen? See below.

Can one fix it? Yes, at least in an ad hoc way.

\documentclass{article}
\usepackage{tikz-cd}
\tikzcdset{ad hoc fix/.style={r'/.style={r,/tikz/above},l'/.style={l,/tikz/below},
every label/.append style={/tikz/auto=false}}}

\begin{document}
\begin{tikzcd}[ampersand replacement=\&,ad hoc fix]
    0 \arrow[r', "N\lambda", bend left] \& 
    1 \arrow[r', "(N - 1)\lambda", bend left] \arrow[l', "\lambda", bend left] \& 
    2 \arrow[r', "(N - 2)\lambda", bend left] \arrow[l', "2\lambda", bend left] \& 
    \cdots \arrow[r', "2\lambda", bend left] \arrow[l', "3\lambda", bend left] \& 
    N-1 \arrow[r', "\lambda", bend left] \arrow[l', "(N-1)\lambda", bend left] \& 
    N \arrow[l', "N\lambda", bend left]
\end{tikzcd}

\begin{tikzcd}[ampersand replacement = \&]
    0 \arrow[r, "N\lambda", bend left] \& 1 \arrow[r, "(N - 1)\lambda", bend left] \arrow[l, "\lambda", bend left] \& 2 \arrow[r, "(N - 2)\lambda", bend left] \arrow[l, "2\lambda", bend left] \& 3 \arrow[r, "2\lambda", bend left] \arrow[l, "3\lambda", bend left] \& 4 \arrow[r, "\lambda", bend left] \arrow[l, "(N-1)\lambda", bend left] \& 5 \arrow[l, "N\lambda", bend left]
\end{tikzcd}
\end{document}

enter image description here

The reason why this happens is that the arrows have different vertical positions of start and end points. This can be seen from

\documentclass{article}

\usepackage{tikz-cd}

\begin{document}

\begin{tikzcd}[ampersand replacement = \&,every label/.append style={sloped}]
    0 \arrow[r, "N\lambda", bend left] \& 
    1 \arrow[r, "(N - 1)\lambda", bend left] \arrow[l, "\lambda", bend left] \& 
    2 \arrow[r, "(N - 2)\lambda", bend left] \arrow[l, "2\lambda", bend left] \& 
    \cdots \arrow[r, "2\lambda", bend left] \arrow[l, "3\lambda", bend left] \& 
    N-1 \arrow[r,"\lambda", bend left] \arrow[l, "(N-1)\lambda", bend left] \& 
    N \arrow[l, "N\lambda", bend left]
\end{tikzcd}

\end{document}

enter image description here

5

Misplacement of arrow labels you can be reduce by increasing the bend angles. With this you slightly change the position of the start and end of the arrows for longer nodes' names. For example:

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

\begin{document}
    \begin{tikzcd}[sep=large,
                   every arrow/.style={draw,->,bend left=45}, % for shorter diagram code
                   ]
0 \ar[r, "N\lambda"]
    & 1 \ar[r, "(N - 1)\lambda"]
        \ar[l, "\lambda"]
        & 2 \ar[r, "(N - 2)\lambda"]
            \ar[l, "2\lambda"]
            & \cdots \ar[r, "2\lambda"]
                     \ar[l, "3\lambda"]
                & N-1 \ar[r, "\lambda"]
                      \ar[l, "(N-1)\lambda"]
                    & N \ar[l, "N\lambda"]
    \end{tikzcd}
\end{document}

enter image description here

  • The output is not produced when running your code. – marmot May 15 at 19:58
  • hm, I check the code again and obtain the same result as i show in answer. I use recent MikTeX (updated yesterday) on w10 machine. – Zarko May 15 at 20:15
  • @Åke, thank you very much ! – Zarko May 15 at 20:16
  • No. For instance, you have N-1-2 in your code. Are you saying MikTeX converts this to N-1? – marmot May 15 at 20:18
  • ups, @marmoth you are right. Thank you very much to pointed to this. I didn't notice this, I just looked position of arrows labels. i will correct this asap. this left from some previous test ... – Zarko May 15 at 20:20
1

Looks like a good application for tikz's chain library :

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{automata,chains,positioning}
\begin{document}

\begin{tikzpicture}[
auto,
start chain = going right, 
state/.append style = { 
    on chain,
    draw=none,
},
]
\node (s0)[state] {$0$};
\node (s1)[state] {$1$};
\node (s2)[state] {$2$};

\draw[->] (s0) edge[bend left] node {$N\lambda$} (s1)
          (s1) edge[bend left] node {$\lambda$} (s0)
          (s1) edge[bend left] node {$(N-1)\lambda$} (s2)
          (s2) edge[bend left] node {$2\lambda$} (s1)
          ;

\end{tikzpicture}
\end{document}          

enter image description here

New contributor
Åke is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.