尝试在 tikz 树中对齐外部图像

尝试在 tikz 树中对齐外部图像

我正在尝试生成一个在最外层节点包含图像(方形 pdf)的 tikz 树。(顺便说一句,我正在使用 R 和 Sweave 动态生成这些图像,但这与我的问题无关)。我无法将图像同时发送给两者;

a) 在节点中准确对齐 b) 被节点的边界框包围

这是一个简化的例子

\documentclass[final,t]{beamer}
\usepackage[english]{babel}
\usepackage[latin1]{inputenc}
\usepackage[orientation=landscape,size=a0,scale=0.85]{beamerposter}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing,backgrounds,positioning,fit,trees,mindmap,arrows,shapes}

\begin{document}
\begin{frame}
\begin{figure}[!h]
\centering
\begin{tikzpicture}[align=center, show background grid,
grow cyclic,
level 1/.style={level distance=10cm,sibling angle=90},
level 2/.style={text width=2.5cm,level distance=8cm, sibling angle=60},
level 3/.style={level distance=8cm, sibling angle=45}]
\node [draw]{What Would you Like To Show ?} %root
    child { node [minimum width=6cm, draw]{Composition} %Subtree 1
        child {node [draw]{Changing over Time}
            child{node [draw]{Few Periods} 
                child{node [draw]{Only Relative Differences Matter} 
                    child{node [anchor=south west,inner sep=5pt, minimum width=7cm, draw] at (0,0) { \includegraphics[width=6cm]{graphPostertikz-stacked100col} } }
                }
                child{node [draw]{Relative and Absolute Differences Matter} 
                    child{node [anchor=south west,inner sep=5pt, minimum width=7cm, draw] at (0,0) { \includegraphics[width=6cm]{graphPostertikz-stackedcol} } }
                }
            }
            child{node [draw]{Many Periods} 
                child{node [draw]{Only Relative Differences Matter} 
                    child{node [anchor=south west,inner sep=5pt, draw] at (0,0) { \includegraphics[width=6cm]{graphPostertikz-stacked100area} } }
                }
                child{node [draw]{Relative and Absolute Differences Matter} 
                    child{node [anchor=south west,inner sep=5pt, draw] at (0,0) { \includegraphics[width=6cm]{graphPostertikz-stackedarea} } }
                }
            }
        }
    }
;
\end{tikzpicture}
\end{figure}
\end{frame}
\end{document}

但在生成的树中,没有设置最小宽度的两幅图像很好地左对齐,但边界框比图像窄。当我尝试设置最小宽度时,图像从节点中间开始,并且仍然超出边缘。

有任何想法吗 ?

编辑:正如指出的那样,我的最小示例并不是特别最小,我已经注释掉/删除了一些内容。

答案1

这里的问题是财产继承。手册中的关键短语(PGF2.10 手册第 18.4 节的开始)是(强调矿):

每个孩子可能有自己的<options>,适用于“整个孩子”,包括其所有孙辈

了解树中可以指定选项的不同位置也很有用。这在上面的引述之后不久。我将在这里总结一下。下面列表中的最后一个实际上并未在规则列表中说明,但可以从周围的文本中推断出来。对于这种情况,它也是最重要的一个。

  1. 根节点之前给出的选项适用于整个树。
  2. 给予根节点的选项仅适用于根节点。
  3. 根节点之后给出的选项适用于所有子节点。
  4. 给予孩子的选择适用于该孩子的道路。
  5. 给予节点的选项仅适用于该节点。
  6. 在一个孩子之前给出的选项适用于所有后续的孩子。

最后要理解的是level n样式是如何工作的。这些样式会将其内容插入到该级别的第一个子节点之前,因此它们会应用于该级别的节点以及所有后续

这就是问题所在。在样式中level 2,您有键text width=2.5cm。这将设置第 2 级节点的文本宽度和其一切子孙。这包括包含图像的节点。虽然您稍后设置了,minimum width但这不会覆盖,text width因为两个宽度不同。text width是框内文本的宽度,是minimum width框外部的(最小)宽度。如果您设置了,minimum width则节点将至少是该大小,但里面的文本框仍然只有 2.5 厘米宽。因此,在您的示例中的所有节点中,都有一个宽度为 2.5 厘米的文本框包含图像,并且图像在该文本框中左对齐。在较低的节点(在页面上),此文本框本身居中在宽度为 7 厘米的盒子里,这意味着图像实际上突出到盒子的右侧。

