3

I have the following code :

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning,shadows,arrows}
\tikzset{
  treenode/.style = {shape=rectangle, rounded corners,
                     draw, align=center,
                     top color=white, bottom color=blue!20},
  root/.style     = {treenode, font=\Large, bottom color=red!30},
  env/.style      = {treenode, font=\ttfamily\normalsize},
  dummy/.style    = {circle,draw}
}

\begin{document}

this is the thing

\begin{figure}
\begin{center}
\begin{tikzpicture}[
    root/.style={circle, draw=none, rounded corners=1mm, fill=green, circular drop shadow,
        text centered, anchor=north, text=black},
    main/.style={circle, draw=none, rounded corners=1mm, fill=pink, circular drop shadow,
        text centered, anchor=north, text=black},
    fact/.style={rectangle, draw=none, rounded corners=1mm, fill=blue, drop shadow,
        text centered, anchor=north, text=white},
    state/.style={circle, draw=none, fill=orange, circular drop shadow,
        text centered, anchor=north, text=white},
    leaf/.style={circle, draw=none, fill=red, circular drop shadow,
        text centered, anchor=north, text=white},
    level distance=0.5cm, growth parent anchor=south,
    % 
    % this line will scale the tikz image but messes up the text
    % transform canvas={scale=0.6}
]
\node (State00) [main] {Question} [->]
        child{ [sibling distance=9cm]
            node (State01) [state] {One}
            child{
                node (Fact02) [fact] {yes}
                child{ [sibling distance=4cm]
                    node (State02) [leaf] {Good}
                }
            }
            child{ [sibling distance=4cm]
                node (Fact10) [fact] {no}
                child{
                    node (State10) [state] {Two}
                    child{
                        node (Fact11) [fact] {yes}
                        child{
                            node (State11) [leaf] {Good}
                        }
                    }
                    child{
                        node (Fact12) [fact] {no}
                        child{
                            node (State11) [leaf] {Bad}
                        }
                    }
                }
            }
        }
    % }   
;
\end{tikzpicture}
    \caption[Decision Tree]{%
        %
        \emph{Basic decision tree 
        %
        }}
    \label{tikz:decision-tree}
\end{center}
\end{figure}
\end{document}

Which will out put :

enter image description here

Un-commenting the line

    % transform canvas={scale=0.6}

will scale the TikZ image, but as a result the formatting is broken.

enter image description here

I would like to be able to scale the TikZ image, but keep the formatting otherwise intact.

baxx
  • 709
  • Add scale=0.6 only? –  Mar 30 '19 at 15:40
  • @JouleV thanks, that seems to mainly shrink the width, whereas i was interested in shrinking the overall tree – baxx Mar 30 '19 at 15:42
  • transform canvas is necessary to scale down the nodes, as, normally, we do not want to mess up with the font sizes and line widths. The problem is that transform canvas makes the tikz forget about the original drawing bounding box, thus it scales everything but "missplaces" it... – Guilherme Zanotelli Mar 30 '19 at 15:44
  • 1
    A hack would be to add a \rule{0pt}{5.5cm} inside the argument of your caption – koleygr Mar 30 '19 at 15:52

1 Answers1

7

Uhm, what's wrong with scale=0.6? It does shrink the overall tree without the font size.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning,shadows,arrows}
\tikzset{
  treenode/.style = {shape=rectangle, rounded corners,
                     draw, align=center,
                     top color=white, bottom color=blue!20},
  root/.style     = {treenode, font=\Large, bottom color=red!30},
  env/.style      = {treenode, font=\ttfamily\normalsize},
  dummy/.style    = {circle,draw}
}
\usepackage{lipsum}

\begin{document}

\lipsum[1]

