如何使线边缘倾斜?

如何使线边缘倾斜?

我有一段画两条线的代码

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}
\path[draw=red,line width=3mm](0,0)--(2,0);
\path[draw=red,line width=3mm](0,-1)--(2,-1);
    \end{tikzpicture}
\end{document}

这里我使用图像编辑软件使第二条线有一个倾斜的边缘。

如何使用 TikZ 使线具有类似的倾斜边缘?

答案1

更冗长,但你可以定义新的箭头提示:

在此处输入图片描述

\documentclass[border=5pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\pgfdeclarearrow{
  name=SlantA,
  parameters= {\the\pgfarrowlength},  
  setup code={
   \pgfarrowssettipend{0pt}
   \pgfarrowssetlineend{-\pgfarrowlength}
   \pgfarrowlinewidth=\pgflinewidth
   \pgfarrowssavethe\pgfarrowlength
  },
  drawing code={
   \pgfpathmoveto{\pgfpoint{-\pgfarrowlength}{0.5\pgflinewidth}}
   \pgfpathlineto{\pgfpoint{0}{0.5\pgflinewidth}}
   \pgfpathlineto{\pgfpoint{-\pgfarrowlength}{-0.5\pgflinewidth}}
   \pgfusepathqfill
  },
  defaults = { length = 7pt }
}
\pgfdeclarearrow{
  name=SlantB,
  parameters= {\the\pgfarrowlength},  
  setup code={
   \pgfarrowssettipend{0pt}
   \pgfarrowssetlineend{-\pgfarrowlength}
   \pgfarrowlinewidth=\pgflinewidth
   \pgfarrowssavethe\pgfarrowlength
  },
  drawing code={
   \pgfpathmoveto{\pgfpoint{-\pgfarrowlength}{-0.5\pgflinewidth}}
   \pgfpathlineto{\pgfpoint{0}{-0.5\pgflinewidth}}
   \pgfpathlineto{\pgfpoint{-\pgfarrowlength}{0.5\pgflinewidth}}
   \pgfusepathqfill
  },
  defaults = { length = 7pt }
}
\begin{document}
\begin{tikzpicture}
\path[draw=red,line width=3mm](0,0)--(2,0);
\path[draw=red,line width=3mm,SlantB-SlantA](0,-1)--(2,-1);

\end{tikzpicture}
\end{document}

答案2

如果知道长度和宽度,可以剪裁线条:

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\path[draw=red,line width=3mm](0,0)--(2,0);
\begin{scope}[yshift=-1cm]
\clip (0,0.15)--(2,0.15)--(1.7,-0.15)--(.3,-.15)--cycle;
\path[draw=red,line width=3mm](0,0)--(2,0);
\end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案3

删除了不必要的命令和选项。正确的路径描边。

\documentclass[border=5mm]{standalone}

\usepackage{tikz}
\usetikzlibrary{arrows.meta}

\pgfmathsetlengthmacro{\LINEWIDTH}{3mm}

\colorlet{LINECOLOR}{red}

\pgfdeclarearrow
    {%
        name=TestArrow,
        parameters={\the\pgfarrowlength},%\the\pgfarrowwidth
        setup code=
            {%
                \pgfarrowssetlineend{-\pgfarrowlength}
            },
        drawing code=
            {%
                \pgfsetlinewidth{0.001mm}
                \pgfsetstrokecolor{LINECOLOR}
                \pgfsetfillcolor{LINECOLOR}
                \pgfpathmoveto
                    {%
                        \pgfpoint
                            {-\pgfarrowlength}
                            {-0.5\pgfarrowlength}
                    }
                \pgfpathlineto
                    {%
                        \pgfpoint
                            {-\pgfarrowlength}
                            {0.5\pgfarrowlength}
                    }
                \pgfpathlineto
                    {%
                        \pgfpoint
                            {0mm}
                            {0.5\pgfarrowlength}
                    }
                \pgfpathclose
%               \pgfusepathqstroke % STROKE ONLY
%               \pgfusepathqfill % FILL ONLY
                \pgfusepathqfillstroke % STROKE AND FILL
            },
        defaults={length=\LINEWIDTH}
    }
\begin{document}
    \begin{tikzpicture}
        \path
            [%
                draw=LINECOLOR,
                line width=\LINEWIDTH,
                arrows={TestArrow-}
            ]   (0,0)--(2,0); % DRAW LINE
    \end{tikzpicture}
\end{document}

相关内容