使用 tikz-cd 时标签不居中,箭头不汇合

使用 tikz-cd 时标签不居中,箭头不汇合

使用 tikz 和 tikzcd 进行:

  • 箭头上的标签不居中,请参见倾斜的标签。
  • 当箭分开并重新连接时,端点之间的距离并不相等。

你如何解决这些问题?

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

输出为: 在此处输入图片描述

答案1

我不知道为什么,但倾斜箭头上的标签似乎用其中一个边框固定在箭头的中心。

您可以手动沿箭头移动标签,但对于这种特殊情况,有一个更巧妙的技巧,还可以轻松避免使用数学斜体标签的错误排版。

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

在此处输入图片描述

答案2

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

这是修改后的代码。

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

相关内容