\begin{figure}
\begin{center}
\begin{tikzpicture}[
    root/.style={circle, draw=none, rounded corners=1mm, fill=green, circular drop shadow,
        text centered, anchor=north, text=black},
    main/.style={circle, draw=none, rounded corners=1mm, fill=pink, circular drop shadow,
        text centered, anchor=north, text=black},
    fact/.style={rectangle, draw=none, rounded corners=1mm, fill=blue, drop shadow,
        text centered, anchor=north, text=white},
    state/.style={circle, draw=none, fill=orange, circular drop shadow,
        text centered, anchor=north, text=white},
    leaf/.style={circle, draw=none, fill=red, circular drop shadow,
        text centered, anchor=north, text=white},
    level distance=0.5cm, growth parent anchor=south,
    % 
    % this line will scale the tikz image but messes up the text
    % transform canvas={scale=0.6}
    scale=0.6
]
\node (State00) [main] {Question} [->]
        child{ [sibling distance=9cm]
            node (State01) [state] {One}
            child{
                node (Fact02) [fact] {yes}
                child{ [sibling distance=4cm]
                    node (State02) [leaf] {Good}
                }
            }
            child{ [sibling distance=4cm]
                node (Fact10) [fact] {no}
                child{
                    node (State10) [state] {Two}
                    child{
                        node (Fact11) [fact] {yes}
                        child{
                            node (State11) [leaf] {Good}
                        }
                    }
                    child{
                        node (Fact12) [fact] {no}
                        child{
                            node (State11) [leaf] {Bad}
                        }
                    }
                }
            }
        }
    % }   
;
\end{tikzpicture}
    \caption[Decision Tree]{%
        %
        \emph{Basic decision tree 
        %
        }}
    \label{tikz:decision-tree}
\end{center}
\end{figure}
\end{document}

enter image description here


If you want to make the whole thing (including font size) smaller, \scalebox may be a good option.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning,shadows,arrows}
\tikzset{
  treenode/.style = {shape=rectangle, rounded corners,
                     draw, align=center,
                     top color=white, bottom color=blue!20},
  root/.style     = {treenode, font=\Large, bottom color=red!30},
  env/.style      = {treenode, font=\ttfamily\normalsize},
  dummy/.style    = {circle,draw}
}
\usepackage{lipsum}

\begin{document}

\lipsum[1]

\begin{figure}
\begin{center}
\scalebox{0.6}{\begin{tikzpicture}[
    root/.style={circle, draw=none, rounded corners=1mm, fill=green, circular drop shadow,
        text centered, anchor=north, text=black},
    main/.style={circle, draw=none, rounded corners=1mm, fill=pink, circular drop shadow,
        text centered, anchor=north, text=black},
    fact/.style={rectangle, draw=none, rounded corners=1mm, fill=blue, drop shadow,
        text centered, anchor=north, text=white},
    state/.style={circle, draw=none, fill=orange, circular drop shadow,
        text centered, anchor=north, text=white},
    leaf/.style={circle, draw=none, fill=red, circular drop shadow,
        text centered, anchor=north, text=white},
    level distance=0.5cm, growth parent anchor=south,
    % 
    % this line will scale the tikz image but messes up the text
    % transform canvas={scale=0.6}
]
\node (State00) [main] {Question} [->]
        child{ [sibling distance=9cm]
            node (State01) [state] {One}
            child{
                node (Fact02) [fact] {yes}
                child{ [sibling distance=4cm]
                    node (State02) [leaf] {Good}
                }
            }
            child{ [sibling distance=4cm]
                node (Fact10) [fact] {no}
                child{
                    node (State10) [state] {Two}
                    child{
                        node (Fact11) [fact] {yes}
                        child{
                            node (State11) [leaf] {Good}
                        }
                    }
                    child{
                        node (Fact12) [fact] {no}
                        child{
                            node (State11) [leaf] {Bad}
                        }
                    }
                }
            }
        }
    % }   
;
\end{tikzpicture}}
    \caption[Decision Tree]{%
        %
        \emph{Basic decision tree 
        %
        }}
    \label{tikz:decision-tree}
\end{center}
\end{figure}
\end{document}

enter image description here


Well, \scalebox also has its drawback. Therefore I think resetting the font size of all nodes, as Guilherme Zanotelli suggested, is nicer, but you have to do it manually. Here I use \footnotesize.

enter image description here


If you don't like doing things manually, let TikZ help by transform shape, proposed by marmot in the comments (thank you very much). This option will change the font size in the scale given. However, sometimes you will get a supertiny font size, which is hard to read.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning,shadows,arrows}
\tikzset{
  treenode/.style = {shape=rectangle, rounded corners,
                     draw, align=center,
                     top color=white, bottom color=blue!20},
  root/.style     = {treenode, font=\Large, bottom color=red!30},
  env/.style      = {treenode, font=\ttfamily\normalsize},
  dummy/.style    = {circle,draw}
}
\usepackage{lipsum}

