Tikz:箭头错位

Tikz:箭头错位

我有以下代码:

\documentclass[tikz,border=1pt,12pt]{standalone}
\usetikzlibrary{calc,decorations.markings,arrows.meta}
\begin{document}
\tikzset{-<-/.style={decoration={
  markings,
  mark=at position .5 with {\arrow{<}}},postaction={decorate}}}
\newcommand\Square[1]{+(-#1,-#1) rectangle +(#1,#1)}  
\begin{tikzpicture}[scale=0.5]
  \draw (2,0) circle (.7);
  \draw[-<-] ($(2,0)+(30:.7)$) to[out=30,in=90] (3.8,0) to[out=-90,in=-30] ($(2,0)+(-30:.7)$);
\end{tikzpicture}
\end{document}

结果不太令人满意: 看看箭头

特别是箭头放错了位置。我怎样才能获得更好的结果?

答案1

第二种方法:您遇到问题的原因是箭头的中心稍微位于尖端后面。这是因为 tikz 的箭头算法的工作方式。详细信息请参阅tikz 手册第 1016 页。无论如何,为了解决您的问题,我可以制作标准箭头的修改版本,其中此偏移量设置为零。为此,我复制了箭头定义代码pgfcorearrows.code.tex并注释掉了定义偏移量的某些有问题的代码。结果如下:

\documentclass[tikz,border=1pt,12pt]{standalone}
\usetikzlibrary{calc,decorations.markings,arrows.meta}

\makeatletter
% copied with offending section commented out from pgfcorearrows.code.tex 
% lines 1103 to 1157
\pgfarrowsdeclare{tonooffset}{tonooffset}
{
%  \pgfutil@tempdima=-0.84pt%
%  \advance\pgfutil@tempdima by-1.3\pgflinewidth%
%  \pgfutil@tempdimb=0.21pt%
%  \advance\pgfutil@tempdimb by.625\pgflinewidth%
%  \pgfarrowsleftextend{+\pgfutil@tempdima}
%  \pgfarrowsrightextend{+\pgfutil@tempdimb}
}
{
  \pgfutil@tempdima=0.28pt%
  \advance\pgfutil@tempdima by.3\pgflinewidth%
  \pgfsetlinewidth{0.8\pgflinewidth}
  \pgfsetdash{}{+0pt}
  \pgfsetroundcap
  \pgfsetroundjoin
  \pgfpathmoveto{\pgfqpoint{-3\pgfutil@tempdima}{4\pgfutil@tempdima}}
  \pgfpathcurveto
  {\pgfqpoint{-2.75\pgfutil@tempdima}{2.5\pgfutil@tempdima}}
  {\pgfqpoint{0pt}{0.25\pgfutil@tempdima}}
  {\pgfqpoint{0.75\pgfutil@tempdima}{0pt}}
  \pgfpathcurveto
  {\pgfqpoint{0pt}{-0.25\pgfutil@tempdima}}
  {\pgfqpoint{-2.75\pgfutil@tempdima}{-2.5\pgfutil@tempdima}}
  {\pgfqpoint{-3\pgfutil@tempdima}{-4\pgfutil@tempdima}}
  \pgfusepathqstroke
}
\pgfarrowsdeclare{tonooffset reversed}{tonooffset reversed}
{
  %\pgfutil@tempdima=-0.21pt%
  %\advance\pgfutil@tempdima by-0.475\pgflinewidth%
  %\pgfutil@tempdimb=0.98pt%
  %\advance\pgfutil@tempdimb by1.45\pgflinewidth%
  %\pgfarrowsleftextend{+\pgfutil@tempdima}
  %\pgfarrowsrightextend{+\pgfutil@tempdimb}
}
{
  \pgfutil@tempdima=0.28pt%
  \advance\pgfutil@tempdima by.3\pgflinewidth%
  \pgfsetlinewidth{+0.8\pgflinewidth}
  \pgfsetdash{}{+0pt}
  \pgfsetroundcap
  \pgfsetroundjoin
  \pgfpathmoveto{\pgfqpoint{3.5\pgfutil@tempdima}{4\pgfutil@tempdima}}
  \pgfpathcurveto
  {\pgfqpoint{3.25\pgfutil@tempdima}{2.5\pgfutil@tempdima}}
  {\pgfqpoint{0.5\pgfutil@tempdima}{0.25\pgfutil@tempdima}}
  {\pgfqpoint{-0.25\pgfutil@tempdima}{0\pgfutil@tempdima}}
  \pgfpathcurveto
  {\pgfqpoint{0.5\pgfutil@tempdima}{-0.25\pgfutil@tempdima}}
  {\pgfqpoint{3.25\pgfutil@tempdima}{-2.5\pgfutil@tempdima}}
  {\pgfqpoint{3.5\pgfutil@tempdima}{-4\pgfutil@tempdima}}
  \pgfusepathqstroke
}
\makeatother

\begin{document}
\tikzset{-<-/.style={decoration={
  markings,
  mark=at position .49 with {\arrow{tonooffset reversed}}},postaction={decorate}}}
\newcommand\Square[1]{+(-#1,-#1) rectangle +(#1,#1)}
\begin{tikzpicture}[scale=0.5]
  \draw (2,0) circle (.7);
  \draw[-<-] ($(2,0)+(30:.7)$) to[out=30,in=90] (3.8,0) to[out=-90,in=-30] ($(2,0)+(-30:.7)$);
\end{tikzpicture}
\end{document}

答案2

不寻常但简单 :-) :

\documentclass[tikz,border=1pt,12pt]{standalone}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}
  \node (n1) [circle, minimum size=7mm, draw] {};;
  \draw (n1) to [loop, out=30,in=-30, looseness=8] coordinate (l) (n1);
  \draw[->,shorten <=5] ([yshift=-5] l) -- (l);
\end{tikzpicture}
\end{document}

在此处输入图片描述

附录:上述解决方案的演变。现已纳入装饰:

\documentclass[tikz,border=1pt,12pt]{standalone}
\usetikzlibrary{arrows.meta, decorations.markings}

\begin{document}
    \begin{tikzpicture}[
Arr/.style = {-{Straight Barb[line width=1pt]},shorten <=5},
decoration = {markings,
              mark=at position .5 with {\draw[Arr,rotate=90] (0,5pt) -- (0,0);}}
                    ]
\node (n1) [circle, minimum size=7mm, draw] {};;
  \draw[postaction={decorate}] (n1) to [loop, out= 30,in=-30, looseness=8] (n1);
  \draw[postaction={decorate}] (n1) to [loop, out= 60,in=120, looseness=8] (n1);
  \draw[postaction={decorate}] (n1) to [loop, out=195,in=255, looseness=8] (n1);
    \end{tikzpicture}
\end{document}

在此处输入图片描述

答案3

这个怎么样?我手动修复了它,但是您已经将循环分成了两部分,因此我认为没有什么危害。

\documentclass[tikz,border=1pt,12pt]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[scale=0.5]
  \draw (2,0) circle (.7);
  \draw ($(2,0)+(30:.7)$) edge[out=30,in=90,->] (3.8,0) (3.8,.014) to[out=-90,in=-30] ($(2,0)+(-30:.7)$);
\end{tikzpicture}
\end{document} 

相关内容