Circuitikz three-terminal linear regulators

circuitikz

I've been trying to draw the schematic of a linear power supply using a three-terminal linear regulator. As you can see, I have succeeded with the regulator. Sort of:

enter image description here

However, the code that I'm using seems unnecessarily complicated and I can't help but feel that there must be a better way. Here is a minimal example, take a look:

\documentclass{article}

\usepackage{circuitikz}

\begin{document}

\begin{circuitikz}
  \draw (0,0) -- (1,0) ++(1,0) node[rectangle,draw,
    minimum width=2cm,minimum height=1.2cm,
    label={[below]north:LM317},
    label={[right]west:in},
    label={[above]south:adj},
    label={[left]east:out}]{} ++(1,0) -- (4,0);
\end{circuitikz}

\end{document}

Look especially at the clumsy way the coordinates for the ++ operator are computed: In order to have the left side of the rectangle abut the line, we have to move half the width of the rectangle to the right. And similarly for the right side.

What I would ideally want is a thing that I can use like I can use all the other graphical elements in circuitikz, with proper anchors. I have looked at the circuitikz manual, but didn't find any graphical elements that looked like the one I want, and I have looked into the TikZ manual, but didn't know where to look. Can anyone help?

Edit added minimal complete example

Best Answer

If you think to reuse the circuit, I suggest looking at the manual about muxdemuxes (example here) and at subcircuits (example here).

My take would be something like this (please see the comments in the code snippet):

\documentclass{article}
\usepackage{circuitikz}
\begin{document}

% bare shape for the object.
\tikzset{regulatorshape/.style={muxdemux,
    muxdemux def={
        Lh=2, Rh=2, w=4,
        NL=1,NR=1,NT=0,NB=1,
    }
}}
% se manual section 3.4 "subcircuits"
\ctikzsubcircuitdef{lmTOS}{% TOS=three one seven, no numbers allowed here
    in, out, adj, center}{% anchors
    coordinate (#1-center)
    node [regulatorshape, anchor=center](#1-shape){} % main node
    % labels
    (#1-shape.north) node[font=\ttfamily\small, below]{LM317}
    (#1-shape.blpin 1)  node[font=\ttfamily\small, right]{in}
    (#1-shape.brpin 1)  node[font=\ttfamily\small, left]{out}
    (#1-shape.bbpin 1)  node[font=\ttfamily\small, above]{adj}
    % anchors
    (#1-shape.lpin 1) coordinate(#1-in)
    (#1-shape.bpin 1) coordinate(#1-adj)
    (#1-shape.rpin 1) coordinate(#1-out)
    % we are leaving the "current" position at the output
}

\ctikzsubcircuitactivate{lmTOS}
\begin{tikzpicture}[]
        \draw (0,0) -- ++(1,0) \lmTOS{myreg}{in} % position using anchor "-in"
            -- ++(1,0); % this work because the subcircuit
                        % "end" position is -out
        % otherwise you need
        % ... \lmTOS{myref}{in} (myreg-out) -- ++(1,0)
        \draw (myreg-adj) to[R] ++(0,-2) node[ground]{};
        % you can acces the internal anchors too:
        \draw [red, <-] (myreg-shape.brpin 1) -- ++(45:1);
\end{tikzpicture}

\end{document}

enter image description here

Related Question