4

I try to make a new command to avoid some typing. Don't the arguments of newcommand work within $ $? Or did I something else wrong? Thanks for your help.

I get the error:

Package pgfkeys Error: I do not know the key '/tikz/R=$R_{1}$,i=$I_{1}$,v=$U_{1}$' and I am going to ignore it. Perhaps you misspelled it. \draw (0,0) to [\Res{1}] (3,0)

Here is my minimal not working example:

\documentclass[11pt,a4paper]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{circuitikz}

\begin{document}
    \newcommand{\Res}[1]{R=$R_{#1}$, i=$I_{#1}$, v=$U_{#1}$}

    \begin{circuitikz}

        \draw (0,0) to [\Res{1}] (3,0) ;

    \end{circuitikz}
\end{document}
  • often when parsing special syntax such as tikz or comma separated keys in eg \includegraphics[width=zz, ... tex does not expand macros, so it is not specific to $ it needs to "see" the comma separated list of pgf keys directly – David Carlisle Feb 28 '18 at 19:46
  • @ David Carlisle: does this mean, I can't use newcommand with macros like $$ at all ? Or ist there a different aproach to handle it? – Torsten Feb 28 '18 at 19:56
  • as I said it is nothing to do with $ (the normal use of $ to enter math mode is fine inside a defined command) the tikz command needs a comma separated list of keys you could for example add the \draw(0,0) to part to your definition so the macro expands first then the tikz parser starts, – David Carlisle Feb 28 '18 at 20:06
6

You can (and should) define a style:

\documentclass[11pt,a4paper]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{circuitikz}

\tikzset{
  Res/.style={R=$R_{#1}$, i=$I_{#1}$, v=$U_{#1}$},
}

\begin{document}

\begin{circuitikz}
\draw (0,0) to [Res=1] (3,0) ;
\end{circuitikz}

\end{document}

enter image description here

4

You need the macro to expand before the tikz parser needs to see the comma separated values.

One way would be

enter image description here

\documentclass[11pt,a4paper]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{circuitikz}

\begin{document}
    \newcommand{\Res}[2]{\draw #1 to [R=$R_{#2}$, i=$I_{#2}$, v=$U_{#2}$]}

    \begin{circuitikz}

        \Res{(0,0)}{1} (3,0) ;

    \end{circuitikz}
\end{document}

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.