5

Using tikz with tikzcd the:

  • label on the arrow are not centered, see those that sloped.
  • When arrow split ans come back together the distance between the end points are not equal.

How do you solve these?

\documentclass[preview]{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usetikzlibrary{cd}
\begin{document}

\begin{tikzcd}[column sep=large, row sep=large]
    &&\arrow[rr,"decrement"]&& 
    \arrow[rd,sloped, midway,"return"]&&\\

    \text{Start}\arrow[r,"f(x)"]&
    \ \arrow[ur,"x > 0",sloped,description]\arrow[rd,"x\leq0",sloped,above]
    &&&\    &\arrow[r]&\text{End}\\

    &&\arrow[rr,"increment"]    && \arrow[sloped]{ru}{return}&&
\end{tikzcd}
\end{document}

The output is: enter image description here

6

I'm not sure why, but labels on sloped arrows seem to be anchored at the arrow's center with one of their borders.

You can manually move the label along the arrow, but for this particular case there si s slicker trick, that also allows to easily avoid the wrong typesetting of labels with math italic.

\documentclass{standalone}
\usepackage{amsmath,mathtools}
\usepackage{tikz-cd}

\newcommand{\ctext}[1]{\text{\makebox[0pt]{#1}}}

\begin{document}

\begin{tikzcd}[column sep=large, row sep=large]
  && \arrow[rr,"\ctext{decrement}"]
  && \arrow[rd,sloped, midway,"\ctext{return}"]
  &&
\\
  \text{Start}\arrow[r,"f(x)"]
  & \arrow[ur,"\ctext{$x > 0$}",sloped,description]
    \arrow[rd,"\ctext{$x\leq0$}",sloped,above]
  &&&&
  \arrow[r]
  &\text{End}
\\
  && \arrow[rr,"\ctext{increment}"]
  && \arrow[ru,sloped,"\ctext{return}"]
  &&
\end{tikzcd}
\end{document}

enter image description here

  • 1
    slicker trick :-); where the the 'return' arrows come together the one looks longer than the other, can any thing be done about it? – Johan Oct 26 '18 at 7:50
  • 1
    @Johan \arrow[rd,"\ctext{$x\leq0$}",sloped,above, start anchor=center, shorten <= .5em] – LaRiFaRi Oct 26 '18 at 8:25
  • 1
    @Johan or add an \vphantom{X} to any cell you want to leave free. – LaRiFaRi Oct 26 '18 at 8:29
3
\documentclass[preview]{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usetikzlibrary{cd}
\begin{document}

\begin{tikzcd}[column sep=large, row sep=large]
&&\arrow[rr,"decrement"]&& \arrow[rd,sloped, midway,"return"]&&\\
\text{Start}\arrow[r,"f(x)"]&\ \arrow[ur,sloped,description,"x > 0"near end]\arrow[rd,"x\leq0"near start,sloped,above]&&&\    &\arrow[r]&\text{End}\\
&&\arrow[rr,"increment"]    && \arrow[sloped]{ru}{return}&&
\end{tikzcd}
\end{document}

Here is the code with the modification.

    \documentclass[preview]{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usetikzlibrary{cd}
\begin{document}

\begin{tikzcd}[column sep=large, row sep=large]
&&\arrow[rr,"decrement"]&& \arrow[rd,sloped, midway,"return"near start]&&\\
\text{Start}\arrow[r,"f(x)"]&\ \arrow[ur,sloped,description,"x > 0 "near end]\arrow[rd,"x\leq0"near start,sloped,above]&&&\    &\arrow[r]&\text{End}\\
&&\arrow[rr,"increment"]    && \arrow[ru,sloped, midway,"return"near end]&&
\end{tikzcd}
\end{document}
  • 1
    Almost perfect, you forgot to center the "return" text – AndréC Oct 26 '18 at 8:19

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.