1

How to globally customize these switches (create a \newcommand or custom environment)

\documentclass[]{standalone}
    \usepackage{amsmath}
    \usepackage{tikz}
    \usepackage{circuitikz}

    \begin{document}

    \begin{circuitikz} 
        \draw (0,0) node[spdt, xscale=-1] (Sw) {} (Sw.in) node[right] {in} (Sw.out 1) node[left] {out 1} (Sw.out 2) node[left] {out 2};
    \end{circuitikz} %yscale = -1

    \begin{circuitikz} 
        \draw (0,0) node[spdt] (Sw) {} (Sw.in) node[left] {in} (Sw.out 1) node[right] {out 1} (Sw.out 2) node[right] {out 2};
    \end{circuitikz}


    \end{document}

enter image description here

to have box around like this?

enter image description here

Edit: even reference to manual would be enough

3

2 Answers 2

1

It does seem that you have copied the code without understanding the concept

    \draw (0,0) node[spdt, xscale=-1] (Sw) [red]{a}

(0,0) is the coordinate of the node, initially ypu have not given a name {}

I gave it a name {a} which is reversed bcause of the xscale=-1(same as mirror)

    \draw [red, thick] (Sw.in) -- (Sw.out 2);

simply draws a line from the node you have named (Sw.in) to (Sw.out 2)

enter image description here

What you want is below

enter image description here

The MWE

\documentclass[]{standalone}
    \usepackage{amsmath}
    \usepackage{tikz}
    \usepackage{circuitikz}

    \begin{document}

    \begin{circuitikz} 
        \draw (0,0) node[spdt, xscale=-1] (Sw) {} 
        (Sw.in) node[right] {in} 
        (Sw.out 1) node[left] {out 1} 
        (Sw.out 2) node[left] {out 2};
        \draw [red, thick] (-0.4,-0.4) rectangle (0.4,0.4);%<-----added rectangle
    \end{circuitikz} %yscale = -1

    \begin{circuitikz} 
        \draw (0,0) node[spdt] (Sw) {} 
        (Sw.in) node[left] {in} 
        (Sw.out 1) node[right] {out 1} 
        (Sw.out 2) node[right] {out 2};
    \end{circuitikz}

    \end{document}

EDIT

Add in a

\newcommand environ

the MWE

\documentclass{article}
\usepackage{circuitikz}

\begin{document}
\newcommand\boxspdt{%
    \begin{circuitikz} 
        \draw (0,0) node[spdt, xscale=-1] (Sw) {} 
        (Sw.in) node[right] {in} 
        (Sw.out 1) node[left] {out 1} 
        (Sw.out 2) node[left] {out 2};
        \draw [red, thick] (-0.4,-0.4) rectangle (0.4,0.4);
    \end{circuitikz} %yscale = -1
   }

\boxspdt
\end{document}

Compiling

    \boxspdt

will give

enter image description here

PS You should be able to take the

\begin{circuitikz}...\end{circuitikz} 

out of the definition above; then, your source would have

\begin{circuitikz}
\boxspdt
 :
 : 
<optional other components can be added here other code here>
 :
 :
 \end{circuitikz}

(You might also want to add a way to name your spdt('s) in that case. Perhaps an optional argument.)

4
  • 1
    I am sorry, but this is not the solution I need. Instead of drawing rectangle each time I draw switch I would like to define in preamble (or similar) my custom switch named 'boxed spdt' with box around to use it globally like \draw (0,0) node[boxed spdt, xscale=-1] (Sw) {} in document
    – struct
    Feb 22, 2020 at 8:09
  • so enclose the above definition in an environment \boxspdt
    – js bibra
    Feb 22, 2020 at 8:22
  • 1
    Would you be kind enough to complete it into your answear please?
    – struct
    Feb 22, 2020 at 9:00
  • please see the edit above
    – js bibra
    Feb 22, 2020 at 13:08
2

One can use the other anchors of node (Sw) to draw a box around it, although it will pass through the in and out anchors. Since the calc tikzlibrary is already loaded, it is relatively easy to add offsets.

Note that [xscale=-1] reverses west and east anchors.

\documentclass[]{standalone}
    \usepackage{amsmath}
    \usepackage{circuitikz}

    \begin{document}

    \begin{circuitikz} 
        \draw (0,0) node[spdt, xscale=-1] (Sw1) {} (Sw1.in) node[right] {in} (Sw1.out 1) node[left] {out 1} (Sw1.out 2) node[left] {out 2};
        \draw[red] ($(Sw1.south east)+(4pt,-3pt)$)  rectangle ($(Sw1.north west)+(-4pt,3pt)$);
        \draw (3,0) node[spdt] (Sw2) {} (Sw2.in) node[left] {in} (Sw2.out 1) node[right] {out 1} (Sw2.out 2) node[right] {out 2};
        \draw[red] ($(Sw2.south west)+(4pt,-3pt)$)  rectangle ($(Sw2.north east)+(-4pt,3pt)$);
    \end{circuitikz}

\end{document}

demo

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .