15

I'm a very beginner in using circuitikz in my tex documents.

I'd like to obtain a label that shows both the part name and the component value. Looking in this forum I found this solution:

\documentclass{article}
\usepackage[siunitx]{circuitikz}
\begin{document}
\begin{circuitikz}
\draw (0,0) to  [R, l_={\parbox{1cm}{\centering R$_1$\\\SI{510}{\ohm}}}] (2,0);
\end{circuitikz}
\end{document}

The output is good but I'm not very satisfied by the code.

Is there any simpler/easier solution?

  • 1
    Welcome to TeX.SE. One way would be to define \newcommand*{\MyLabel}[3][1cm]{\parbox{#1}{\centering #2 \\ #3}}, and then use it as ` l_=\MyLabel{$R_1$}{\SI{510}{\ohm}}`. – Peter Grill Aug 1 '12 at 18:25
  • Thank you for your answer! This is a good and simple solution! – bersanri Aug 4 '12 at 8:26
  • @PeterGrill Following your suggestion I defined this \newcommand*{\tlab}[1]{\parbox{1cm}[b]{\centering #1}} for labels above the element, however [b] specification makes problem. Any advice? – Pygmalion Feb 4 '18 at 18:49
  • @Pygmalion: In general just post a new question with fully compilable MWE including \documentclass and the appropriate packages that reproduces the problem, especially on such an old comment. But, I think your issue is that that optional parameter [b] needs to be before the width specification. So you want \parbox[b]{}... – Peter Grill Feb 5 '18 at 5:42
15

Here I provide a different solution from the one suggested by Peter.

Basically, I define a new kind of label lx that accepts two commands: the first one is the component name (for example R_1, C_1) and the second is dedicated to the component value. Its behaviour is similar to the standard l, therefore the followings:

  • lx_=...
  • lx^=...

hold. In conclusion, the command should be used as:

lx_={component_name and component_value}

For instance:

lx^={C$_1$ and \SI{1}{\farad}}

Here is the complete example:

\documentclass{article}
\usepackage[siunitx]{circuitikz}

\makeatletter
\ctikzset{lx/.code args={#1 and #2}{ 
  \pgfkeys{/tikz/circuitikz/bipole/label/name=\parbox{1cm}{\centering #1  \\ #2}}
    \ctikzsetvalof{bipole/label/unit}{}
    \ifpgf@circ@siunitx 
        \pgf@circ@handleSI{#2}
        \ifpgf@circ@siunitx@res 
            \edef\pgf@temp{\pgf@circ@handleSI@val}
            \pgfkeyslet{/tikz/circuitikz/bipole/label/name}{\pgf@temp}
            \edef\pgf@temp{\pgf@circ@handleSI@unit}
            \pgfkeyslet{/tikz/circuitikz/bipole/label/unit}{\pgf@temp}
        \else
        \fi
    \else
    \fi
}}

\ctikzset{lx^/.style args={#1 and #2}{ 
    lx=#2 and #1,
    \circuitikzbasekey/bipole/label/position=90 } 
}

\ctikzset{lx_/.style args={#1 and #2}{ 
    lx=#1 and #2,
    \circuitikzbasekey/bipole/label/position=-90 } 
}
\makeatother

\begin{document}
\begin{circuitikz}
\draw (0,-3) to  [R, lx_={R$_1$ and \SI{510}{\ohm}}] (2,-3);
\draw (6,-3) to  [C, lx^={C$_1$ and \SI{1}{\farad}}] (8,-3);
\end{circuitikz}
\end{document}

Result:

enter image description here

  • This is what I was looking for! I probably will use lx=#1 and #2, even with then lx^ command. Thank you! – bersanri Aug 4 '12 at 8:24
  • Glad of having helped you. For lx^ I personally prefer this solution, but it's just an opinion. :) – Claudio Fiandrino Aug 4 '12 at 11:55
  • @ClaudioFiandrino --- can I use your code to pull an update to circuitikz? Thanks! – Rmano Nov 4 '18 at 19:59
  • @Rmano: sure, feel free. Please, just quote this as the original source. – Claudio Fiandrino Nov 5 '18 at 12:35
  • Done, see github.com/circuitikz/circuitikz/pull/127 – Rmano Nov 8 '18 at 21:25
0

I know that this answer will give me one of that creepy badges... ;-) but we are in Halloween, so why not?

I did a more flexible version of Claudio's ctikz-lx (to fix some alignment problem in case of long labels) and I think it's my duty to share it. I called it ctikz-ly.

The packages is here (manual in comments):

\NeedsTeXFormat{LaTeX2e}[1996/06/01]
\ProvidesPackage{ctikz-ly}[2018/10/25 stacked labels for circuitikz]
%
% stacked labels by Romano Giannetti romano@rgtti.com
% heavily based on Claudo Fiandrinos's https://tex.stackexchange.com/a/65792/38080
% \expandafter trick inspired by Matthew Leingang's https://tex.stackexchange.com/a/12272/38080
%
% labels are in a tabular, globally aligned:
%        vertically with key ly valign (default c)
%        c: center t: top b: bottom
%        horizontally with key lx align (default l)
%        l: left c: centered r: right
% you can switch sides using ly_=... and ly^=...
% syntax is ly_ = line1 and line2 (same for ly^)
%
\ctikzset{%
    ly valign/.store in=\ly@valign, ly valign=c,
    ly halign/.store in=\ly@halign, ly halign=l,
}
\ctikzset{ly/.code n args={2}{
  \pgfkeys{/tikz/circuitikz/bipole/label/name=%
        \bgroup
        \setlength{\tabcolsep}{2pt}%
        \def\ly@tabu{\tabular[\ly@valign]}%
        \expandafter\ly@tabu\expandafter{\ly@halign}%
        #1\\ #2%
        \endtabular
        \egroup
    }%
    \ctikzsetvalof{bipole/label/unit}{}
    \ifpgf@circ@siunitx
        \pgf@circ@handleSI{#2}
        \ifpgf@circ@siunitx@res
            \edef\pgf@temp{\pgf@circ@handleSI@val}
            \pgfkeyslet{/tikz/circuitikz/bipole/label/name}{\pgf@temp}
            \edef\pgf@temp{\pgf@circ@handleSI@unit}
            \pgfkeyslet{/tikz/circuitikz/bipole/label/unit}{\pgf@temp}
        \else
        \fi
    \else
    \fi
}}
\ctikzset{ly^/.style args={#1 and #2}{
        ly={#1}{#2},
    \circuitikzbasekey/bipole/label/position=90 }
}
\ctikzset{ly_/.style args={#1 and #2}{
        ly={#1}{#2},
    \circuitikzbasekey/bipole/label/position=-90 }
}
\endinput

and a MWE using it, showing several combinations:

\documentclass[border=10pt]{standalone}
\usepackage[siunitx, oldvoltagedirection]{circuitikzgit}
\usepackage{ctikz-ly}
\ctikzset{tripoles/mos style/arrows, bipoles/thickness=1, }
\makeatletter
\makeatother
\begin{document}
\begin{circuitikz}[ american, ]
    %
    % default is ly halign=l, ly valign=c 
    %
    \begin{scope}[color=blue]
        \draw (0,0) to[R, ly_=$R_{CC}$ and \SI{4.7}{k\ohm},            , ly valign=t] (3,0);
        \draw (0,0) to[R, ly_=$R_{CC}$ and \SI{4.7}{k\ohm},            ,            ] (0,3);
        \draw (0,0) to[R, ly_=$R_{CC}$ and \SI{4.7}{k\ohm}, ly halign=c, ly valign=b] (-3,0);
        \draw (0,0) to[R, ly_=$R_{CC}$ and \SI{4.7}{k\ohm}, ly halign=r, ly valign=c] (0, -3);
    \end{scope}
    \begin{scope}[xshift=6cm, color=red]
        \draw (0,0) to[R, ly^=$R_{CC}$ and \SI{4.7}{k\ohm}, ly halign=c, ly valign=b] (3,0);
        \draw (0,0) to[R, ly^=$R_{CC}$ and \SI{4.7}{k\ohm}, ly halign=c,            ] (0,3);
        \draw (0,0) to[R, ly^=$R_{CC}$ and \SI{4.7}{k\ohm},            , ly valign=t] (-3,0);
        \draw (0,0) to[R, ly^=$R_{CC}$ and \SI{4.7}{k\ohm}, ly halign=c, ly valign=t](0, -3);
    \end{scope}
\end{circuitikz}
\end{document}

which results in:

labels, labels, labels...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.