如何缩放图形环境中的 TikZ 图像

如何缩放图形环境中的 TikZ 图像

我有以下代码:

\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}

输出结果为:

在此处输入图片描述

取消注释该行

    % transform canvas={scale=0.6}

将扩大 TiZ 图像,但结果格式被破坏了。

在此处输入图片描述

我希望能够扩展 TiZ 图像,但保持其他格式不变。

答案1

嗯,有什么问题scale=0.6?它确实会缩小整个树,但字体大小不会改变。

\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}

在此处输入图片描述


如果您想让整个内容(包括字体大小)变小,这\scalebox可能是一个不错的选择。

\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}

在此处输入图片描述


嗯,\scalebox也有它的缺点。因此我认为重置所有节点的字体大小,就像吉列尔梅·扎诺泰利建议的,更好,但你必须手动完成。这里我使用\footnotesize

在此处输入图片描述


如果你不喜欢手动操作,那么让 TiZ 帮助transform shape,由 提议土拨鼠在评论中(非常感谢)。此选项将按给定的比例更改字体大小。但是,有时您会得到非常小的字体大小,这很难阅读。

\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}

在此处输入图片描述

有很多方法可以实现。选择你最喜欢的一个 ;)

相关内容