TikZ 横向图片定位

TikZ 横向图片定位

我写了一个大型游戏树(使用自动机包)并需要以横向模式显示。我已经尝试使用两者文本位置几何学(newgeometry 命令)包,但没有任何积极的结果。我需要将图片向左移动,以便游戏树的右侧部分正确显示。有什么建议吗?此外,如果有办法将其调整为纵向模式,那就更好了。以下是代码:

\documentclass[12pt]{exam}
\usepackage{tikz}
\usepackage{pgf}
\usepackage{geometry}
\usepackage{textpos} 
\usepackage{pdflscape}
\usetikzlibrary{external,automata,trees,positioning,shadows,arrows,shapes.geometric}
\begin{document}

\begin{landscape}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm,
                semithick]
\tikzstyle{every state}=[fill,draw=none,green,text=white,circular drop shadow]
\tikzstyle{accepting}=[circle split, draw,text=black,inner sep=0.08cm]
\tikzstyle{initial}=[red,text=white]
\node[state,initial]  (N0) {Nature};
\node[state]   (N1) [below left of=N0,xshift=-5cm]  {Nature};
\node[state]   (F1) [below left of=N1]  {R\&G};
\node[state]   (K1) [below right of=N1] {R\&G};
\node[state]   (FS1) [below left of=F1]  {H};
\node[accepting]    (FN1) [above left of=F1] {\phantom{1}};
\node[state]        (KS1) [below right of=K1]  {H};
\node[accepting]    (KN1) [above right of=K1] {\phantom{1}};
\node[accepting]    (FSC1) [below left of=FS1]  {\phantom{1}};
\node[accepting]    (FSN1) [above left of=FS1] {\phantom{1}};
\node[accepting]    (KSC1) [below right of=KS1]  {\phantom{1}};
\node[accepting]    (KSN1) [above right of=KS1] {\phantom{1}};

