我怎样才能使声明的形状的 pgftext 具有不同的颜色,而形状本身却不具有不同的颜色?
下面是我想要的和代码(目前我在中间的框中使用黑色文字)。
\documentclass[tikz]{standalone}
%--------------------------------------------------------
% Packages
%--------------------------------------------------------
\usepackage[T1]{fontenc}
\usepackage[sfdefault]{AlegreyaSans}
\usepackage{bm} % bold math symbols
\usepackage{xcolor}
%--------------------------------------------------------
% Shapes
%--------------------------------------------------------
\makeatletter
\pgfdeclareshape{shape1}{
\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{A}{
\pgf@process{\northeast}%
\pgf@x=-1\pgf@x%
\pgf@y=.5\pgf@y%
}
\anchor{B}{
\pgf@process{\northeast}%
\pgf@x=-1\pgf@x%
\pgf@y=-.5\pgf@y%
}
% Draw the Rectangle box and the port labels
\backgroundpath{
% Rectangle box
\pgfpathrectanglecorners{\southwest}{\northeast}
% Draw port labels
\begingroup
\tikzset{mystyle/port labels} % Use font from this style
\tikz@textfont
\csname pgf@anchor@shape1@A\endcsname %
\pgftext[left,at={\pgfpoint{\pgf@x}{\pgf@y}},x=\pgfshapeinnerxsep]{A}
\csname pgf@anchor@shape1@B\endcsname %
\pgftext[left,at={\pgfpoint{\pgf@x}{\pgf@y}},x=\pgfshapeinnerxsep]{B}
\endgroup
}
}
\makeatother
%--------------------------------------------------------
% Shape configuration
%--------------------------------------------------------
% Key to add font macros to the current font
\tikzset{add font/.code={\expandafter\def\expandafter\tikz@textfont\expandafter{\tikz@textfont#1}}}
% % Define default style for this node
\tikzset{mystyle/port labels/.style={font=\small,text=blue}}
\tikzset{every shape1 node/.style={draw,minimum width=2.5cm,minimum height=2cm,very thick,inner sep=1mm,outer sep=0mm,cap=round}}
%--------------------------------------------------------
% Begin Document
%--------------------------------------------------------
\begin{document}
\begin{tikzpicture}
\node[shape=shape1] (p1) at (0,0) {};
\node[shape=shape1] (p2) at (0,3) {};
\node[shape=shape1] (p3) at (0,6) {};
\end{tikzpicture}
\end{document}
答案1
我可能不明白这个问题,但文本颜色文本存储在中\tikz@textcolor
,所以你可以使用它。
\documentclass[tikz]{standalone}
%--------------------------------------------------------
% Packages
%--------------------------------------------------------
\usepackage[T1]{fontenc}
\usepackage[sfdefault]{AlegreyaSans}
\usepackage{bm} % bold math symbols
\usepackage{xcolor}
%--------------------------------------------------------
% Shapes
%--------------------------------------------------------
\makeatletter
\pgfdeclareshape{shape1}{
\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{A}{
\pgf@process{\northeast}%
\pgf@x=-1\pgf@x%
\pgf@y=.5\pgf@y%
}
\anchor{B}{
\pgf@process{\northeast}%
\pgf@x=-1\pgf@x%
\pgf@y=-.5\pgf@y%
}
% Draw the Rectangle box and the port labels
\backgroundpath{
% Rectangle box
\pgfpathrectanglecorners{\southwest}{\northeast}
% Draw port labels
\begingroup
\tikzset{mystyle/port labels} % Use font from this style
\csname pgf@anchor@shape1@A\endcsname %
\pgftext[left,at={\pgfpoint{\pgf@x}{\pgf@y}},x=\pgfshapeinnerxsep]{%
\textcolor{\tikz@textcolor}{\tikz@textfont A}}
\csname pgf@anchor@shape1@B\endcsname %
\pgftext[left,at={\pgfpoint{\pgf@x}{\pgf@y}},x=\pgfshapeinnerxsep]{%
\textcolor{\tikz@textcolor}{\tikz@textfont B}}
\endgroup
}
}
\makeatother
%--------------------------------------------------------
% Shape configuration
%--------------------------------------------------------
% Key to add font macros to the current font
\tikzset{add font/.code={\expandafter\def\expandafter\tikz@textfont\expandafter{\tikz@textfont#1}}}
% % Define default style for this node
\tikzset{mystyle/port labels/.style={font=\small,text=blue}}
\tikzset{every shape1 node/.style={draw,minimum width=2.5cm,minimum height=2cm,very thick,inner sep=1mm,outer sep=0mm,cap=round}}
%--------------------------------------------------------
% Begin Document
%--------------------------------------------------------
\begin{document}
\begin{tikzpicture}
\node[shape=shape1] (p1) at (0,0) {};
\node[shape=shape1] (p2) at (0,3) {};
\node[shape=shape1] (p3) at (0,6) {};
\end{tikzpicture}
\end{document}
答案2
这定义了一个可以作为样式传递的新键“图钉颜色”。
\documentclass[tikz]{standalone}
%--------------------------------------------------------
% Packages
%--------------------------------------------------------
\usepackage[T1]{fontenc}
\usepackage[sfdefault]{AlegreyaSans}
\usepackage{bm} % bold math symbols
\usepackage{xcolor}
%--------------------------------------------------------
% Shapes
%--------------------------------------------------------
\makeatletter
%define new keys
\pgfkeys{/tikz/pin font/.store in=\pinfont}
\pgfkeys{/tikz/pin color/.store in=\pincolor}
\pgfdeclareshape{shape1}{
\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{A}{
\pgf@process{\northeast}%
\pgf@x=-1\pgf@x%
\pgf@y=.5\pgf@y%
}
\anchor{B}{
\pgf@process{\northeast}%
\pgf@x=-1\pgf@x%
\pgf@y=-.5\pgf@y%
}
% Draw the Rectangle box and the port labels
\backgroundpath{
% Rectangle box
\pgfpathrectanglecorners{\southwest}{\northeast}
% Draw port labels
\begingroup
\ifdefined\pinfont\pinfont\fi
\ifdefined\pincolor\color{\pincolor}\fi
\csname pgf@anchor@shape1@A\endcsname %
\pgftext[left,at={\pgfpoint{\pgf@x}{\pgf@y}},x=\pgfshapeinnerxsep]{A}
\csname pgf@anchor@shape1@B\endcsname %
\pgftext[left,at={\pgfpoint{\pgf@x}{\pgf@y}},x=\pgfshapeinnerxsep]{B}
\endgroup
}
}
\makeatother
%--------------------------------------------------------
% Shape configuration
%--------------------------------------------------------
\tikzset{every shape1 node/.style={draw,minimum width=2.5cm,minimum height=2cm,pin font=\small,very thick,inner sep=1mm,outer sep=0mm,cap=round}}
%--------------------------------------------------------
% Begin Document
%--------------------------------------------------------
\begin{document}
\begin{tikzpicture}
\node[shape=shape1] (p1) at (0,0) {};
\node[shape=shape1,pin color=blue] (p2) at (0,3) {};
\node[shape=shape1] (p3) at (0,6) {};
\end{tikzpicture}
\end{document}