圆角在自定义装饰中无法正常工作

圆角在自定义装饰中无法正常工作

我想用 TikZ 创建一些化学路易斯结构 - 我不能使用chemfig它因为我需要根据它们的坐标绘制原子 - 并且需要定义一些自定义键类型,例如楔形键和虚线楔形键。装饰似乎是做到这一点的自然方式(如果有人知道更好的方法,我愿意接受建议)并设法使一些东西工作。我这样做是为了让键被一些白色空间包围,以便使其在两个键可能重叠的情况下看起来正确(我将白色空间涂成蓝色以使其在我的示例代码中可见)。为了让它看起来更漂亮一点,我想使用圆角,但不幸的是这些似乎在我的装饰中无法正常工作。我怎样才能让圆角工作?

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{decorations}

\begin{document}

\begin{tikzpicture}

\newlength{\dashedwedgelen}
\setlength{\dashedwedgelen}{5pt}

\newlength{\wedgeheight}
\setlength{\wedgeheight}{2.5pt}

\pgfmathsetmacro\startheight{\wedgeheight/5}
\pgfmathsetmacro\whitespace{\wedgeheight/2}
\pgfmathsetmacro\dashgapratio{0.3}

\pgfdeclaredecoration{dashed wedge}{initial}
{
  \state{initial}[
  width=0.0,
  persistent precomputation={
    \pgfmathsetmacro\dashseglen{(\pgfdecoratedpathlength - \dashgapratio*\dashedwedgelen) / floor((\pgfdecoratedpathlength - \dashgapratio*\dashedwedgelen)/\dashedwedgelen)}},
  next state=body
  ]
  {
    % surrounding white space
    \color{blue}
    \pgfpathmoveto{\pgfpoint{0}{\startheight + \whitespace}}
    \pgfpathlineto{\pgfpoint{\pgfdecoratedpathlength}{\wedgeheight + \whitespace}}
    \pgfpathlineto{\pgfpoint{\pgfdecoratedpathlength}{-(\wedgeheight + \whitespace)}}
    \pgfpathlineto{\pgfpoint{0}{-(\startheight + \whitespace)}}
    \pgfpathlineto{\pgfpoint{0}{\startheight + \whitespace}}
    \pgfclosepath
    \pgfusepath{fill, stroke}
    \pgfpathmoveto{\pgfpointdecoratedinputsegmentlast}
  }
  \state{body}[width=\dashseglen]
  {
    % computations
    \pgfmathsetmacro\segwidth{\dashgapratio*\dashseglen}
    \pgfmathsetmacro\segstartheight{\startheight + (\wedgeheight - \startheight)*(\pgfdecoratedcompleteddistance) / \pgfdecoratedpathlength}
    \pgfmathsetmacro\segendheight{\startheight + (\wedgeheight - \startheight)*(\pgfdecoratedcompleteddistance + \dashgapratio*\dashseglen) / \pgfdecoratedpathlength}
    % drawing
    \pgfpathmoveto{\pgfpoint{0}{\segstartheight}}
    \pgfpathlineto{\pgfpoint{\segwidth}{\segendheight}}
    \pgfpathlineto{\pgfpoint{\segwidth}{-\segendheight}}
    \pgfpathlineto{\pgfpoint{0}{-\segstartheight}}
    \pgfpathlineto{\pgfpoint{0}{\segstartheight}}
    \pgfpathclose
  }
  \state{final}
  { 
    % computations
    \pgfmathsetmacro\segwidth{\dashgapratio*\dashseglen}
    \pgfmathsetmacro\segstartheight{\startheight + (\wedgeheight - \startheight)*(\pgfdecoratedcompleteddistance) / \pgfdecoratedpathlength}
    \pgfmathsetmacro\segendheight{\startheight + (\wedgeheight - \startheight)*(\pgfdecoratedcompleteddistance + \dashgapratio*\dashseglen) / \pgfdecoratedpathlength}
    % drawing
    \pgfpathmoveto{\pgfpoint{0}{\segstartheight}}
    \pgfpathlineto{\pgfpoint{\segwidth}{\segendheight}}
    \pgfpathlineto{\pgfpoint{\segwidth}{-\segendheight}}
    \pgfpathlineto{\pgfpoint{0}{-\segstartheight}}
    \pgfpathlineto{\pgfpoint{0}{\segstartheight}}
    \pgfpathclose
    \pgfmoveto{\pgfpointdecoratedpathlast}
  }
}

\node (x) at (0,0) [rectangle, rounded corners=0.8mm, fill = blue, inner sep=0.1mm, scale=2.5] {Ti};
\node (y) at (1.5,0.) [rectangle, rounded corners=0.8mm, fill = blue, inner sep=0.1mm, scale=2.5] {L};

