3

I'm trying to figure out a way to position labels inside european resistors.

I'm aware of the answer here, but I think the solution is a bit too complicated to use.

I have figured out the following methods:

\documentclass{article}
\usepackage{tikz}
\usepackage[european]{circuitikz}

\begin{document}

\begin{circuitikz}
    \tikzset{R/.append style={color=red, label distance=-13pt, label/align = rotate}}

    \def\DIR{0,45,90,135,180,-90,-45,-135}
    \foreach \i in \DIR {
        \draw (0,0) to[R=$\i$, *-o] (\i:2.5);
    }
\end{circuitikz}
\begin{circuitikz}
    \newcommand{\vc}[1]{$\raisebox{-2.5pt}{#1}$}
    \tikzset{R/.append style={color=red, label distance=-10.5pt, label/align = rotate}}

    \def\DIR{0,45,90,135,180,-90,-45,-135}
    \foreach \i in \DIR {
        \draw (0,0) to[R=\vc{$\i$}, *-o] (\i:2.5);
    }
\end{circuitikz}

\end{document}

circuitikz european resistor label example

The the syntax in the first example requires no modifications of the actual resistor code, but it only works on resistors that go from left to right.

The second one fixes this, but requires wrapping the label in \vc{}.

I would like to figure out a way to use the simple syntax of the first example but have the placement work correctly for all resistors. What possibilities are there to achieve this?

Is there a way to automatically put \vc{} or some other command around every resistor label? I have read about hooks in the CircuiTikZ manual, but I haven't found anything that would work for this.

Or would it be possible to write a hook that is called before the resistor is drawn and that would check the resistor orientation and then calculate the appropriate label distance for each resistor?

It seems to me that the problem has to do with text baseline. I understand that when CircuiTikZ positions resistor labels above or below the resistor, the distance from the resistor to label text baseline is different even though the distance between resistor and label appears to be the same regardless of whether the label is above or below the resistor. Would it be possible to adjust the baseline position so that the baseline would run throught the middle of the characters and not at the bottom?

CircuiTikZ manual describes a method for defining custom components. Would it be possible to implement a custom component that would draw a resistor with the label wrapped in \raisebox? (Of course I could just duplicate the original code for the resistor and make this change, but I think that's way too clumsy. I'm wondering if it is possible to define a custom component that only makes this small change and then calls the original code for drawing the resistor.)

I would also welcome any other ideas about how to achieve this goal.

3
  • 1
    I can't test now, but what about a new key at TikZ level? Like \tikzset{RV/.style={R=\vc{#1}}}? (Unchecked, I'm on a phone...)
    – Rmano
    Nov 20, 2023 at 11:48
  • @Rmano, this is exactly what I was looking for. I tested it and it works. If you post this as an answer, I will accept it.
    – ttsc
    Nov 21, 2023 at 17:24
  • 1
    I added it at the top of my answer.
    – Rmano
    Nov 21, 2023 at 19:27

1 Answer 1

2

If you want just a way to apply you macro automatically, you can define a key at TikZ level:

\tikzset{RV/.style={R=\vc{#1}}}

...and then use Rv=... instead of R=....

On the other hand it seems to me that what you want is a label in the center of a european-style resistor, similar, for example, to this question. This is not provided because it would not work for almost every other component (think a capacitor). But you can "fake" it quite easily (and independently from the angle) naming the component and using a simple macro):

\documentclass{article}
\usepackage{a4wide}
\usepackage[european]{circuitikz}
\newcommand\centerlabel[2]{%
    \node[rotate=\ctikzgetdirection{#1}] at (#1.center) {#2};
}

\begin{document}
\begin{circuitikz}[]
    \def\DIR{0,45,90,135,180,-90,-45,-135}
    \foreach \i in \DIR {
        \draw (0,0) to[R, name=R\i, color=red, *-o] (\i:3);
        \centerlabel{R\i}{\i}
    }
\end{circuitikz}
\begin{circuitikz}[]
    \foreach \i in {0,27,...,340} {
        \draw (0,0) to[R, name=R\i, color=red, *-o] (\i:3);
        \centerlabel{R\i}{\i}
    }
\end{circuitikz}
\end{document}

enter image description here

In this case, the label just follows the angle of the component. But you can use the number form \ctikzgetdirection and use a bit of math to choose the angle you want; for example:


\documentclass{article}
\usepackage{a4wide}
\usepackage[european]{circuitikz}
\newcommand\centerlabel[2]{%
    \pgfmathsetmacro{\dipolerot}{\ctikzgetdirection{#1}}
    \pgfmathsetmacro{\myrot}{\dipolerot<90?  \dipolerot : 
        (\dipolerot>270 ? \dipolerot: \dipolerot-180)}
    \node[rotate=\myrot] at (#1.center) {#2};
}

\begin{document}
\begin{circuitikz}[]
    \def\DIR{0,45,90,135,180,-90,-45,-135}
    \foreach \i in \DIR {
        \draw (0,0) to[R, name=R\i, color=red, *-o] (\i:3);
        \centerlabel{R\i}{\i}
    }
\end{circuitikz}
\begin{circuitikz}[]
    \foreach \i in {0,27,...,340} {
        \draw (0,0) to[R, name=R\i, color=red, *-o] (\i:3);
        \centerlabel{R\i}{\i}
    }
\end{circuitikz}
\end{document}

enter image description here

You must log in to answer this question.

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