是否可以更改 TikZ/PGF 中箭头的大小?

是否可以更改 TikZ/PGF 中箭头的大小?

我想画一个比正常箭头更大的箭头。在不改变线条本身样式的情况下可以实现吗?

答案1

更新 2013-02-27

arrows.meta该代码现在已被TikZ 3.0.0 捆绑的新库取代。

参见使用示例以下是 Ignasi 的回答


更新 2013-01-23

完整代码的开发版本现已发布源码。旧arrowhead关键字现已被替换,arrow head以与包中的其他关键字保持一致。


更新 2012-11-13

我编辑了所有预定义的箭头提示和箭头库中的箭头提示,除了“几乎 0d”的箭头提示(serif cmspace所有线帽箭头)和implies(因为我认为这确实取决于线宽),其原始代码已包含在完整性中,以便只需一个代码就可以找到所有以前可用的箭头。

对于“几乎 1d”的箭头(类似钩子和括号),箭头值设置尖端的高度,而不是其长度(对于左和右变体,是整个钩子的高度)。

箭头的代码to new现已修复。对于 45 度箭头,尖端的幅度实际上是 45°,而不是原始代码中的 46°。

当然,您也可以使用新的箭头作为标记。

我无法上传新代码,因为它超出了答案的大小限制,但我发布了新测试文件结果的图片。我给 Till Tantau 发了一封电子邮件,要求将代码转入新库,TikZ然后我们再看看。


我不是TikZ大师,所以我已经尽力了。因此,欢迎大家改进代码,我真的认为它可以改进。将新的箭头代码另存为,并通过arrowsnew.tex加载到\input{arrowsnew}您的序言中。有九个新箭头,分别名为、、、、、、latexnew和(latexnew reversed或)。它的工作原理如下:如果您调用没有键的箭头或没有为其分配任何值,则使用原始代码,以便新代码可以完全替换旧代码。否则,如果您为键分配一个值,则箭头的长度将设置为。目前,箭头与原始箭头略有不同。请不要直接编辑代码,但可以随意在新的答案中重复使用/编辑它。latex'newlatex'new reversedonewsquarenewstealthnewstealthnew reversedtonew>newarrowheadarrowhead<value>tonew

新代码的结果 2012-11-13

在此处输入图片描述

旧的测试代码

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows}
\input{arrowsnew}