\begin{document}

\lipsum[1]

\begin{figure}
\begin{center}
\begin{tikzpicture}[
    root/.style={circle, draw=none, rounded corners=1mm, fill=green, circular drop shadow,
        text centered, anchor=north, text=black},
    main/.style={circle, draw=none, rounded corners=1mm, fill=pink, circular drop shadow,
        text centered, anchor=north, text=black},
    fact/.style={rectangle, draw=none, rounded corners=1mm, fill=blue, drop shadow,
        text centered, anchor=north, text=white},
    state/.style={circle, draw=none, fill=orange, circular drop shadow,
        text centered, anchor=north, text=white},
    leaf/.style={circle, draw=none, fill=red, circular drop shadow,
        text centered, anchor=north, text=white},
    level distance=0.5cm, growth parent anchor=south,
    % 
    % this line will scale the tikz image but messes up the text
    % transform canvas={scale=0.6}
    scale=0.6,transform shape
]
\node (State00) [main] {Question} [->]
        child{ [sibling distance=9cm]
            node (State01) [state] {One}
            child{
                node (Fact02) [fact] {yes}
                child{ [sibling distance=4cm]
                    node (State02) [leaf] {Good}
                }
            }
            child{ [sibling distance=4cm]
                node (Fact10) [fact] {no}
                child{
                    node (State10) [state] {Two}
                    child{
                        node (Fact11) [fact] {yes}
                        child{
                            node (State11) [leaf] {Good}
                        }
                    }
                    child{
                        node (Fact12) [fact] {no}
                        child{
                            node (State11) [leaf] {Bad}
                        }
                    }
                }
            }
        }
    % }   
;
\end{tikzpicture}
    \caption[Decision Tree]{%
        %
        \emph{Basic decision tree 
        %
        }}
    \label{tikz:decision-tree}
\end{center}
\end{figure}
\end{document}

enter image description here

There are many ways to do it. Choose the one you like best ;)

  • Using scale=0.6 plus every node/.style={font=\desiredfontsize} properly scales the picture while maintaining font properties. =D – Guilherme Zanotelli Mar 30 '19 at 15:52
  • @GuilhermeZanotelli Seeing the OP's second image, I don't think (s)he wants to keep the font size. –  Mar 30 '19 at 15:54
  • keeping font size is a nice option - do i just add every node/.style={font=\desiredfontsize} into the tikz options? Thank you Joule – baxx Mar 30 '19 at 15:55
  • @baxx Well, you have at least three options to choose from right now. Choose whatever you want :) –  Mar 30 '19 at 15:56
  • 2
    If you use scale=0.6,transform shape the fonts will be transformed, too. (\scalebox may cause trouble if you want to work with remember pictrue.) –  Mar 30 '19 at 16:06
  • @marmot You can always write an answer, and I can always upvote your answer :) Your proposal is very nice! –  Mar 30 '19 at 16:08
  • 2
    @JouleV No need. Your answer is great and I do not like "picking". But you could just add this alternative. (And of course I gave +1.) –  Mar 30 '19 at 16:09
  • @JouleV My sincere compliments (+1) also for me. :-) I saw your profile :-): I can ask you a question, I apologize before. – Sebastiano Mar 30 '19 at 16:11
  • @Sebastiano What do you apologize for? I don't really understand. –  Mar 30 '19 at 16:12
  • @marmot Well, I will add your option as requested. –  Mar 30 '19 at 16:12
  • @JouleV My only curiosity was that you're very good at math. Since it's something personal, I generally don't ask for something private in my life. – Sebastiano Mar 30 '19 at 16:13
  • @GuilhermeZanotelli Since when \desiredfontsize is defined? I am using a (not up-to-date) compiler and it tells me that the command is not defined. –  Mar 30 '19 at 16:17
  • 1
    @JouleV it's not, hahahaha. It's meant as "desired font size command" \footnotesize and etcetera! ;D – Guilherme Zanotelli Mar 30 '19 at 17:05
  • @GuilhermeZanotelli Ohh, you made my day :) I am completely trolled :D –  Mar 30 '19 at 17:05