如何为其中的一部分声明具有不同线条粗细的 pgf 形状?

如何为其中的一部分声明具有不同线条粗细的 pgf 形状?

如何更改已声明形状的一部分的线条粗细?我希望它具有其他线条的一半粗细。我尝试\pgfsetlinewidth{0.5\pgflinewidth}在该部分之前添加线条,但没有成功。

在此处输入图片描述

\documentclass[tikz]{standalone}
%--------------------------------------------------------
% Packages
%--------------------------------------------------------
\usepackage[T1]{fontenc}
\usepackage[sfdefault]{AlegreyaSans}
\usepackage{bm}                 % bold math symbols
\usepackage{xcolor}

%--------------------------------------------------------
% Shapes
%--------------------------------------------------------
\makeatletter

\pgfdeclareshape{reg}{
  \savedanchor\northeast{%
    \pgfmathsetlength\pgf@x{\pgfshapeminwidth}%
    \pgfmathsetlength\pgf@y{\pgfshapeminheight}%
    \pgf@x=0.5\pgf@x
    \pgf@y=0.5\pgf@y
  }
  % This is redundant, but makes some things easier:
  \savedanchor\southwest{%
    \pgfmathsetlength\pgf@x{\pgfshapeminwidth}%
    \pgfmathsetlength\pgf@y{\pgfshapeminheight}%
    \pgf@x=-0.5\pgf@x
    \pgf@y=-0.5\pgf@y
  }
  \inheritanchorborder[from=rectangle]

  % Define same anchor a normal rectangle has
  \anchor{center}{\pgfpointorigin}
  \anchor{north}{\northeast \pgf@x=0pt}
  \anchor{east}{\northeast \pgf@y=0pt}
  \anchor{south}{\southwest \pgf@x=0pt}
  \anchor{west}{\southwest \pgf@y=0pt}
  \anchor{north east}{\northeast}
  \anchor{north west}{\northeast \pgf@x=-\pgf@x}
  \anchor{south west}{\southwest}
  \anchor{south east}{\southwest \pgf@x=-\pgf@x}
  \anchor{text}{
    \pgfpointorigin
    \advance\pgf@x by -.5\wd\pgfnodeparttextbox%
    \advance\pgf@y by -.5\ht\pgfnodeparttextbox%
    \advance\pgf@y by +.5\dp\pgfnodeparttextbox%
  }

  %Port Anchors
  \anchor{CLK}{
    \pgf@process{\northeast}%
    \pgf@x=-1\pgf@x%
    \pgf@y=-.6\pgf@y%
  }

  % Draw the Rectangle box and the port labels
  \backgroundpath{
    % Rectangle box
    \pgfpathrectanglecorners{\southwest}{\northeast}

    \begingroup
      %TODO: need to change thickness for this part
      \pgf@anchor@reg@CLK
      \pgf@xa=\pgf@x \pgf@ya=\pgf@y
      \pgf@xb=\pgf@x \pgf@yb=\pgf@y
      \pgf@xc=\pgf@x \pgf@yc=\pgf@y
      \pgfmathsetlength\pgf@x{1.8ex} % size depends on font size
      \advance\pgf@ya by \pgf@x
      \advance\pgf@xb by \pgf@x
      \advance\pgf@yc by -\pgf@x
      \pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya}}
      \pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@yb}}
      \pgfpathlineto{\pgfpoint{\pgf@xc}{\pgf@yc}}
      \pgfclosepath
    \endgroup

  }
}
\makeatother

%--------------------------------------------------------
% Shape configuration
%--------------------------------------------------------

\tikzset{every reg node/.style={draw,minimum width=1cm,minimum height=2cm,pin font=\small,ultra thick,inner sep=1mm,outer sep=0mm}}

%--------------------------------------------------------
% Begin Document
%--------------------------------------------------------
\begin{document}
\begin{tikzpicture}
  \node[shape=reg] (r1) at (0,0) {};
\end{tikzpicture}
\end{document}

答案1

我搞明白了。我用过\pgfusepath{stroke}。它的工作原理与 html 画布描边非常相似。

在此处输入图片描述

