2

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?

13
  • 1
    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 Dec 5, 2015 at 22:43
  • @DavidCarlisle Wait, I'll edit my question. Dec 5, 2015 at 22:46
  • @DavidCarlisle There, done. Actually now I have a completely different problem. I have no idea where the error came from... Dec 5, 2015 at 22:48
  • @DanielRobert-Nicoud: Fragments of code are not really useful, in my point of view
    – user31729
    Dec 5, 2015 at 22:48
  • @ChristianHupfer This is my whole macro for now. I run it in a document simply by calling \bintree{}{}{}{}{3} (for example). Dec 5, 2015 at 22:51

1 Answer 1

2

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.

5
  • It works. So the problem was protect? I'm really, really confused about my code right now... Dec 5, 2015 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, 2015 at 23:04
  • 1
    @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 Dec 5, 2015 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) Dec 5, 2015 at 23:06
  • Ok, thanks, this makes it a bit clearer. I still have a lot to learn about the inner workings of TeX... Dec 5, 2015 at 23:06

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.