我有两种方法可以在一个线段上画箭头,但我想找到第三种更明确的方法。
1) \draw[arrow scale=3,pos=.75,arrow=Stealth] (A) -- (B);
2) \draw[new arrow={.3}{3}{To}](A)-- (B);
对于最后一个,如果“To”或多或少明确,则 .3 和 3 的情况并非如此。对于第一个,存在混淆。例如,我需要使用“箭头比例”,因为使用了“比例”。这个想法是得到一个混合体,类似于\draw[arrow={[type = Latex,pos=.5,scale=2]}]
具有默认值的可能性。但我不知道如何做到这一点。
\documentclass[a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{ arrows.meta,decorations.markings}
\begin{document}
\tikzset{arrow/.default = Triangle,
pos/.store in = \apos,
pos = .5,
arrow scale/.store in = \ascale,
arrow scale = 1,
arrow/.style = {decoration = {markings,
mark=at position \apos with {\arrow[scale=\ascale]{#1}}
},
postaction = {decorate}
}
}
\begin{tikzpicture}
\coordinate (A) at (2,1) ;
\coordinate (B) at (6,3);
\draw[arrow scale=3,pos=.75,arrow=Stealth] (A) -- (B);
\end{tikzpicture}
\begin{tikzpicture}
\coordinate (A) at (2,1) ;
\coordinate (B) at (6,3);
\tikzset{new arrow/.style n args={3}{decoration={markings,
mark=at position #1 with {\arrow[scale=#2]{#3}}},
postaction={decorate}
}
}
\draw[new arrow={.3}{3}{To}](A)-- (B);
\end{tikzpicture}
\end{document}
答案1
尝试这个。
句法:
on path arrow=<arrow end tip>
on path arrow=<arrow end tip> at <pos>
on path arrow={<arrow end tip>[<arrow options>] at <pos>}
其中<pos>
默认值为0.5。
使用示例:
\draw[on path arrow=Stealth] (A) -- (B);
\draw[on path arrow={To[scale=3] at .3}] (A)-- (B);
\draw[on path arrow={Latex[scale=5,blue] at .8}] (A)-- (B);
实施和完整示例:
\documentclass[a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, decorations.markings}
\begin{document}
\makeatletter
\tikzset{
on path arrow/.code=%
{%
\pgfutil@in@{ at }{#1}%
\ifpgfutil@in@
\mytikz@parsearrow#1\mytikz@stop
\else
\mytikz@parsearrow#1 at .5\mytikz@stop
\fi
}
}
\def\mytikz@parsearrow#1 at #2\mytikz@stop{%
% \pgferror{"\detokenize{#1}", "\detokenize{#2}"} % debug info
\pgfutil@in@{[}{#1}%
\ifpgfutil@in@
\mytikz@parsearrow@opt{#2}#1\mytikz@stop
\else
\mytikz@parsearrow@opt{#2}#1[]\mytikz@stop
\fi
}
% #1 = pos, #2 = arrow end tip, #3 = arrow options
\def\mytikz@parsearrow@opt#1#2[#3]\mytikz@stop{%
\pgfkeysalso{decoration={
markings,
mark=at position #1 with {\arrow[#3]{#2}}
},
postaction={decorate}
}%
}
\makeatother
\begin{tikzpicture}
\coordinate (A) at (2,1);
\coordinate (B) at (6,3);
\draw[on path arrow=Stealth] (A) -- (B);
\draw[on path arrow={To[scale=3] at .3}] (A)-- (B);
\draw[on path arrow={Latex[scale=5,blue] at .8}] (A)-- (B);
\end{tikzpicture}
\end{document}
/pgf/decoration/mark
我引用了key 的实现pgflibrarydecorations.markings.code.tex
。
类似地,on path reversed arrow
可以定义最终将使用的\arrowreversed
而不是\arrow
。再多花点功夫就可以on path arrow
在路径上多次使用 。