divert(-1)
  libcct.m4

* Circuit_macros Version 5.0, copyright (c) 2001 J. D. Aplevich.       *
* The files of this distribution can be redistributed or modified,     *
* provided that this copyright notice is included, and provided that   *
* modifications are clearly marked to distinguish them from this       *
* distribution.  There is no warranty whatsoever for these files.      *

                                Installation directory.  You can set this to
                                the null string if you use an environment
                                variable to tell m4 where to search:
ifdef(`HOMELIB_',,
  `define(`HOMELIB_',`')')
#  `define(`HOMELIB_',`/u/aplevich/lib/')')

                                Default pic processor: gpic.
                                To make dpic -p the default, change gpic.m4
                                to pstricks.m4.
ifdef(`gpic_',,
  `ifdef(`pstricks_',,
  `ifdef(`mfpic_',,
  `include(HOMELIB_`'gpic.m4)')')')dnl
divert(-1)

ifdef(`right_',,`include(HOMELIB_`'libgen.m4)')dnl
divert(-1)

`==============================================================================

                                THE ARGUMENTS of every circuit element are
                                optional; if omitted, the current location,
                                default length and direction are used.

===============================================================================

   With variations, 2-TERMINAL ELEMENTS are constructed as follows:

   # Draw the initial invisible line to length rp_len, and set the direction
   #   cosines:
   eleminit(`$1')

   # Element body height and width
   define(`m4v',...)define(`m4h',...)

   # Visible lines:
   { line to rvec_(rp_len/2-m4h/2,0)
     (element body lines)
     line to rvec_(rp_len/2-m4h/2,0) }

   # The invisible body block:
   {[box invis ht_ m4v wid_ m4h ] at rvec_(rp_len/2,0)}

   # The final invisible line:
   line to rvec_(rp_len,0) invis

==============================================================================

                                DEBUGGING: A statement like the following
                                inserted into a macro will print its
                                arguments for debugging:
                                print sprintf("`$0'(`$1',`$2',`$3')") ;

=============================================================================='

                                `capacitor( linespec, c|C )'
define(`capacitor',`eleminit_(`$1')
   ifelse(`$2',C,`m4Capacitor',`m4capacitor')
   {[box invis ht_ m4v wid_ m4h ] at rvec_(rp_len/2,0)}
   line to rvec_(rp_len,0) invis')

                                `straight-plate capacitor'
define(`m4capacitor',`define(`m4v',`dimen_/3')define(`m4h',`m4v*.3')dnl
   { line to rvec_(rp_len/2-m4h/2,0)
     {line from rvec_(0,-m4v/2) to rvec_(0,m4v/2)}
     move to rvec_(m4h,0)
     {line from rvec_(0,-m4v/2) to rvec_(0,m4v/2)}
     line to rvec_(rp_len/2-m4h/2,0) }')

                                `curved-plate capacitor'
define(`m4Capacitor',`define(`m4v',`dimen_/4')dnl
   define(`m4h',`m4v*.4')define(`m4cr',`dimen_*.25')dnl
   { line to rvec_(rp_len/2-m4h/2,0)
     {line from rvec_(0,-m4v/2) to rvec_(0,m4v/2)}
     {arc cw from rvec_(m4h,-m4v/2) to rvec_(m4h,m4v/2) \
         with .c at rvec_(m4h+sqrt((m4cr)^2-(m4v/2)^2),0) }
   line from last arc.c+vec_(-m4cr,0) to rvec_(rp_len/2+m4h/2,0) }')

                                `resistor( linespec,cycles )'
define(`resistor',`eleminit_(`$1')
 define(`m4n',`ifelse(`$2',,6,`eval(2*$2)')')dnl
 define(`m4v',`dimen_/12')define(`m4h',`dimen_/24')dnl
 {move to last line.c; line from last line.start to rvec_(prod_(m4n,-m4h),0) \
   m4_resist(0)dnl
   then to rvec_(prod_(m4n,m4h), 0) then to last line.end }
 {[box invis ht_ 2*m4v wid_ prod_(m4n,2*m4h)] at 2nd last line.c }
 line from 2nd last line.start to 2nd last line.end invis ')
define(`m4_resist',`ifelse(eval(`$1'<m4n),1,dnl
  `then to rvec_((`eval(2*$1+1-m4n)')*m4h, m4v) \
   then to rvec_((`eval(2*$1+3-m4n)')*m4h,-m4v) \
   m4_resist(eval($1+2))')')
                                `b_current( label ,above_|below_ ,OUT, S|E )'
                                Branch current for previous element
define(`b_current',`define(`m4h',`(rp_len-last [].wid_)/2'){dnl
  move to last line.start
  ifelse(substr(`$4',0,1),E,
    `ifelse(substr(`$3',0,1),O,
      `arrow from rvec_(rp_len-m4h,0) to rvec_(rp_len-(m4h-arrowht)*2/3,0)',
      `arrow from rvec_(rp_len,0) to rvec_(rp_len-(m4h*2/3+arrowht/3),0)')',
    `ifelse(substr(`$3',0,1),O,
      `arrow from rvec_(m4h,0) to rvec_((m4h-arrowht)*2/3,0)',
      `arrow to rvec_(m4h*2/3+arrowht/3,0)')')
  ifelse(`$1',,,
    `ifelse(substr(`$1',0,1),",`$1',"sp_$`$1'$sp_") at \
      arrowht/2/lin_leng(last arrow) between Here and last arrow.start dnl
      ifelse(`$2',,`above_',`$2')') } ')

                                `inductor( linespec,W,cycles,M )'
define(`inductor',`eleminit_(`$1')
define(`m4di',`dimen_'/10)define(`m4ct',Cos(25))define(`m4st',Sin(25))dnl
define(`m4c2t',Cos(50))define(`m4s2t',Sin(50))dnl
 define(`m4n',`ifelse(`$3',,4,`$3')')dnl
 ifelse(`$2',W,`m4Inductor(`$4')',`m4inductor(`$4')')
 {[box invis ht_ m4v+m4cs wid_ m4h] at rvec_(rp_len/2,(m4v-m4cs)/2)}
 line to rvec_(rp_len,0) invis')

                                `Wide (iron-core) inductor'
define(`m4Inductor',`define(`m4v',`(1+m4st)*m4di')dnl
 define(`m4cs',`(m4s2t-m4st)*m4di')define(`m4h',((2*m4n-2)*m4c2t+2)*m4di)dnl
 { line to rvec_(rp_len/2-((m4n-1)*m4c2t+m4ct)*m4di,0)
   arc cw from Here to rvec_(vscal_(m4di,m4ct+m4c2t,m4st-m4s2t)) \
      with .c at rvec_(vscal_(m4di,m4ct,m4st))
   for m4i=3 to m4n do { arc cw from Here to rvec_(vscal_(m4di,2*m4c2t,0)) \
      with .c at rvec_(vscal_(m4di,m4c2t,m4s2t)) }
   arc cw from Here to rvec_(vscal_(m4di,m4ct+m4c2t,m4s2t-m4st)) \
      with .c at rvec_(vscal_(m4di,m4c2t,m4s2t))
   line to rvec_(rp_len/2-((m4n-1)*m4c2t+m4ct)*m4di,0) }
 ifelse(`$1',M,`m4m_core(rvec_(rp_len/2,0),m4h,m4v,dimen_/24,dimen_/16)
   define(`m4v',`((1+m4st)*m4di+dimen_/24+dimen_/16)')') ')

                                `Narrow inductor'
define(`m4inductor',`define(`m4v',`dimen_/16')dnl
 define(`m4cs',0)define(`m4h',m4n*m4v*2)dnl
 { line to rvec_((rp_len-m4h)/2,0)
   for m4i=1 to m4n do {
     arc cw from Here to rvec_(m4v*2,0) with .c at rvec_(m4v,0)}
   line to rvec_((rp_len-m4h)/2,0)}
   ifelse(`$1',M,`m4m_core(rvec_(rp_len/2,0),m4h,m4v,dimen_/24,dimen_/16)
     define(`m4v',`dimen_/6')') ')

                                `Two lines for the metal core'
define(`m4m_core',`dnl
  {line from `$1'+vec_(-(`$2')/2,`$3'+`$4') \
          to `$1'+vec_( (`$2')/2,`$3'+`$4')
   line from last line.start+vec_(0,`$5') \
          to last line.end  +vec_(0,`$5')}')

                                `2-winding transformer( linespec, L|R, n_P )'
define(`transformer',` [
P1: Here
  inductor( ifelse(`$1',,`to rvec_(dimen_*2/3,0)',`$1'),,`$3')
P2: Here
  m4m_core(rvec_(-(rp_len/2),0),m4h,m4v,dimen_/12,dimen_/8)
S2: last line.c+vec_( rp_len/2,m4v+dimen_/12)
  inductor(from S2 to S2+vec_(-rp_len,0),,`$3')
S1: Here ; dnl                  Requires symmetry:
  ifelse(`$2',R,`M4_Tmp:S1;S1:P1;P1:M4_Tmp;M4_Tmp:S2;S2:P2;P2:M4_Tmp')
  manhattan ]')

                               `delay( linespec, width )'
define(`delay',`eleminit_(`$1')
  define(`m4v',`ifelse(`$2',,`delay_rad_*2',`($2)')')define(`m4h',`m4v*5/6')dnl
  { line to rvec_(rp_len/2-m4h/2,0)
    { line from rvec_(m4v/3,-m4v/2) to rvec_(0,-m4v/2)\
        then to rvec_(0,m4v/2) then to rvec_(m4v/3,m4v/2)
      arc cw from Here to rvec_(0,-m4v) with .c at rvec_(0,-m4v/2) }
    move to rvec_(m4h,0)
    line to rvec_(rp_len/2-m4h/2,0) }
  {[box invis ht_ m4v wid_ m4h ] at rvec_(rp_len/2,0)}
  line to rvec_(rp_len,0) invis')

                                crystal
define(`xtal',`eleminit_(`$1')
  define(`m4v',`dimen_/4')define(`m4h',`m4v*2/3')define(`m4cs',`m4v/3')dnl
  { line to rvec_(rp_len/2-m4h/2,0)
    {line from rvec_(0,-m4v/3) to rvec_(0,m4v/3)}
    { move to rvec_(m4h/2-m4cs/2,0)
      line to rvec_(0,m4v/2) then to rvec_(m4cs,m4v/2) \
        then to rvec_(m4cs,-m4v/2) then to rvec_(0,-m4v/2) then to Here }
    move to rvec_(m4h,0)
    {line from rvec_(0,-m4v/3) to rvec_(0,m4v/3)}
    line to rvec_(rp_len/2-m4h/2,0) }
  {[box invis ht_ m4v wid_ m4h ] at rvec_(rp_len/2,0)}
  line to rvec_(rp_len,0) invis')

                     `source( linespec , V | I | AC | X | string , diameter )'
define(`source',`eleminit_(`$1')
  define(`m4h',ifelse(`$3',,`sourcerad_',`($3)/2'))dnl
  define(`m4cr',`max(0,rp_len/2-m4h)')dnl
  {line to rvec_(m4cr,0)
  move to rvec_(m4h,0)
  {[circle rad m4h] at Here}
  ifelse(`$2',,,
  `$2',I, `{arrow from rvec_(-m4h*3/4,0) to rvec_(m4h*3/4,0)}',
  `$2',V, `{"$-$" at rvec_(-m4h/2,0)}
           {"$+$" at rvec_( m4h/2,0)}',
  `$2',AC,`{
    arc rad m4h/3 cw from Here-(m4h*2/3,0) to Here with .c at Here-(m4h/3,0)
    arc rad m4h/3 ccw from Here to Here+(m4h*2/3,0) with .c at Here+(m4h/3,0)}',
  `$2',X,`define(`m4v',`m4h/sqrt(2)')dnl
    {line from rvec_(-m4v,m4v) to rvec_(m4v,-m4v)}
    {line from rvec_(-m4v,-m4v) to rvec_(m4v,m4v)}',
  `{$2}' )
  line from rvec_(m4h,0) to rvec_(m4h+m4cr,0)}
  line to rvec_(rp_len,0) invis')

                                `Controlled source consource( linespec ,V|I )'
define(`consource',`eleminit_(`$1')
   {line to rvec_(rp_len/2-csdim_,0)
      {line to rvec_(csdim_,csdim_) then to rvec_(2*csdim_,0)}
      line to rvec_(csdim_,-csdim_) then to rvec_(2*csdim_,0)\
      then to rvec_(rp_len/2+csdim_,0)}
  ifelse(`$2',I,
    `{arrow from rvec_(rp_len/2-csdim_*3/4,0) \
              to rvec_(rp_len/2+csdim_*3/4,0)}',
  `$2',V,
    `{"$-$" at rvec_(rp_len/2-csdim_*.5,0)}
     {"$+$" at rvec_(rp_len/2+csdim_*.5,0)} ')
   {[box invis ht_ 2*csdim_ wid_ 2*csdim_] at rvec_(rp_len/2,0)}
   line to rvec_(rp_len,0) invis')

# ARC Mod: gpic bug again. Causes battery stem to be too far away. Add ;
#        move to rvec_(m4cs,0) } } }
                                `battery( linespec ,n )'
define(`battery',`eleminit_(`$1')
  define(`m4n',`ifelse(`$2',,1,(`$2'))')define(`m4cs',`dimen_/12')dnl
  define(`m4h',`m4cs*(m4n*2-1)')define(`m4v',`dimen_/2')dnl
  { line to rvec_(rp_len/2-m4h/2,0)
    { for m4i=1 to m4n do {
      for m4j = 1 to 2 do {
        {line from rvec_(0,m4j*m4v/4) to rvec_(0,-m4j*m4v/4)}
        move to rvec_(m4cs,0) } }; }
    line from rvec_(m4h,0) to rvec_(rp_len/2+m4h/2,0) }
  {[box invis ht_ m4v wid_ m4h] at rvec_(rp_len/2,0)}
  line to rvec_(rp_len,0) invis ')

                                `Box element ebox( linespec, wid, ht )'
define(`ebox',`eleminit_(`$1')
   define(`m4h',ifelse(`$2',,`dimen_/2',`($2)'))dnl
   define(`m4v',ifelse(`$3',,`dimen_/5',`($3)'))dnl
   {line to rvec_(max(0,rp_len/2-m4h/2),0)
      {line to rvec_(0,m4v/2) then to rvec_(m4h,m4v/2)\
      then to rvec_(m4h,-m4v/2) then to rvec_(0,-m4v/2) then to Here }
    move to rvec_(m4h,0); line to rvec_(max(0,rp_len/2-m4h/2),0)}
  {[box invis ht_ m4v wid_ m4h] at rvec_(rp_len/2,0)}
   line to rvec_(rp_len,0) invis ')

# ARC Mod: Due to a bug in gpic, fuses come out too small. Add explicit .c
#    arc cw to rvec_(m4v,0) rad m4v/2; arc ccw to rvec_(m4v,0) rad m4v/2
                                `fuse( linespec, ht )'
define(`fuse',`eleminit_(`$1')
   define(`m4v',ifelse(`$2',,`dimen_/5',`($2)'))dnl
   {line to rvec_(max(0,rp_len/2-m4v),0)
     arc  cw to rvec_(m4v,0) rad m4v/2 with .c at rvec_(m4v/2,0)
     arc ccw to rvec_(m4v,0) rad m4v/2 with .c at rvec_(m4v/2,0)
    line to rvec_(max(0,rp_len/2-m4v),0)}
  {[box invis ht_ m4v wid_ m4v*2] at rvec_(rp_len/2,0)}
   line to rvec_(rp_len,0) invis ')

                                `gap( linespec,fill )  Gap with dots e.g.
                                 gap(down_ linewid/2,1); rlabel(+,v_1,-)'
define(`gap',`eleminit_(`$1')
  dot(,,ifelse(`$2',,0,`$2')); dot(at last line.end,,ifelse(`$2',,0,`$2'))
  {[box invis ht_ 0 wid_ min(rp_len,(dimen_*4/9+rp_len)/3)] at last line.c} ')

                     			`ground( at position, T )'
define(`ground',`box invis ht 0 wid 0 with .c ifelse(`$1',,`at Here',`$1')
   {right_; ifelse(`$2',,`line from last box.c to rvec_(0,-dimen_/4)')
      {line from rvec_(-dimen_/6,0) to rvec_(dimen_/6,0)}
      {line from rvec_(-dimen_/9,-dimen_/24) to rvec_(dimen_/9,-dimen_/24)}
      line from rvec_(-dimen_/14,-dimen_/12) to rvec_(dimen_/14,-dimen_/12) }')

                                `switch( linespec,L|R,O|C,B )'
define(`switch',`ifelse(`$4',,
 `lswitch(`$1',`$2',`$3')',
 `bswitch(`$1',`$2',`$3')')')

                                `pushbutton switch'
define(`bswitch',`eleminit_(`$1') dnl
 define(`m4h',`dimen_/3') define(`m4cs',`0.069186*dimen_')dnl (2.5pt)
 define(`m4v',`ifelse(`$2',R,-m4cs,m4cs)') dnl
 {line to rvec_(rp_len/2-m4h/2,0) chop 0 chop m4cs}
 { circle rad m4cs at rvec_(rp_len/2-m4h/2,0); move to last circle
   { circle rad m4cs at rvec_(m4h,0) }
   ifelse(`$3',C,dnl
  `{ line from rvec_(-m4cs,-(m4v)) to rvec_(m4h+m4cs,-(m4v)) }
   { line from rvec_(m4h/2,-(m4v)) to rvec_(m4h/2,m4v*3) }
   {[box invis ht_ 4*m4cs wid_ m4h+2*m4cs] at rvec_(m4h/2,m4v)}',
  `{ line from rvec_(-m4cs,m4v*2.5) to rvec_(m4h+m4cs,m4v*2.5) }
   { line from rvec_(m4h/2,m4v*2.5) to rvec_(m4h/2,m4v*4.5) }
   {[box invis ht_ 5.5*m4cs wid_ m4h+2*m4cs] at rvec_(m4h/2,m4v*1.75)}')
   line from rvec_(m4h,0) to rvec_(m4h/2+rp_len/2,0) chop m4cs chop 0 }
 line to rvec_(rp_len,0) invis')

                                `knife switch'
define(`lswitch',`eleminit_(`$1') dnl
 define(`m4v',`dimen_/4')define(`m4cs',`dimen_/4*Sin(10)')dnl
 {line to rvec_(rp_len/2-dimen_/6,0)
   {line to rvec_(dimen_/4,ifelse(`$2',R,-)dimen_/4)}
   m4t1 = arrowht; m4t2 = arrowwid;
   arrowht = dimen_/.75*0.08; arrowwid = dimen_/.75*0.053
   ifelse(`$3',C,`{ arc <- ifelse(`$2',R,,`c')cw \
       from rvec_(Rect_(dimen_/4,ifelse(`$2',R,,-)15))\
       to rvec_(Rect_(dimen_/4,ifelse(`$2',R,-)60)) \
       with .c at rvec_(rect_(-dimen_/4,ifelse(`$2',R,-)(60-15)/2*dtor_)) }',
   `$3',O, `{ arc -> ifelse(`$2',R,,`c')cw \
       from rvec_(Rect_(dimen_/4,ifelse(`$2',R,,-)10))\
       to rvec_(Rect_(dimen_/4,ifelse(`$2',R,-)75)) \
       with .c at rvec_(rect_(-dimen_/4,ifelse(`$2',R,-)(75-10)/2*dtor_)) }')
   arrowht = m4t1 ; arrowwid = m4t2;
   [box invis ht_ dimen_/4+m4cs wid_ dimen_/4] \
     with .c at rvec_(dimen_/8,ifelse(`$2',R,-)(m4v-(m4cs))/2)}
 {line from rvec_(rp_len/2+dimen_/6,0) to rvec_(rp_len,0)}
 line to rvec_(rp_len,0) invis')

                                `Amplifier amp( linespec,size )'
define(`amp',`eleminit_(`$1') define(`m4h',`ifelse(`$2',,`dimen_',`($2)')')dnl
 {line to rvec_(max(0,rp_len/2-m4h/2),0)
   line from rvec_(m4h,0) to rvec_(0,m4h/2) then to rvec_(0,-m4h/2) \
     then to rvec_(m4h,0) then to rvec_(max(m4h,rp_len/2+m4h/2),0) }
 {[box invis ht_ m4h wid_ m4h] at rvec_(max(m4h,rp_len)/2,0)}
 line to rvec_(max(rp_len,m4h),0) invis')

                                `integrator( linespec,size )'
define(`integrator',`eleminit_(`$1')
 define(`m4h',`ifelse(`$2',,`dimen_',`($2)')')dnl
 {line from rvec_(m4h/4,m4h/2) to rvec_(0,m4h/2) then to rvec_(0,-m4h/2) \
   then to rvec_(m4h/4,-m4h/2) }
 {line from rvec_(m4h*5/4,0) to rvec_(m4h/4,m4h/2) then to rvec_(m4h/4,-m4h/2) \
   then to rvec_(m4h*5/4,0) then to rvec_(max(rp_len,m4h*5/4),0) }
 {[box invis ht_ m4h wid_ m4h*5/4] at rvec_(m4h*5/8,0)}
 line to rvec_(max(rp_len,m4h*5/4),0) invis')

                                `opamp( linespec, -, +, size,P )  (as a [])
                                   arg 5: power connections V1,V2'
define(`opamp',`[define(`m4h',`ifelse(`$4',,`dimen_',`($4)')')dnl
 rpoint_(ifelse(`$1',,`to rvec_(max(elen_-m4h/4,m4h),0)',`$1'))
 {line from rvec_(m4h,0) to rvec_(0,m4h/2) then to rvec_(0,-m4h/2) \
   then to rvec_(m4h,0) then to rvec_(max(m4h,rp_len),0)
 Out: Here }
 In1: rvec_(0,m4h/4)
   { ifelse(`$2',,"sp_{\small$-$}sp_",`$2') at In1 ljust_ }
 In2: rvec_(0,-m4h/4)
   { ifelse(`$3',,"sp_{\small$+$}sp_",`$3') at In2 ljust_ }
 ifelse(`$5',P,
   `{line from rvec_(m4h/2,m4h/4) to rvec_(m4h/2,m4h/4+m4h/8); V1: Here}
    {line from rvec_(m4h/2,-m4h/4) to rvec_(m4h/2,-(m4h/4+m4h/8)); V2: Here}')
 ] ')

                                `diode( linespec, B | T | Z | LE , L | R )'
define(`diode',`eleminit_(`$1')dnl
  ifelse(`$2',,`m4plain_d',
         substr(`$2',0,1),B, `m4bidirect_di',
         `$2',T, `m4plain_d(T)',
         `$2',Z, `m4plain_d(Z)',
         substr(`$2',0,2),LE, `m4plain_d
              ifelse(`$3',R,`{em_arrows(,D) at rvec_(rp_len/2-m4h,-m4v*2)}',
                            `{em_arrows at rvec_(rp_len/2-m4h,m4v*2)}') ')
 { [box invis ht_ m4v wid_ m4h] at rvec_(rp_len/2,0) }
   line to rvec_(rp_len,0) invis ')

define(`m4plain_d',`define(`m4v',`dimen_/5')define(`m4h',`sqrt(3)*m4v/2')dnl
 { m4_arrow(to rvec_(rp_len/2+m4h/2,0),m4h,m4v)
   { line from ifelse(
     `$1',T,`rvec_(-m4v/4,-m4v/2) to rvec_(0,-m4v/2) then to rvec_(0,m4v/2) \
        then to rvec_(-m4v/4,m4v/2)',
     `$1',Z,`rvec_(-m4v/4,-m4v/2) to rvec_(0,-m4v/2) then to rvec_(0,m4v/2) \
        then to rvec_( m4v/4,m4v/2)',
     `rvec_(0,-m4v/2) to rvec_(0,m4v/2)') }
   line to rvec_(rp_len/2-m4h/2,0) } ')

define(`m4bidirect_di',`define(`m4v',`dimen_/5')define(`m4h',`sqrt(3)*m4v')dnl
 { m4_arrow(to rvec_(rp_len/2,0),m4h/2,m4v)
   {m4_arrow(from rvec_(rp_len/2,0) to Here,m4h/2,m4v)}
   { line from rvec_(-m4v/4,-m4v/2) to rvec_(0,-m4v/2) then to rvec_(0,m4v/2) \
        then to rvec_(m4v/4,m4v/2) } } ')

define(`em_arrows',`[dnl        `em_arrows(L | R, U | D)'
  arrow to rvec_(ifelse(`$1',R,,-)dimen_/3,ifelse(`$2',D,-,)dimen_/3) \
    wid dimen_/9 ht dimen_/6
  move to rvec_(ifelse(`$1',R,-,)dimen_/8,ifelse(`$2',D,-,)dimen_/8)
  arrow <- to rvec_(ifelse(`$1',R,-,)dimen_/3,ifelse(`$2',D,,-)dimen_/3) \
    wid dimen_/9 ht dimen_/6]')

define(`bi_tr',`dnl             `Bipolar transistor bi_tr(linespec, L|R, P, E)'
[ define(`m4_U',dimen_/10) ifelse(`$1',,,`eleminit_($1)')
E: Here
   ifelse(`$1',,,`line to rvec_( rp_len/2-3*m4_U,0)')
   line to rvec_(m4_U*1.8,ifelse(`$2',R,-)4*m4_U)
   {arrow from last line.ifelse(`$3',P,`start to 3',`end to 1')/4 \
     between last line.start and Here wid m4_U*10/9 ht m4_U*10/6}
Bulk: line from rvec_(-m4_U*.8,0) to rvec_(m4_U*3.2,0)
   line from rvec_(-m4_U*.8,0) to rvec_(m4_U,ifelse(`$2',R,,-)4*m4_U)
   ifelse(`$1',,,`line to rvec_(rp_len/2-3*m4_U,0)')
C: Here
B: Bulk.c+vec_(0,ifelse(`$2',R,-)2.5*m4_U)
   line from B to Bulk.c
   ifelse(`$4',E,dnl
     `circle rad 4*m4_U with .c at Bulk.c+vec_(0,ifelse(`$2',R,,-)1.5*m4_U)')
  manhattan]')

define(`j_fet',`dnl             `JFET j_fet(linespec, L|R, P, E )'
[ define(`m4_U',dimen_/10) ifelse(`$1',,,`eleminit_($1)')
S: Here
   ifelse(`$1',,,`line to rvec_(rp_len/2-m4_U*2,0)')
   line to rvec_(0,ifelse(`$2',R,-)3.5*m4_U)
Bulk: line from rvec_(-m4_U*.5,0) to rvec_(m4_U*4.5,0)
   line from Bulk.end+vec_(-m4_U/2,0) \
      to Bulk.end+vec_(-m4_U/2,ifelse(`$2',R,,-)3.5*m4_U) \
      ifelse(`$1',,,`then to Bulk.c+vec_(rp_len/2,ifelse(`$2',R,,-)3.5*m4_U)')
D: Here
G: Bulk.start+vec_(m4_U/2,ifelse(`$2',R,-)3.5*m4_U)
    line from G to Bulk.start+vec_(m4_U/2,0)
    {arrow from last line.ifelse(`$3',P,`end to 1',`start to 7')/8 \
      between last line.start and last line.end wid m4_U*10/9 ht m4_U*10/6}
    ifelse(`$4',E,dnl
      `circle rad 4*m4_U with .c at Bulk.c')
   manhattan] ')

                                `Enhancement-mode FET e_fet(linespec,L|R,P,S,E)
                                   arg 3: p-channel, default n-channel
                                   arg 4: simplified diagram
                                   arg 5: envelope'
define(`e_fet',`ifelse(`$4',S,`smosfet',`mosfet')(`$1',`$2',`$3',,`$5')')
                                `Depletion-mode FET d_fet(linespec,L|R,P,S,E)
                                   arg 3: p-channel, default n-channel
                                   arg 4: simplified diagram
                                   arg 5: envelope'
define(`d_fet',`ifelse(`$4',S,`smosfet',`mosfet')(`$1',`$2',`$3',D,`$5')')

                                `mosfet(linespec,L|R,P,D,E)
                                   arg 3: p-channel, default n-channel
                                   arg 4: depletion mode, default enhancement
                                   arg 5: envelope'
define(`mosfet',`[ define(`m4_U',dimen_/10) ifelse(`$1',,,`eleminit_($1)')
S: Here
   ifelse(`$1',,,`line to rvec_(rp_len/2-m4_U*2,0)')
   {line to rvec_(m4_U*2,0); line to rvec_(0,ifelse(`$2',R,-)m4_U*3.5)
     arrow from last line.ifelse(`$3',P,end to 1,start to 4)/5 \
       between last line.start and last line.end wid m4_U*10/9 ht m4_U*10/6}
   line to rvec_(0,ifelse(`$2',R,-)3.5*m4_U); move to rvec_(-m4_U/2,0)
   ifelse(`$4',D,
     `Channel: line to rvec_(m4_U*5,0)',
     `{Channel: line invis to rvec_(m4_U*5,0)}
        line to rvec_(m4_U*3/2,0)
        move to rvec_(m4_U/2,0); line to rvec_(m4_U,0); move to rvec_(m4_U/2,0)
        line to rvec_(m4_U*3/2,0)' )
   line from Channel.end+vec_(-m4_U/2,0) to \
     Channel.end+vec_(-m4_U/2,ifelse(`$2',R,,-)3.5*m4_U)
   ifelse(`$1',,,`line to rvec_(rp_len/2-2*m4_U,0)')
D: Here
G: Channel.start+vec_(m4_U/2,ifelse(`$2',R,-)3.5*m4_U)
   line from G to Channel.start+vec_(m4_U/2,ifelse(`$2',R,-)m4_U) \
          then to Channel.start+vec_(m4_U*9/2,ifelse(`$2',R,-)m4_U)
   ifelse(`$5',E,`circle rad 4*m4_U with .c at Channel.c')
   manhattan] ')

                                `Simplified diagram smosfet(linespec,L|R,P,D,E)
                                   arg 3: p-channel, default n-channel
                                   arg 4: depletion mode, default enhancement'
                                   arg 5: envelope'
define(`smosfet',`[ define(`m4_U',dimen_/10) ifelse(`$1',,,`eleminit_($1)')
S: Here
   ifelse(`$1',,,`line to rvec_(rp_len/2-m4_U*2,0)')
   line to rvec_(0,ifelse(`$2',R,-)3.5*m4_U)
   {arrow from last line.ifelse(`$3',P,start to 4,end to 1)/5 \
     between last line.start and last line.end wid m4_U*10/9 ht m4_U*10/6}
   ifelse(`$4',D,`{move to rvec_(0,ifelse(`$2',R,,-)linethick*3/2pt__)
     line thick 2*linethick to rvec_(m4_U*4,0)}')
Channel: line from rvec_(-m4_U/2,0) to rvec_(m4_U*9/2,0)
   line from Channel.end+vec_(-m4_U/2,0) to \
     Channel.end+vec_(-m4_U/2,ifelse(`$2',R,,-)3.5*m4_U)
   ifelse(`$1',,,`line to rvec_(rp_len/2-2*m4_U,0)')
D: Here
G: Channel.c+vec_(0,ifelse(`$2',R,-)ifelse(`$5',E,4,3.5)*m4_U)
   line from G to Channel.c+vec_(0,ifelse(`$2',R,-)m4_U)
   line from rvec_(-m4_U*3/2,0) to rvec_(m4_U*3/2,0)
   ifelse(`$5',E,`circle rad 4*m4_U with .c at Channel.c')
   manhattan] ')

                                element labels to the left, right, centre of
                                the current direction
define(`rlabel',`m4label(`$1',`$2',`$3',.s_,below_)')
define(`llabel',`m4label(`$1',`$2',`$3',.n_,above_)')
define(`clabel',`m4label(`$1',`$2',`$3',,)')
                   labels at centre and both ends of an element `dimen_' long
define(`m4label',`dnl
 ifelse(`$1',,,`{"sp_$`$1'$sp_" at last [].w_ $5 rjust_};')dnl
 ifelse(`$2',,,`{"sp_$`$2'$sp_" at last []$4 $5};')dnl
 ifelse(`$3',,,`{"sp_$`$3'$sp_" at last [].e_ $5 ljust_};')dnl
 ')
                                oblique element label
                                `dlabel(long,lateral,label,label,label)'
define(`dlabel',`dnl
 ifelse(`$3',,,`{"$`$3'$" at last [].c+vec_(-(`$1'),`$2')};')dnl
 ifelse(`$4',,,`{"$`$4'$" at last [].c+vec_(0,`$2')};')dnl
 ifelse(`$5',,,`{"$`$5'$" at last [].c+vec_(`$1',`$2')};')
 ')
                                `eleminit_( linespec )'
                                compute element direction and length
define(`eleminit_',`rpoint_(ifelse(`$1',,`to rvec_(elen_,0)',`$1')) ')

                                `par_( top element, bottom element, height )'
                                Top & bottem elements must have the same length
define(`par_',`[Start: Here
   line from Start + vec_(0,($3)/2) to Start + vec_(0,-($3)/2)
   move to Start + vec_(0,($3)/2);  $1
   move to Start + vec_(0,-($3)/2); $2
   line to rvec_(0,$3)
   End: last line.c] with .Start at Here
   move to last [].End')
                                `Line hopping over named lines,
                                diverting left or right:
                                crossover(linespec,L|R,line_name,line_name,...)'
define(`hoprad_',`dimen_/12')
define(`crossover',`eleminit_(`$1')dnl
  M4_Tmp: last line.end
  m4_xover_(shift($@))dnl
  line to M4_Tmp ')
                                `internal to crossover:'
define(`m4_xover_',`ifelse(`$2',,,`line to \
  intersect_(last line.start,M4_Tmp,`$2'.start,`$2'.end) chop 0 chop hoprad_
  arc ifelse(`$1',R,c)cw to rvec_(2*hoprad_,0) with .c at rvec_(hoprad_,0)
  m4_xover_(`$1',shift(shift($@)))')')

				Size parameters: they must all be terms (products), i.e.
                                a sum would have to be parenthesized
define(`dimen_',`linewid')
                                Source element default radius
define(`sourcerad_',`0.25*dimen_')
                                Controlled Source width/2
define(`csdim_',`dimen_*0.3')
                                Element length:
define(`elen_',`dimen_*3/2')
                                Delay elements:
define(`delay_rad_',`0.35*dimen_')
                                Redefine dot size for circuits
define(`dotrad_',`0.04*dimen_')
right_
                                Initialize global variables:
define(`cct_init',`dnl
`#' `$0': ifdef(`gpic_',`Gpic',
  `ifdef(`pstricks_',`PSTricks',
  `ifdef(`mfpic_',`mfpic',`default')')') m4 macro settings detected.
gen_init dnl
dnl                             Customizations can be put here
')
divert(0)dnl
psset_(arrowsize=1.2pt 4`,'arrowlength=1.64`,'arrowinset=0)dnl