为了确认情况确实如此,我们可以定义一个新键,告诉我们已声明任何节点的文本宽度:

\makeatletter
\tikzset{
  show text width/.code={%
    \show\tikz@text@width
  }
}
\makeatother

将其放在节点上表明level 2 全部节点的文本宽度为2.5cm。

我们如何解决这个问题?有几种方法。一种方法是text width首先避免将密钥放在可继承的位置。但这样你就必须手动将其放在第 2 级的每个节点上。另一种方法是text width在第 3 级开始时重置。在我看来,这似乎是一个合理的解决方案(我不排除有更好的解决方案的可能性)。它是不是足以将文本宽度设置为 0pt,我们必须将其设置为“空”。我们通过编写 来实现这一点text width={}。或者我们可以定义一个可能更合适的新键\tikzset{reset text width/.style={text width={}}}(即,当我们在六个月后查看此代码时, 的效果text width={}可能不透明但reset text width很明显)。最后,我们将其放在level 3样式上(或您希望它生效的任何级别)。

这是一个更简单的例子:

\documentclass{article}
%\url{http://tex.stackexchange.com/q/16147/86}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing,backgrounds,positioning,fit,trees,mindmap,arrows,shapes}

\tikzset{
  reset text width/.style={%
    text width={}
  }
}

\begin{document}
\begin{tikzpicture}[
  every node/.style={draw},
  level 1/.style={text width=2.5cm},
  level 2/.style={level distance=5cm},
  level 3/.style={reset text width}
]
\node [draw] {root}
child {node {Changing over Time}
  child{node { \includegraphics[width=6cm]{tqft}}
      child{node { \includegraphics[width=6cm]{tqft}}
      }
  }
}
;
\end{tikzpicture}
\end{document}

结果:

与文本宽度对齐图像

(实际上,一个更简单的例子是没有图形,但有 TeX 无法换行的东西。您在图形中看到这种行为的原因是 TeX 无法使框小于图像:如果您在其中放置文本,那么 TeX 会尝试将其排版在宽度为 2.5 厘米的文本框中,这样就不会出现重叠。另一方面,您会更容易看到节点中的文本位于 2.5 厘米的框中!)

对于您的特定代码,图片显示在 上level 5。因此,为了在该点(而不是更早)重置文本宽度,我们使用样式level 5。这是您的代码,经过适当修改:

\documentclass[final,t]{beamer}
\usepackage[english]{babel}
\usepackage[latin1]{inputenc}
\usepackage[orientation=landscape,size=a0,scale=0.85]{beamerposter}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing,backgrounds,positioning,fit,trees,mindmap,arrows,shapes}

\tikzset{
  reset text width/.style={%
    text width={}
  }
}

\begin{document}
\begin{frame}
\begin{figure}[!h]
\centering
\begin{tikzpicture}[align=center, show background grid,
grow cyclic,
level 1/.style={level distance=10cm,sibling angle=90},
level 2/.style={text width=2.5cm,level distance=8cm, sibling angle=60},
level 3/.style={level distance=8cm, sibling angle=45},
level 5/.style={reset text width}]
\node [draw]{What Would you Like To Show ?} %root
    child { node [minimum width=6cm, draw]{Composition} %Subtree 1
        child {node [draw]{Changing over Time}
            child{node [draw]{Few Periods} 
                child{node [draw]{Only Relative Differences Matter} 
                    child{node [anchor=south west,inner sep=5pt, minimum width=7cm, draw] at (0,0) { \includegraphics[width=6cm]{tqft} } }
                }
                child{node [draw]{Relative and Absolute Differences Matter} 
                    child{node [anchor=south west,inner sep=5pt, minimum width=7cm, draw] at (0,0) { \includegraphics[width=6cm]{tqft} } }
                }
            }
            child{node [draw]{Many Periods} 
                child{node [draw]{Only Relative Differences Matter} 
                    child{node [anchor=south west,inner sep=5pt, draw] at (0,0) { \includegraphics[width=6cm]{tqft} } }
                }
                child{node [draw]{Relative and Absolute Differences Matter} 
                    child{node [anchor=south west,inner sep=5pt, draw] at (0,0) { \includegraphics[width=6cm]{tqft} } }
                }
            }
        }
    }
;
\end{tikzpicture}
\end{figure}
\end{frame}
\end{document}

相关内容