6

I'd like to draw a user defined a simple node shape in combination with the circuit library. The node has one input and one output. I'd like to put text inside the node shape near the input and output.

I couldn't find an example in the TikZ manual that describes how to put text inside the node shapes (except for the info label).

There are two problems.

  • The circuit library fills the node and this overwrites the text. I managed to turn that off with a style. I'm not sure if this is the best thing to do.

  • I can't figure out how to position the text. It always ends up in the centre of the node shape.

The following is what I've got:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{circuits.logic.CDH}
\tikzset{circuit logic CDH}
\usetikzlibrary{shapes.misc}
\makeatletter
\pgfdeclareshape{myshapeshape} {
  \inheritsavedanchors[from=rectangle] 
  \inheritanchorborder[from=rectangle]
  \inheritanchor[from=rectangle]{north}
  \inheritanchor[from=rectangle]{north west}
  \inheritanchor[from=rectangle]{north east}
  \inheritanchor[from=rectangle]{center}
  \inheritanchor[from=rectangle]{west}
  \inheritanchor[from=rectangle]{east}
  \inheritanchor[from=rectangle]{mid}
  \inheritanchor[from=rectangle]{mid west}
  \inheritanchor[from=rectangle]{mid east}
  \inheritanchor[from=rectangle]{base}
  \inheritanchor[from=rectangle]{base west}
  \inheritanchor[from=rectangle]{base east}
  \inheritanchor[from=rectangle]{south}
  \inheritanchor[from=rectangle]{south west}
  \inheritanchor[from=rectangle]{south east}
  \inheritbackgroundpath[from=rectangle]
  \anchor{1}{
    \pgf@process{\northeast}%
    \pgf@xa=\pgf@x%
    \pgf@process{\southwest}%
    \advance\pgf@x by \pgf@xa%
    \pgf@x=0.5\pgf@x%
  }
  \anchor{2}{
    \pgf@process{\southwest}%
    \pgf@xa=\pgf@x%
    \pgf@process{\northeast}%
    \advance\pgf@x by \pgf@xa%
    \pgf@x=0.5\pgf@x%
  }
  % attempt to put in text.
  \beforebackgroundpath{%
      \pgf@process{\northeast}%
      \pgf@xa=\pgf@x%
      \pgf@process{\southwest}%
      \advance\pgf@x by \pgf@xa%
      \pgf@x=0.5\pgf@x%
      \pgftext{\textsc{1}}
  }%
}
\tikzset{circuit declare symbol=myshapeg,
         set myshapeg graphic={draw,
                              fill=none,
                              shape=myshapeshape,%
                              minimum width=10mm,
                              minimum height=20mm}}
\tikzset{myshape/.style={myshapeg,info=center:{#1}},
         myshape/.default={null}}
\makeatother

\begin{document}

\begin{tikzpicture}
\draw (0,0) node(inout)[myshape=text] {}
      (inout.1) -- + (0,-1)
      (inout.2) -- + (0,1)
;
\end{tikzpicture}

\end{document}

example of node shape

Any help much appreciated.

7
  • 1
    You need to include the center and text anchors. The text anchor locates the lower left hand corner of the text. The big question is, do you want the shape to expand to fit the text? If not, you can overwrite an existing circuit element with one or more nodes located next to the in and out anchor points. Nov 22, 2013 at 22:01
  • 1
    I’d use a simple rectangular shape from the circuits (a resistor from the IEC sub-library?) and add the text with append after command (i.e. labels on the inside). (This surely can also done on a lower level with PGF.) Why are using a rectangle definition? The circuits library stuff already offers anchors like .input and .output Nov 23, 2013 at 4:26
  • 1
    Correction: \center and \text are saved anchors and were inherited. Now if you really want to do this as a node, the Tikz sty file is not a good style guide (uses TeX registers for everything). Take a look at elfsoft2000.com/projects/multipole.pdf Nov 23, 2013 at 13:10
  • My correction was incorrect. center and text are normal anchors. Nov 23, 2013 at 13:37
  • 1
    For your second problem, try \pgftext[bottom,at={\pgfpoint{\pgf@x}{\pgf@y}}]{\textsc{1}} Dec 23, 2013 at 0:09

1 Answer 1

1
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{circuits.logic.CDH}

\begin{document}

\begin{tikzpicture}
\draw (0,0) node(inout)[rectangle,
            draw=black,
            minimum width=10mm,
            minimum height=20mm]{text}
      (inout.south) -- + (0,-1)
      (inout.north) -- + (0,1)
      (inout.south) node[above]{1}
      (inout.north) node[below]{2}
;
\end{tikzpicture}

\end{document}

enter image description here

1
  • Thanks but it's not what I need. What I need is code to insert the text in the code that defines the node shape.
    – user10274
    Nov 25, 2013 at 9:33

You must log in to answer this question.