\documentclass[tikz]{standalone}
%--------------------------------------------------------
% Packages
%--------------------------------------------------------
\usepackage[T1]{fontenc}
\usepackage[sfdefault]{AlegreyaSans}
\usepackage{bm}                 % bold math symbols
\usepackage{xcolor}

%--------------------------------------------------------
% Shapes
%--------------------------------------------------------
\makeatletter

\pgfkeys{/tikz/pin font/.store in=\pinfont}
\pgfkeys{/tikz/pin color/.store in=\pincolor}

\pgfdeclareshape{reg}{
  \savedanchor\northeast{%
    \pgfmathsetlength\pgf@x{\pgfshapeminwidth}%
    \pgfmathsetlength\pgf@y{\pgfshapeminheight}%
    \pgf@x=0.5\pgf@x
    \pgf@y=0.5\pgf@y
  }
  % This is redundant, but makes some things easier:
  \savedanchor\southwest{%
    \pgfmathsetlength\pgf@x{\pgfshapeminwidth}%
    \pgfmathsetlength\pgf@y{\pgfshapeminheight}%
    \pgf@x=-0.5\pgf@x
    \pgf@y=-0.5\pgf@y
  }
  \inheritanchorborder[from=rectangle]

  % Define same anchor a normal rectangle has
  \anchor{center}{\pgfpointorigin}
  \anchor{north}{\northeast \pgf@x=0pt}
  \anchor{east}{\northeast \pgf@y=0pt}
  \anchor{south}{\southwest \pgf@x=0pt}
  \anchor{west}{\southwest \pgf@y=0pt}
  \anchor{north east}{\northeast}
  \anchor{north west}{\northeast \pgf@x=-\pgf@x}
  \anchor{south west}{\southwest}
  \anchor{south east}{\southwest \pgf@x=-\pgf@x}
  \anchor{text}{
    \pgfpointorigin
    \advance\pgf@x by -.5\wd\pgfnodeparttextbox%
    \advance\pgf@y by -.5\ht\pgfnodeparttextbox%
    \advance\pgf@y by +.5\dp\pgfnodeparttextbox%
  }

  %Port Anchors
  \anchor{CLK}{
    \pgf@process{\northeast}%
    \pgf@x=-1\pgf@x%
    \pgf@y=-.6\pgf@y%
  }

  % Draw the Rectangle box and the port labels
  \backgroundpath{
    %save default linewidth
    \pgfmathsetlength\pgf@xd{\pgflinewidth}
    \pgfsetlinewidth{\pgf@xd}
    % Rectangle box
    \pgfpathrectanglecorners{\southwest}{\northeast}
    \pgfusepath{stroke} % draw rectangle at default linewidth

    \pgfsetlinewidth{.5\pgf@xd} % set linewidth to 50% default linewidth
    \begingroup
      \pgf@anchor@reg@CLK
      \pgf@xa=\pgf@x \pgf@ya=\pgf@y
      \pgf@xb=\pgf@x \pgf@yb=\pgf@y
      \pgf@xc=\pgf@x \pgf@yc=\pgf@y
      \pgfmathsetlength\pgf@x{1.8ex} % size depends on font size
      \advance\pgf@ya by \pgf@x
      \advance\pgf@xb by \pgf@x
      \advance\pgf@yc by -\pgf@x
      \pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya}}
      \pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@yb}}
      \pgfpathlineto{\pgfpoint{\pgf@xc}{\pgf@yc}}
      \pgfclosepath
    \endgroup
    \pgfusepath{stroke} % draw triangle at 50% default linewidth
    \pgfsetlinewidth{\pgf@xd} % reset to default linewidth
  }
}
\makeatother

%--------------------------------------------------------
% Shape configuration
%--------------------------------------------------------

\tikzset{every reg node/.style={draw,minimum width=1cm,minimum height=2cm,pin font=\small,ultra thick,inner sep=1mm,outer sep=0mm}}

%--------------------------------------------------------
% Begin Document
%--------------------------------------------------------
\begin{document}
\begin{tikzpicture}
  \node[shape=reg] (r1) at (0,0) {};
  \node[shape=reg] (r2) at (2,0) {};
\end{tikzpicture}
\end{document}

相关内容