我目前正在 TikZ 中创建自己的形状。我试图让多路复用器工作,但我将形状mux4to1
及其锚点之一命名为D0
。
基本上,我遇到了以下代码(MWE)的语法错误:
\documentclass[twoside,a4paper,12pt,headsepline]{scrbook}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[USenglish]{babel}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes,arrows.meta}
\makeatletter
% multiplexer shape
\pgfdeclareshape{mux4to1}{
% The 'minimum width' and 'minimum height' keys, not the content, determine
% the size
\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
}
% Inherit from rectangle
\inheritanchorborder[from=rectangle]
% Define same anchor a normal rectangle has
\anchor{center}{\pgfpointorigin}
\anchor{text}{
\pgfpointorigin
\advance\pgf@x by -.5\wd\pgfnodeparttextbox%
\advance\pgf@y by -.5\ht\pgfnodeparttextbox%
\advance\pgf@y by +.5\dp\pgfnodeparttextbox%
}
% Define anchors for signal ports
\anchor{D0}{
\northeast%
\pgf@x=.75\pgf@x%
}
%...
% Draw the box and the port labels
\backgroundpath{
%...
\begingroup
\tikzset{mux/port labels} % Use font from this style
\tikz@textfont
% ----------------------------------------------------------------
\pgf@anchor@mux4to1@D0 % <--------------------- Syntax error here
\pgftext[bottom,at={\pgfpoint{\pgf@x}{\pgf@y}},y=\pgfshapeinnerysep]{0}
% ----------------------------------------------------------------
\endgroup
}
}
% 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{mux/port labels/.style={font=\sffamily\scriptsize}}
\tikzset{every mux4to1 node/.style={draw,minimum width=4cm,minimum
height=1cm,very thick,inner sep=1mm,outer sep=0pt,cap=round,add
font=\sffamily}}
\makeatother
\begin{document}
\begin{tikzpicture}
\node[mux4to1] (mux) {};
\end{tikzpicture}
\end{document}
我明目张胆地偷了一些东西Texample:示例:D 触发器和移位寄存器来帮助我开始。
我的问题可能是一般的 LaTeX 或 TeX 错误,因为我之前注意到带有数字的命令也有问题。
理想的解决方案不应该是只需重命名形状/锚点,如果可能的话。因为mux.Dzero, mux.Done
等等都很丑陋……
答案1
改变
\pgf@anchor@mux4to1@D0 %
到
\csname pgf@anchor@mux4to1@D0\endcsname %
代码:
\documentclass[twoside,a4paper,12pt,headsepline]{scrbook}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[USenglish]{babel}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes,arrows.meta}
\makeatletter
% multiplexer shape
\pgfdeclareshape{mux4to1}{
% The 'minimum width' and 'minimum height' keys, not the content, determine
% the size
\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
}
% Inherit from rectangle
\inheritanchorborder[from=rectangle]
% Define same anchor a normal rectangle has
\anchor{center}{\pgfpointorigin}
\anchor{text}{
\pgfpointorigin
\advance\pgf@x by -.5\wd\pgfnodeparttextbox%
\advance\pgf@y by -.5\ht\pgfnodeparttextbox%
\advance\pgf@y by +.5\dp\pgfnodeparttextbox%
}
% Define anchors for signal ports
\anchor{D0}{
\northeast%
\pgf@x=.75\pgf@x%
}
%...
% Draw the box and the port labels
\backgroundpath{
%...
\begingroup
\tikzset{mux/port labels} % Use font from this style
\tikz@textfont
% ----------------------------------------------------------------
\csname pgf@anchor@mux4to1@D0\endcsname %
\pgftext[bottom,at={\pgfpoint{\pgf@x}{\pgf@y}},y=\pgfshapeinnerysep]{0}
% ----------------------------------------------------------------
\endgroup
}
}
% 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{mux/port labels/.style={font=\sffamily\scriptsize}}
\tikzset{every mux4to1 node/.style={draw,minimum width=4cm,minimum
height=1cm,very thick,inner sep=1mm,outer sep=0pt,cap=round,add
font=\sffamily}}
\makeatother
\begin{document}
\begin{tikzpicture}
\node[mux4to1] (mux) {};
\end{tikzpicture}
\end{document}