# Redefining a variable inside a newcommand

I have the following code:

\documentclass{article}

\usepackage{tikz}
\usepackage{graphicx}

\newlength{\cm}
\setlength{\cm}{1cm}
\newcommand\scalefactor{2}

\newcommand{\bintree}[5]{
\protect{\renewcommand\scalefactor{#5}}
\resizebox{\scalefactor\cm}{!}{%
\begin{tikzpicture}%
\draw (0,0)--(0,.5);%
\draw (0,.5)--(-.5,1);%
\draw (0,.5)--(.5,1);%

\node[below] at (0,0){#3};%
\node[above] at (-.5,1){#1};%
\node[above] at (.5,1){#2};%
\node[right] at (0,.4){#4};%
\end{tikzpicture}}}

\begin{document}

\bintree{$s$}{$s$}{$s$}{$*$}{1}
\bintree{$s$}{$s$}{$s$}{$*$}{3}

\end{document}


Unfortunately, it doesn't seem to work (I don't get the rescaling I would like). Here's what I obtain:

and nothing in the log. How can I fix the problem?

• the posted code does not give the error you state, so presumably it is from some code you do not show, Please post an example that demonstrates the problem – David Carlisle Dec 5 '15 at 22:43
• @DavidCarlisle Wait, I'll edit my question. – Daniel Robert-Nicoud Dec 5 '15 at 22:46
• @DavidCarlisle There, done. Actually now I have a completely different problem. I have no idea where the error came from... – Daniel Robert-Nicoud Dec 5 '15 at 22:48
• @DanielRobert-Nicoud: Fragments of code are not really useful, in my point of view – user31729 Dec 5 '15 at 22:48
• @ChristianHupfer This is my whole macro for now. I run it in a document simply by calling \bintree{}{}{}{}{3} (for example). – Daniel Robert-Nicoud Dec 5 '15 at 22:51

## 1 Answer

You had done the redefinition inside a group so you could do

\documentclass{article}

\usepackage{tikz}
\usepackage{graphicx}

\newlength{\cm}
\setlength{\cm}{1cm}
\newcommand\scalefactor{2}

\newcommand{\bintree}[5]{%
\renewcommand\scalefactor{#5}%
\resizebox{\scalefactor\cm}{!}{%
\begin{tikzpicture}%
\draw (0,0)--(0,.5);%
\draw (0,.5)--(-.5,1);%
\draw (0,.5)--(.5,1);%

\node[below] at (0,0){#3};%
\node[above] at (-.5,1){#1};%
\node[above] at (.5,1){#2};%
\node[right] at (0,.4){#4};%
\end{tikzpicture}}}

\begin{document}

\bintree{$s$}{$s$}{$s$}{$*$}{1}
\bintree{$s$}{$s$}{$s$}{$*$}{3}

\end{document}


although you didn't need \cm or \scalefactor at all you could just do

  \resizebox{#5cm}


and never apply \protect to { it can never do anything useful and will usually generate weird errors.

• It works. So the problem was protect? I'm really, really confused about my code right now... – Daniel Robert-Nicoud Dec 5 '15 at 23:03
• Probably one should wrap the \renewcommand in a group not to leave \scalefactor in a different state than before. – egreg Dec 5 '15 at 23:04
• @DanielRobert-Nicoud the \protect theer was doing nothing, so you have { \renewcommand\xxx{zzz} } with the {} forming a group (not delimiting an argument to \protect which never takes a brace delimited argument) so the redefinition was undone at the } as always happens with tex groups – David Carlisle Dec 5 '15 at 23:05
• @egreg I'd guess it is only used in this macro and reset everytime so it's harmeless (and not needed anyway) – David Carlisle Dec 5 '15 at 23:06
• Ok, thanks, this makes it a bit clearer. I still have a lot to learn about the inner workings of TeX... – Daniel Robert-Nicoud Dec 5 '15 at 23:06