2

I'm currently creating my own shapes in TikZ. I'm trying to get a multiplexer to work, but I named the shape mux4to1 and one of its anchors D0.

Basically, I'm getting a syntax error with the following code (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}

I blatantly stole some stuff from Texample:Example: D flip-flops and shift register to help me get started.

My problem might be a general LaTeX or TeX error, as I noticed before that commands with numbers in them were problematic as well.

Ideally, a solution should not be to just rename the shape/anchor, if at all possible. Because mux.Dzero, mux.Done and so on are ugly...

3

Change

    \pgf@anchor@mux4to1@D0 %

to

    \csname pgf@anchor@mux4to1@D0\endcsname %

The code:

\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}
2
  • Thanks for your answer. The syntax error goes away. However, so does the desired effect of moving \pgf@x/\pgf@y to the location of the anchor. See imgur.com/vxnc9qr The bottom left corner is where I moved my path to before \begingroup.
    – FRob
    Jan 23 '15 at 21:42
  • Argh, never mind. I was subconsciously pretty-printing that to \csname pgf@anchor@mux4to1@D0 \endcsname -- notice the space -- which is obviously another command than without the space before \endcsname.
    – FRob
    Jan 23 '15 at 22:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.