\fill [red, decorate, decoration=dashed wedge, rounded corners=0.5pt] (x) -- (y);

\end{tikzpicture}

\end{document}

在此处输入图片描述

答案1

问题在于您明确地绘制了一条线并关闭了路径。如果您删除\pgfpathlineto每个之前的\pgfclosepath,则问题就消失了。

还可以通过使用\pgfqpoint(不使用数学解析器)和\pgfmathsetlengthmacro(确保存储在宏中的解析表达式以 结尾pt)使代码更加高效。

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{decorations}

\begin{document}

\begin{tikzpicture}

\newlength{\dashedwedgelen}
\setlength{\dashedwedgelen}{5pt}

\newlength{\wedgeheight}
\setlength{\wedgeheight}{2.5pt}

\pgfmathsetmacro\startheight{\wedgeheight/5}
\pgfmathsetmacro\whitespace{\wedgeheight/2}
\pgfmathsetmacro\dashgapratio{0.3}

\pgfdeclaredecoration{dashed wedge}{initial}
{
  \state{initial}[
  width=0.0,
  persistent precomputation={
    \pgfmathsetmacro\dashseglen{(\pgfdecoratedpathlength - \dashgapratio*\dashedwedgelen) / floor((\pgfdecoratedpathlength - \dashgapratio*\dashedwedgelen)/\dashedwedgelen)}%
    %
    \pgfmathsetlengthmacro\wedgeheight{\wedgeheight}%
    \pgfmathsetlengthmacro\startheight{\startheight}%
    \pgfmathsetlengthmacro\whitespace{\whitespace}%
    },
  next state=body
  ]
  {
    % surrounding white space
    \pgfmathsetlengthmacro\beginheight{\startheight+\whitespace}
    \pgfmathsetlengthmacro\endheight{\wedgeheight + \whitespace}
    \pgfpathmoveto{\pgfqpoint{0pt}{\beginheight}}
    \pgfpathlineto{\pgfqpoint{\pgfdecoratedpathlength}{\endheight}}
    \pgfpathlineto{\pgfqpoint{\pgfdecoratedpathlength}{-\endheight}}
    \pgfpathlineto{\pgfqpoint{0pt}{-\beginheight}}
    \pgfclosepath
    \pgfsetfillcolor{blue}
    \pgfsetstrokecolor{blue}
    \pgfusepath{fill, stroke}
  }
  \state{body}[width=\dashseglen]
  {
    % computations
    \pgfmathsetlengthmacro\segwidth{\dashgapratio*\dashseglen}
    \pgfmathsetlengthmacro\segstartheight{\startheight + (\wedgeheight - \startheight)*(\pgfdecoratedcompleteddistance) / \pgfdecoratedpathlength}
    \pgfmathsetlengthmacro\segendheight{\startheight + (\wedgeheight - \startheight)*(\pgfdecoratedcompleteddistance + \dashgapratio*\dashseglen) / \pgfdecoratedpathlength}
    % drawing
    \pgfpathmoveto{\pgfqpoint{0pt}{\segstartheight}}
    \pgfpathlineto{\pgfqpoint{\segwidth}{\segendheight}}
    \pgfpathlineto{\pgfqpoint{\segwidth}{-\segendheight}}
    \pgfpathlineto{\pgfqpoint{0pt}{-\segstartheight}}
    \pgfpathclose
  }
  \state{final}
  { 
    % computations
    \pgfmathsetlengthmacro\segwidth{\dashgapratio*\dashseglen}
    \pgfmathsetlengthmacro\segstartheight{\startheight + (\wedgeheight - \startheight)*(\pgfdecoratedcompleteddistance) / \pgfdecoratedpathlength}
    \pgfmathsetlengthmacro\segendheight{\startheight + (\wedgeheight - \startheight)*(\pgfdecoratedcompleteddistance + \dashgapratio*\dashseglen) / \pgfdecoratedpathlength}
    % drawing
    \pgfpathmoveto{\pgfqpoint{0pt}{\segstartheight}}
    \pgfpathlineto{\pgfqpoint{\segwidth}{\segendheight}}
    \pgfpathlineto{\pgfqpoint{\segwidth}{-\segendheight}}
    \pgfpathlineto{\pgfqpoint{0pt}{-\segstartheight}}
    \pgfpathclose
  }
}

\node (x) at (0,0) [rectangle, rounded corners=0.8mm, fill = blue, inner sep=0.1mm, scale=2.5] {Ti};
\node (y) at (1.5,0.) [rectangle, rounded corners=0.8mm, fill = blue, inner sep=0.1mm, scale=2.5] {L};

\fill [red, decorate, decoration=dashed wedge, rounded corners=0.5pt] (x) -- (y);

\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容