\tikzset{options/.code={\tikzset{#1}}} % just to compact the code

\begin{document}
\begin{tikzpicture}
\draw [red] (2,-24.2) -- (2,0.2) node [above right, black] {1 cm};
\draw [red] (3,-24.2) -- (3,0.2);
\foreach \x [count=\i from 0] in {
  {latex-latexnew},
  {latex-latexnew, arrowhead=1cm},
  {latex-latexnew, arrowhead=1cm, line width=1pt},
  {latex reversed-latexnew reversed, arrowhead=1cm, line width=1pt},
  {latex'-latex'new},
  {latex'-latex'new, arrowhead=1cm},
  {latex'-latex'new, arrowhead=1cm, line width=2pt},
  {latex' reversed-latex'new reversed, arrowhead=1cm, line width=2pt},
  {o-onew},
  {o-onew, arrowhead=1cm},
  {o-onew, arrowhead=1cm, line width=2pt},
  {square-squarenew},
  {square-squarenew, arrowhead=1cm},
  {square-squarenew, arrowhead=1cm, ultra thick},
  {stealth-stealthnew},
  {stealth-stealthnew, arrowhead=1cm},
  {stealth-stealthnew, arrowhead=1cm, ultra thick},
  {stealth reversed-stealthnew reversed, arrowhead=1cm, ultra thick},
  {to-tonew},
  {to-tonew, arrowhead=1cm}
}
{
  \draw [options/.expand once=\x] (0,-1.2*\i) -- (3,-1.2*\i) node [right] {\x};
}
\draw [<->new, arrowhead=0.25in, line width=4pt] (0,-24) -- (3,-24)
      node [right] {\verb!<->!new, arrowhead=0.25in, line width=4pt};  
\draw [red] (3,-23.2) ++ (-0.25in,0) -- ++(0,-1.6) node [below,black] {0.25 in}; 
\end{tikzpicture}
\end{document}

结果

在此处输入图片描述

旧箭头新代码(已被取代)

% This is not an official TikZ library. Use at your own risk!

\makeatletter
% alternative latex arrow
\pgfarrowsdeclare{latexnew}{latexnew}
{
  \ifdim\pgfgetarrowoptions{latexnew}=-1pt%
    \pgfutil@tempdima=0.28pt%
    \pgfutil@tempdimb=\pgflinewidth%
    \ifdim\pgfinnerlinewidth>0pt%
      \pgfmathsetlength\pgfutil@tempdimb{.6\pgflinewidth-.4*\pgfinnerlinewidth}%
    \fi%
    \advance\pgfutil@tempdima by.3\pgfutil@tempdimb%
  \else%
    \pgfutil@tempdima=\pgfgetarrowoptions{latexnew}%
    \divide\pgfutil@tempdima by 10%
  \fi%
  \pgfarrowsleftextend{+-1\pgfutil@tempdima}%
  \pgfarrowsrightextend{+9\pgfutil@tempdima}%
}
{
  \ifdim\pgfgetarrowoptions{latexnew}=-1pt%
    \pgfutil@tempdima=0.28pt%
    \pgfutil@tempdimb=\pgflinewidth%
    \ifdim\pgfinnerlinewidth>0pt%
      \pgfmathsetlength\pgfutil@tempdimb{.6\pgflinewidth-.4*\pgfinnerlinewidth}%
    \fi%
    \advance\pgfutil@tempdima by.3\pgfutil@tempdimb%
  \else%
    \pgfutil@tempdima=\pgfgetarrowoptions{latexnew}%
    \divide\pgfutil@tempdima by 10%
    \pgfsetlinewidth{0bp}%
  \fi%
  \pgfpathmoveto{\pgfqpoint{9\pgfutil@tempdima}{0pt}}
  \pgfpathcurveto
  {\pgfqpoint{6.3333\pgfutil@tempdima}{.5\pgfutil@tempdima}}
  {\pgfqpoint{2\pgfutil@tempdima}{2\pgfutil@tempdima}}
  {\pgfqpoint{-1\pgfutil@tempdima}{3.75\pgfutil@tempdima}}
  \pgfpathlineto{\pgfqpoint{-1\pgfutil@tempdima}{-3.75\pgfutil@tempdima}}
  \pgfpathcurveto
  {\pgfqpoint{2\pgfutil@tempdima}{-2\pgfutil@tempdima}}
  {\pgfqpoint{6.3333\pgfutil@tempdima}{-.5\pgfutil@tempdima}}
  {\pgfqpoint{9\pgfutil@tempdima}{0pt}}
  \pgfusepathqfill
}

% alternative latex reversed arrow
\pgfarrowsdeclarereversed{latexnew reversed}{latexnew reversed}{latexnew}{latexnew}

% alternative latex' arrow
\pgfarrowsdeclare{latex'new}{latex'new}
{
  \ifdim\pgfgetarrowoptions{latex'new}=-1pt%
    \pgfutil@tempdima=0.28pt%
    \advance\pgfutil@tempdima by.3\pgflinewidth%
  \else%
    \pgfutil@tempdima=\pgfgetarrowoptions{latex'new}%
    \divide\pgfutil@tempdima by 10%
  \fi%
  \pgfarrowsleftextend{+-4\pgfutil@tempdima}
  \pgfarrowsrightextend{+6\pgfutil@tempdima}
}
{
  \ifdim\pgfgetarrowoptions{latex'new}=-1pt%
    \pgfutil@tempdima=0.28pt%
    \advance\pgfutil@tempdima by.3\pgflinewidth%
  \else%
    \pgfutil@tempdima=\pgfgetarrowoptions{latex'new}%
    \divide\pgfutil@tempdima by 10%
    \pgfsetlinewidth{0bp}%
  \fi%
  \pgfpathmoveto{\pgfqpoint{6\pgfutil@tempdima}{0\pgfutil@tempdima}}
  \pgfpathcurveto
  {\pgfqpoint{3.5\pgfutil@tempdima}{.5\pgfutil@tempdima}}
  {\pgfqpoint{-1\pgfutil@tempdima}{1.5\pgfutil@tempdima}}
  {\pgfqpoint{-4\pgfutil@tempdima}{3.75\pgfutil@tempdima}}
  \pgfpathcurveto
  {\pgfqpoint{-1.5\pgfutil@tempdima}{1\pgfutil@tempdima}}
  {\pgfqpoint{-1.5\pgfutil@tempdima}{-1\pgfutil@tempdima}}
  {\pgfqpoint{-4\pgfutil@tempdima}{-3.75\pgfutil@tempdima}}
  \pgfpathcurveto
  {\pgfqpoint{-1\pgfutil@tempdima}{-1.5\pgfutil@tempdima}}
  {\pgfqpoint{3.5\pgfutil@tempdima}{-.5\pgfutil@tempdima}}
  {\pgfqpoint{6\pgfutil@tempdima}{0\pgfutil@tempdima}}
  \pgfusepathqfill
}

% alternative latex' reversed arrow
\pgfarrowsdeclarereversed{latex'new reversed}{latex'new reversed}{latex'new}{latex'new}

% alternative o arrow
\pgfarrowsdeclare{onew}{onew}
{
  \pgfarrowsleftextend{+-.5\pgflinewidth}
  \ifdim\pgfgetarrowoptions{onew}=-1pt%
    \pgfutil@tempdima=0.4pt%
    \advance\pgfutil@tempdima by.2\pgflinewidth%
    \pgfutil@tempdimb=9\pgfutil@tempdima\advance\pgfutil@tempdimb by.5\pgflinewidth%
    \pgfarrowsrightextend{+\pgfutil@tempdimb}%
  \else%
    \pgfutil@tempdima=\pgfgetarrowoptions{onew}%
    \advance\pgfutil@tempdima by -0.5\pgflinewidth%
    \pgfarrowsrightextend{+\pgfutil@tempdima}%
  \fi%
}
{ 
  \ifdim\pgfgetarrowoptions{onew}=-1pt%
    \pgfutil@tempdima=0.4pt%
    \advance\pgfutil@tempdima by.2\pgflinewidth%
    \pgfutil@tempdimb=0pt%
  \else%
    \pgfutil@tempdima=\pgfgetarrowoptions{onew}%
    \divide\pgfutil@tempdima by 9%
    \pgfutil@tempdimb=0.5\pgflinewidth%
  \fi%
  \pgfsetdash{}{+0pt}
  \pgfpathcircle{\pgfpointadd{\pgfqpoint{4.5\pgfutil@tempdima}{0bp}}%
                             {\pgfqpoint{-\pgfutil@tempdimb}{0bp}}}%
                {4.5\pgfutil@tempdima-\pgfutil@tempdimb}%
  \pgfusepathqstroke
}

% alternative square arrow
\pgfarrowsdeclare{squarenew}{squarenew}
{
 \ifdim\pgfgetarrowoptions{squarenew}=-1pt%
   \pgfutil@tempdima=0.4pt
   \advance\pgfutil@tempdima by.275\pgflinewidth%
   \pgfarrowsleftextend{+-\pgfutil@tempdima}
   \advance\pgfutil@tempdima by.5\pgflinewidth
   \pgfarrowsrightextend{+\pgfutil@tempdima}
 \else%
   \pgfutil@tempdima=\pgfgetarrowoptions{squarenew}%
   \divide\pgfutil@tempdima by 8%
   \pgfarrowsleftextend{+-7\pgfutil@tempdima}%
   \pgfarrowsrightextend{+1\pgfutil@tempdima}%
 \fi%
}
{
 \ifdim\pgfgetarrowoptions{squarenew}=-1pt%
   \pgfutil@tempdima=0.4pt%
   \advance\pgfutil@tempdima by.275\pgflinewidth%
   \pgfutil@tempdimb=0pt%
 \else%
   \pgfutil@tempdima=\pgfgetarrowoptions{squarenew}%   
   \divide\pgfutil@tempdima by 8%
   \pgfutil@tempdimb=0.5\pgflinewidth%
 \fi%
 \pgfsetdash{}{+0pt}
 \pgfsetroundjoin
 \pgfpathmoveto{\pgfpointadd{\pgfqpoint{1\pgfutil@tempdima}{4\pgfutil@tempdima}}
                            {\pgfqpoint{-\pgfutil@tempdimb}{-\pgfutil@tempdimb}}}
 \pgfpathlineto{\pgfpointadd{\pgfqpoint{-7\pgfutil@tempdima}{4\pgfutil@tempdima}}
                            {\pgfqpoint{\pgfutil@tempdimb}{-\pgfutil@tempdimb}}}
 \pgfpathlineto{\pgfpointadd{\pgfqpoint{-7\pgfutil@tempdima}{-4\pgfutil@tempdima}}
                            {\pgfqpoint{\pgfutil@tempdimb}{\pgfutil@tempdimb}}}
 \pgfpathlineto{\pgfpointadd{\pgfqpoint{1\pgfutil@tempdima}{-4\pgfutil@tempdima}}
                            {\pgfqpoint{-\pgfutil@tempdimb}{\pgfutil@tempdimb}}}
 \pgfpathclose
 \pgfusepathqfillstroke
}

% alternative stealth arrow
\pgfarrowsdeclare{stealthnew}{stealthnew}
{
  \ifdim\pgfgetarrowoptions{stealthnew}=-1pt%
    \pgfutil@tempdima=0.28pt%
    \pgfutil@tempdimb=\pgflinewidth%
    \ifdim\pgfinnerlinewidth>0pt%
      \pgfmathsetlength\pgfutil@tempdimb{.6\pgflinewidth-.4*\pgfinnerlinewidth}%
    \fi%
    \advance\pgfutil@tempdima by.3\pgfutil@tempdimb%
  \else%
    \pgfutil@tempdima=\pgfgetarrowoptions{stealthnew}%
    \divide\pgfutil@tempdima by 8%
  \fi%
  \pgfarrowsleftextend{+-3\pgfutil@tempdima}
  \pgfarrowsrightextend{+5\pgfutil@tempdima}
}
{
  \ifdim\pgfgetarrowoptions{stealthnew}=-1pt%
    \pgfutil@tempdima=0.28pt%
    \pgfutil@tempdimb=\pgflinewidth%
    \ifdim\pgfinnerlinewidth>0pt%
      \pgfmathsetlength\pgfutil@tempdimb{.6\pgflinewidth-.4*\pgfinnerlinewidth}%
    \fi%
    \advance\pgfutil@tempdima by.3\pgfutil@tempdimb%
  \else%
    \pgfutil@tempdima=\pgfgetarrowoptions{stealthnew}%
    \divide\pgfutil@tempdima by 8%
    \pgfsetlinewidth{0bp}%
  \fi%
  \pgfpathmoveto{\pgfqpoint{5\pgfutil@tempdima}{0pt}}
  \pgfpathlineto{\pgfqpoint{-3\pgfutil@tempdima}{4\pgfutil@tempdima}}
  \pgfpathlineto{\pgfpointorigin}
  \pgfpathlineto{\pgfqpoint{-3\pgfutil@tempdima}{-4\pgfutil@tempdima}}
  \pgfusepathqfill
}

% alternative stealth reversed arrow
\pgfarrowsdeclarereversed{stealthnew reversed}{stealthnew reversed}{stealthnew}{stealthnew}

% alternative to arrow
\pgfarrowsdeclare{tonew}{tonew}
{
  \ifdim\pgfgetarrowoptions{tonew}=-1pt%
    \pgfutil@tempdima=0.84pt%
    \advance\pgfutil@tempdima by1.3\pgflinewidth%
    \pgfutil@tempdimb=0.21pt%
    \advance\pgfutil@tempdimb by.625\pgflinewidth%
  \else%
    \pgfutil@tempdima=\pgfgetarrowoptions{tonew}%
    \pgfarrowsleftextend{+-0.8\pgfutil@tempdima}%
    \pgfarrowsrightextend{+0.2\pgfutil@tempdima}%
  \fi%
}
{
  \ifdim\pgfgetarrowoptions{tonew}=-1pt%
    \pgfutil@tempdima=0.28pt%
    \advance\pgfutil@tempdima by.3\pgflinewidth%
    \pgfutil@tempdimb=0pt,%
  \else%
    \pgfutil@tempdima=\pgfgetarrowoptions{tonew}%
    \multiply\pgfutil@tempdima by 100%
    \divide\pgfutil@tempdima by 375%
    \pgfutil@tempdimb=0.4\pgflinewidth%
  \fi%
  \pgfsetdash{}{+0pt}
  \pgfsetroundcap
  \pgfsetroundjoin
  \pgfpathmoveto{\pgfpointorigin}
  \pgflineto{\pgfpointadd{\pgfpoint{0.75\pgfutil@tempdima}{0bp}}
                         {\pgfqpoint{-2\pgfutil@tempdimb}{0bp}}}
  \pgfusepathqstroke
  \pgfsetlinewidth{0.8\pgflinewidth}
  \pgfpathmoveto{\pgfpointadd{\pgfqpoint{-3\pgfutil@tempdima}{4\pgfutil@tempdima}}
                             {\pgfqpoint{\pgfutil@tempdimb}{0bp}}}
  \pgfpathcurveto
  {\pgfpointadd{\pgfqpoint{-2.75\pgfutil@tempdima}{2.5\pgfutil@tempdima}}
               {\pgfqpoint{0.5\pgfutil@tempdimb}{0bp}}}
  {\pgfpointadd{\pgfqpoint{0pt}{0.25\pgfutil@tempdima}}
               {\pgfqpoint{-0.5\pgfutil@tempdimb}{0bp}}}
  {\pgfpointadd{\pgfqpoint{0.75\pgfutil@tempdima}{0pt}}
               {\pgfqpoint{-\pgfutil@tempdimb}{0bp}}}
  \pgfpathcurveto
  {\pgfpointadd{\pgfqpoint{0pt}{-0.25\pgfutil@tempdima}}
               {\pgfqpoint{-0.5\pgfutil@tempdimb}{0bp}}}
  {\pgfpointadd{\pgfqpoint{-2.75\pgfutil@tempdima}{-2.5\pgfutil@tempdima}}
               {\pgfqpoint{0.5\pgfutil@tempdimb}{0bp}}}
  {\pgfpointadd{\pgfqpoint{-3\pgfutil@tempdima}{-4\pgfutil@tempdima}}
               {\pgfqpoint{\pgfutil@tempdimb}{0bp}}}
  \pgfusepathqstroke
}

% alias alternative to arrow
\pgfarrowsdeclarealias{<new}{>new}{tonew}{tonew}

\makeatother

% tip length code
\pgfsetarrowoptions{latexnew}{-1pt}
\pgfsetarrowoptions{latex'new}{-1pt}
\pgfsetarrowoptions{onew}{-1pt}
\pgfsetarrowoptions{squarenew}{-1pt}
\pgfsetarrowoptions{stealthnew}{-1pt}
\pgfsetarrowoptions{tonew}{-1pt}
\pgfkeys{/tikz/.cd, arrowhead/.default=-1pt, arrowhead/.code={
  \pgfsetarrowoptions{latexnew}{#1},
  \pgfsetarrowoptions{latex'new}{#1},
  \pgfsetarrowoptions{onew}{#1},
  \pgfsetarrowoptions{squarenew}{#1},
  \pgfsetarrowoptions{stealthnew}{#1},
  \pgfsetarrowoptions{tonew}{#1},
}}

答案2

TikZ v3.0(除其他外新功能)包含一个新arrows.meta库。它可能受到路易吉的图书馆并弃用以前的arrowsarrows.spaced库。

使用这个新库,可以很容易地定义箭头参数,例如长度或宽度(取决于或不取决于线宽)。

\draw[-{Latex[length=3mm,width=5mm]}] (0,0)--(2,0);

将绘制一个Latex箭头长 3 毫米、粗 5 毫米的箭头。所有新的箭头已更改其名称,现在以大写字母开头,以区别于老的名称。当然,旧名称仍然有效,但它们不接受如上例中的选项。

值得一读的是 pgfmanual 中的“16 Arrows”部分。

一些小例子:

\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}
\draw[-latex] (0,0)--(2,0) node[right]{\emph{Old} latex arrow};
\draw[-Latex] (0,-.5)--(2,-.5) node[right]{\emph{new} Latex arrow};
\draw[-{Latex[length=3mm]}] (0,-1)--(2,-1) node[right]{with fixed length};
\draw[-{Latex[width=3mm]}] (0,-1.5)--(2,-1.5) node[right]{with fixed width};
\draw[-{Latex[length=5mm, width=2mm]}] (0,-2)--(2,-2) node[right]{widh fixed length and width};
\draw[-{Latex[red]}] (0,-2.5)--(2,-2.5) node[right]{with colorful tip};
\end{tikzpicture}
\end{document}

在此处输入图片描述

有关使用的更多示例arrows.meta

答案3

以下是 PGF/TikZ 2.10 中指向外部的三角形尖端的示例(请参阅第 24 节“箭头尖端库”2.10 手册):

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{arrows}
\begin{document}
\newcounter{tmp}
\begin{tikzpicture}
\foreach \s in {latex,latex',stealth,triangle 90,triangle 45,angle 90, angle 60} {
    \stepcounter{tmp}
    \begin{scope}[yshift=-\thetmp cm]
        \node[anchor=west] (0,0) {\texttt{\s}};     
        \draw[arrows={\s-\s}] (3,0) --++ (1,0);
    \end{scope}
}
\end{tikzpicture}
\end{document}

箭

如果你想要更定制化或更具体的内容,请阅读第 82 节“箭头提示” pgf 2.10 手册。您寻求的规模有多大以及方向是什么?

答案4

我刚刚偶然发现了同样的问题,但是那些解决方案不符合我的需要。但我记得后续操作,所以我想到了这个:

\usetikzlibrary{arrows}
\begin{tikzpicture}
    \draw[
        -triangle 90,
        line width=4mm,
        postaction={draw, line width=1cm, shorten >=1cm, -}
    ] (0,0) -- (2,0);
\end{tikzpicture}

https://i.stack.imgur.com/WbXRg.png

这个想法是选择一个产生正确箭头的线宽,然后用更粗的线再次绘制线条。

当然,只有当你需要线比头更粗时它才有效。

相关内容