\path(N1) edge node[ above left, pos=.20]{feed Hamlet}  node[ below right, pos=.30]{0} (F1) 
edge node[above right, pos=.20]{kill Hamlet} node[below left, pos=.30]{1} (K1);
\path(F1) edge node[above left,pos=.5]{escort} (FS1) edge node[ left]{don't}(FN1);
\path(K1) edge node[above right,pos=.5]{escort} (KS1) edge node[ right]{don't}(KN1);
\path(FS1) edge node[above left]{change} (FSC1) edge node[ left]{don't}(FSN1);
\path(KS1) edge node[above right]{change} (KSC1) edge node[ right]{don't}(KSN1);
\path(K1) edge[thick,dotted,-](F1);

 \node[state]   (N)[below right of=N0,xshift=5cm]  {Nature};
 \node[state]    (F) [below left of=N]  {R\&G};
 \node[state]    (K) [below right of=N] {R\&G};
 \node[state]    (FS) [below left of=F]  {H};
 \node[accepting]    (FN) [above left of=F] {\phantom{1}};
 \node[state]    (KS) [below right of=K]  {H};
 \node[accepting]     (KN) [above right of=K] {\phantom{1}};
  \node[accepting]    (FSC) [below left of=FS]  {\phantom{1}};
 \node[accepting]    (FSN) [above left of=FS] {\phantom{1}};
 \node[accepting]    (KSC) [below right of=KS]  {\phantom{1}};
 \node[accepting]     (KSN) [above right of=KS] {\phantom{1}};

 \path(N) edge node[ above left, pos=.20]{feed Hamlet}  node[ below right, pos=.30]{0} (F) 
edge node[above right, pos=.20]{kill Hamlet} node[below left, pos=.30]{1} (K);
 \path(F) edge node[above left,pos=.5]{escort} (FS) edge node[ left]{don't}(FN);
 \path(K) edge node[above right,pos=.5]{escort} (KS) edge node[ right]{don't}(KN);
 \path(FS) edge node[above left]{change} (FSC) edge node[ left]{don't}(FSN);
 \path(KS) edge node[above right]{change} (KSC) edge node[ right]{don't}(KSN);
 \path(K) edge[thick,dotted,-](F);

\end{tikzpicture}
\end{landscape}
\end{document}

答案1

解决方案没有,\resizebox因为使用此宏,文本会调整大小。我尝试像您一样使用横向环境。问题是要知道您是否需要在大型文档中使用此树或单独使用。

1)parindent=0pt 额外的水平空间。

2)\eject\null尝试将框以垂直模式放在新页面上;现在可以\vfill在框周围使用。

3)\hspace*{-4cm}我不知道课堂考试具体采用什么样的布局,所以我手动寻找一个合适的尺寸,使框居中而没有左边距。

4) 我在这里稍微缩小了node distance2.5 厘米而不是 2.8 厘米,也许可以再次缩小,但在这种情况下你需要更改一些文本的位置。

\documentclass[12pt]{exam}
\usepackage{tikz}
\usepackage{geometry}
\usepackage{pdflscape}
\usepackage{graphicx} 
\usetikzlibrary{external,automata,trees,positioning,shadows,arrows,shapes.geometric}

\newlength\Textht
\setlength\Textht{\textheight}

\begin{document}

\begin{landscape}
\parindent=0pt
\eject\null
\vfill
\hspace*{-4cm}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.5cm,
                semithick]
\tikzstyle{every state}=[fill,draw=none,green,text=white,circular drop shadow]
\tikzstyle{accepting}=[circle split, draw,text=black,inner sep=0.08cm]
\tikzstyle{initial}=[red,text=white]
\node[state,initial]  (N0) {Nature};
\node[state]        (N1)   [below left  of=N0,xshift=-5cm]  {Nature};
\node[state]        (F1)   [below left  of=N1]  {R\&G};
\node[state]        (K1)   [below right of=N1]  {R\&G};
\node[state]        (FS1)  [below left  of=F1]  {H};
\node[accepting]    (FN1)  [above left  of=F1]  {\phantom{1}};
\node[state]        (KS1)  [below right of=K1]  {H};
\node[accepting]    (KN1)  [above right of=K1]  {\phantom{1}};
\node[accepting]    (FSC1) [below left  of=FS1] {\phantom{1}};
\node[accepting]    (FSN1) [above left  of=FS1] {\phantom{1}};
\node[accepting]    (KSC1) [below right of=KS1] {\phantom{1}};
\node[accepting]    (KSN1) [above right of=KS1] {\phantom{1}};

\path(N1) edge node[ above left, pos=.20]{feed Hamlet}  node[ below right, pos=.30]{0} (F1) 
edge node[above right, pos=.20]{kill Hamlet} node[below left, pos=.30]{1} (K1);
\path(F1) edge node[above left,pos=.5]{escort} (FS1) edge node[ left]{don't}(FN1);
\path(K1) edge node[above right,pos=.5]{escort} (KS1) edge node[ right]{don't}(KN1);
\path(FS1) edge node[above left]{change} (FSC1) edge node[ left]{don't}(FSN1);
\path(KS1) edge node[above right]{change} (KSC1) edge node[ right]{don't}(KSN1);
\path(K1) edge[thick,dotted,-](F1);

 \node[state]        (N)   [below right of=N0,xshift=5cm]  {Nature};
 \node[state]        (F)   [below left  of=N]  {R\&G};
 \node[state]        (K)   [below right of=N] {R\&G};
 \node[state]        (FS)  [below left  of=F]  {H};
 \node[accepting]    (FN)  [above left  of=F] {\phantom{1}};
 \node[state]        (KS)  [below right of=K]  {H};
 \node[accepting]    (KN)  [above right of=K] {\phantom{1}};
 \node[accepting]    (FSC) [below left  of=FS]  {\phantom{1}};
 \node[accepting]    (FSN) [above left  of=FS] {\phantom{1}};
 \node[accepting]    (KSC) [below right of=KS]  {\phantom{1}};
 \node[accepting]    (KSN) [above right of=KS] {\phantom{1}};

 \path(N) edge node[ above left, pos=.20]{feed Hamlet}  node[ below right, pos=.30]{0} (F) 
edge node[above right, pos=.20]{kill Hamlet} node[below left, pos=.30]{1} (K);
 \path(F) edge node[above left,pos=.5]{escort} (FS) edge node[ left]{don't}(FN);
 \path(K) edge node[above right,pos=.5]{escort} (KS) edge node[ right]{don't}(KN);
 \path(FS) edge node[above left]{change} (FSC) edge node[ left]{don't}(FSN);
 \path(KS) edge node[above right]{change} (KSC) edge node[ right]{don't}(KSN);
 \path(K) edge[thick,dotted,-](F);
\end{tikzpicture}%
\hfill
\vfill
\end{landscape}
\end{document}

在此处输入图片描述

答案2

您可以\resizeboxgraphicx包中使用它;由于在横向模式下宽度和高度会交换,因此您需要使用原始宽度\textheight作为调整大小的新宽度:

\documentclass[12pt]{exam}
\usepackage{tikz}
\usepackage{geometry}
\usepackage{graphicx} 
\usetikzlibrary{external,automata,trees,positioning,shadows,arrows,shapes.geometric}

\newlength\Textht
\setlength\Textht{\textheight}

\begin{document}

\begin{landscape}
\null\vfill

\noindent\resizebox{\Textht}{!}{%
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm,
                semithick]
\tikzstyle{every state}=[fill,draw=none,green,text=white,circular drop shadow]
\tikzstyle{accepting}=[circle split, draw,text=black,inner sep=0.08cm]
\tikzstyle{initial}=[red,text=white]
\node[state,initial]  (N0) {Nature};
\node[state]   (N1) [below left of=N0,xshift=-5cm]  {Nature};
\node[state]   (F1) [below left of=N1]  {R\&G};
\node[state]   (K1) [below right of=N1] {R\&G};
\node[state]   (FS1) [below left of=F1]  {H};
\node[accepting]    (FN1) [above left of=F1] {\phantom{1}};
\node[state]        (KS1) [below right of=K1]  {H};
\node[accepting]    (KN1) [above right of=K1] {\phantom{1}};
\node[accepting]    (FSC1) [below left of=FS1]  {\phantom{1}};
\node[accepting]    (FSN1) [above left of=FS1] {\phantom{1}};
\node[accepting]    (KSC1) [below right of=KS1]  {\phantom{1}};
\node[accepting]    (KSN1) [above right of=KS1] {\phantom{1}};

\path(N1) edge node[ above left, pos=.20]{feed Hamlet}  node[ below right, pos=.30]{0} (F1) 
edge node[above right, pos=.20]{kill Hamlet} node[below left, pos=.30]{1} (K1);
\path(F1) edge node[above left,pos=.5]{escort} (FS1) edge node[ left]{don't}(FN1);
\path(K1) edge node[above right,pos=.5]{escort} (KS1) edge node[ right]{don't}(KN1);
\path(FS1) edge node[above left]{change} (FSC1) edge node[ left]{don't}(FSN1);
\path(KS1) edge node[above right]{change} (KSC1) edge node[ right]{don't}(KSN1);
\path(K1) edge[thick,dotted,-](F1);

 \node[state]   (N)[below right of=N0,xshift=5cm]  {Nature};
 \node[state]    (F) [below left of=N]  {R\&G};
 \node[state]    (K) [below right of=N] {R\&G};
 \node[state]    (FS) [below left of=F]  {H};
 \node[accepting]    (FN) [above left of=F] {\phantom{1}};
 \node[state]    (KS) [below right of=K]  {H};
 \node[accepting]     (KN) [above right of=K] {\phantom{1}};
  \node[accepting]    (FSC) [below left of=FS]  {\phantom{1}};
 \node[accepting]    (FSN) [above left of=FS] {\phantom{1}};
 \node[accepting]    (KSC) [below right of=KS]  {\phantom{1}};
 \node[accepting]     (KSN) [above right of=KS] {\phantom{1}};

 \path(N) edge node[ above left, pos=.20]{feed Hamlet}  node[ below right, pos=.30]{0} (F) 
edge node[above right, pos=.20]{kill Hamlet} node[below left, pos=.30]{1} (K);
 \path(F) edge node[above left,pos=.5]{escort} (FS) edge node[ left]{don't}(FN);
 \path(K) edge node[above right,pos=.5]{escort} (KS) edge node[ right]{don't}(KN);
 \path(FS) edge node[above left]{change} (FSC) edge node[ left]{don't}(FSN);
 \path(KS) edge node[above right]{change} (KSC) edge node[ right]{don't}(KSN);
 \path(K) edge[thick,dotted,-](F);

\end{tikzpicture}%
}
\vfill
\end{landscape}

\end{document}

在此处输入图片描述

我还使用了一些\vfill命令来垂直居中tikzpicture,但这些是可选的。还请注意,由于您正在加载tikz,因此无需明确加载pgf

可以使用相同的命令在纵向模式下调整树的大小:

\documentclass[12pt]{exam}
\usepackage{tikz}
\usepackage{geometry}
\usepackage{graphicx} 
\usepackage{pdflscape}
\usetikzlibrary{external,automata,trees,positioning,shadows,arrows,shapes.geometric}

\begin{document}

\noindent\resizebox{\textwidth}{!}{%
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=2.8cm,
                semithick]
\tikzstyle{every state}=[fill,draw=none,green,text=white,circular drop shadow]
\tikzstyle{accepting}=[circle split, draw,text=black,inner sep=0.08cm]
\tikzstyle{initial}=[red,text=white]
\node[state,initial]  (N0) {Nature};
\node[state]   (N1) [below left of=N0,xshift=-5cm]  {Nature};
\node[state]   (F1) [below left of=N1]  {R\&G};
\node[state]   (K1) [below right of=N1] {R\&G};
\node[state]   (FS1) [below left of=F1]  {H};
\node[accepting]    (FN1) [above left of=F1] {\phantom{1}};
\node[state]        (KS1) [below right of=K1]  {H};
\node[accepting]    (KN1) [above right of=K1] {\phantom{1}};
\node[accepting]    (FSC1) [below left of=FS1]  {\phantom{1}};
\node[accepting]    (FSN1) [above left of=FS1] {\phantom{1}};
\node[accepting]    (KSC1) [below right of=KS1]  {\phantom{1}};
\node[accepting]    (KSN1) [above right of=KS1] {\phantom{1}};

\path(N1) edge node[ above left, pos=.20]{feed Hamlet}  node[ below right, pos=.30]{0} (F1) 
edge node[above right, pos=.20]{kill Hamlet} node[below left, pos=.30]{1} (K1);
\path(F1) edge node[above left,pos=.5]{escort} (FS1) edge node[ left]{don't}(FN1);
\path(K1) edge node[above right,pos=.5]{escort} (KS1) edge node[ right]{don't}(KN1);
\path(FS1) edge node[above left]{change} (FSC1) edge node[ left]{don't}(FSN1);
\path(KS1) edge node[above right]{change} (KSC1) edge node[ right]{don't}(KSN1);
\path(K1) edge[thick,dotted,-](F1);

 \node[state]   (N)[below right of=N0,xshift=5cm]  {Nature};
 \node[state]    (F) [below left of=N]  {R\&G};
 \node[state]    (K) [below right of=N] {R\&G};
 \node[state]    (FS) [below left of=F]  {H};
 \node[accepting]    (FN) [above left of=F] {\phantom{1}};
 \node[state]    (KS) [below right of=K]  {H};
 \node[accepting]     (KN) [above right of=K] {\phantom{1}};
  \node[accepting]    (FSC) [below left of=FS]  {\phantom{1}};
 \node[accepting]    (FSN) [above left of=FS] {\phantom{1}};
 \node[accepting]    (KSC) [below right of=KS]  {\phantom{1}};
 \node[accepting]     (KSN) [above right of=KS] {\phantom{1}};

 \path(N) edge node[ above left, pos=.20]{feed Hamlet}  node[ below right, pos=.30]{0} (F) 
edge node[above right, pos=.20]{kill Hamlet} node[below left, pos=.30]{1} (K);
 \path(F) edge node[above left,pos=.5]{escort} (FS) edge node[ left]{don't}(FN);
 \path(K) edge node[above right,pos=.5]{escort} (KS) edge node[ right]{don't}(KN);
 \path(FS) edge node[above left]{change} (FSC) edge node[ left]{don't}(FSN);
 \path(KS) edge node[above right]{change} (KSC) edge node[ right]{don't}(KSN);
 \path(K) edge[thick,dotted,-](F);

\end{tikzpicture}%
}

\end{document}

在此处输入图片描述

相关内容