1 个 TikZ 图形位于 3 个投影仪帧中,出现奇数边距,需要全局定位

1 个 TikZ 图形位于 3 个投影仪帧中,出现奇数边距,需要全局定位

我想创建一个 beamer 演示文稿,我打算使用一个 TikZ 图形来显示连续 3 帧,但使用不同的节点标签。我有一个单独的 TikZ 文件,我在其中使用自定义命令来显示标签文本,这些命令分别在主 TeX 文件中为每个帧重新定义。

问题 1: 由于标签大小不同,框架内图形的位置也会发生变化。有没有一种简单的方法可以应用相对于框架的 TikZ 坐标,或者其他方法可以均匀地定位每个框架的图形?

问题2: 当我重新定义一些标签文本时,会出现部分额外的边距。有人能向我解释为什么会发生这种情况以及如何防止这种情况吗?(--> 参见最小示例,根节点处的标签)。我开始为每个标签文本引入单独的标签距离值以进行微调,但我真的不喜欢这种不充分的解决方法。有人有更优雅的解决方案吗?

请注意,\lblAA、\lblBA 等用于定义标签文本,而 \sepAA、\sepBA 等最初用于微调间距(在此示例中设置为 0cm)。

我感谢任何帮助或建议。

提前谢谢!

% MAIN TEX FILE
\documentclass[compress,nonav,10pt]{beamer}

\usetheme{Warsaw}

\makeatletter
\DeclareMathSizes{\@xpt}{\@xpt}{3}{2}
\makeatother

\usepackage[utf8]{inputenc}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{calc,shapes}

\begin{document}

\newcommand{\lblAA}{$\vec{\phi}_{1,1}$}
\newcommand{\sepAA}{0cm}
\newcommand{\lblBA}{$\vec{\phi}_{2,1}$}
\newcommand{\sepBA}{0cm}
\newcommand{\lblBB}{$\vec{\phi}_{2,2}$}
\newcommand{\sepBB}{0cm}
\newcommand{\lblCA}{$\vec{\phi}_{\log_2\mkern-5mu N,1}$}
\newcommand{\sepCA}{0cm}
\newcommand{\lblCD}{$\vec{\phi}_{\log_2\mkern-5mu N,N\! /\! 2}$}
\newcommand{\sepCD}{0cm}
\newcommand{\lblDA}{$\vec{\psi}_1$}
\newcommand{\sepDA}{0cm}
\newcommand{\lblDB}{$\vec{\psi}_2$}
\newcommand{\sepDB}{0cm}
\newcommand{\lblDG}{$\vec{\psi}_{N-1}$}
\newcommand{\sepDG}{0cm}
\newcommand{\lblDH}{$\vec{\psi}_{N}$}
\newcommand{\sepDH}{0cm}

\begin{frame}{Tree}
\begin{figure}
\centering
\input{tree.tikz}
\end{figure}
\end{frame}

\renewcommand{\lblAA}{$\left \vert \left \langle \vec{\phi}_{1,1}, \vec{x} \right \rangle \right \vert > \tau_1$ ?}
\renewcommand{\sepAA}{0cm}
\renewcommand{\lblBA}{$\left \vert \left \langle \vec{\phi}_{2,1}, \vec{x} \right \rangle \right \vert > \tau_2$ ?}
\renewcommand{\sepBA}{0cm}
\renewcommand{\lblBB}{$\tau_2 < \left \vert \left \langle \vec{\phi}_{2,2}, \vec{x} \right \rangle \right \vert$ ?}
\renewcommand{\sepBB}{0cm}
\renewcommand{\lblCA}{$\left \vert \left \langle \vec{\phi}_{\log_2\mkern-5mu N,1}, \vec{x}\right \rangle \right \vert > \tau_{\log_2\mkern-5mu N}$ ?}
\renewcommand{\sepCA}{0cm}
\renewcommand{\lblCD}{$\tau_{\log_2\mkern-5mu N} < \left \vert \left \langle \vec{\phi}_{\log_2\mkern-5mu N,N\! /\! 2}, \vec{x} \right \rangle \right \vert$ ?}
\renewcommand{\sepCD}{0cm}
\renewcommand{\lblDA}{$\left \langle \vec{\psi}_1, \vec{x} \right \rangle$}
\renewcommand{\sepDA}{0cm}
\renewcommand{\lblDB}{$\left \langle \vec{\psi}_2, \vec{x} \right \rangle$}
\renewcommand{\sepDB}{0cm}
\renewcommand{\lblDG}{$\left \langle \vec{\psi}_{N-1}, \vec{x} \right \rangle$}
\renewcommand{\sepDG}{0cm}
\renewcommand{\lblDH}{$\left \langle \vec{\psi}_{N}, \vec{x} \right \rangle$}
\renewcommand{\sepDH}{0cm}

\begin{frame}{Tree}
\begin{figure}
\centering
\input{tree.tikz}
\end{figure}
\end{frame}

\end{document}


% THE TIKZ FILE ("tree.tikz")
\newcommand{\dx}{1.8}
\newcommand{\dy}{3}

\begin{tikzpicture}[scale=.5, every node/.style={fill=black,circle,inner sep=0pt,circle,minimum size=.2cm}]
\normalsize

\coordinate (N11) at (0,0);
\node[label={[label distance=\sepAA]above:{\lblAA}}] at (N11) {};

\coordinate (N21) at ($ (N11) + (-\dx, -\dy) $);
\node[label={[label distance=\sepBA]west:{\lblBA}}] at (N21) {};
\draw[-, thick] (N21) -- (N11);

\coordinate (N22) at ($ (N11) + (\dx, -\dy) $);
\node[ label={[label distance=\sepBB]east:{\lblBB}}] at (N22) {};
\draw[-, thick] (N22) -- (N11);

\coordinate (N31) at ($ (N21) + (-\dx, -\dy) $);
\node[label={[label distance=\sepCA]west:{\lblCA}}] at (N31) {};
\draw[dash pattern=on 1pt off 2pt, thick] (N21) -- ($ (N21) + .33*(-\dx, -\dy) $);
\draw[dash pattern=on 1pt off 2pt, thick] ($ (N21) + .66*(-\dx, -\dy) $) -- (N31);
\draw[dash pattern=on 1pt off 2pt, thick] (N21) -- ($ (N21) + .33*(\dx, -\dy) $);

\coordinate (N34) at ($ (N22) + (\dx, -\dy) $);
\node[ label={[label distance=\sepCD]east:{\lblCD}}] at (N34) {};
\draw[dash pattern=on 1pt off 2pt, thick] (N22) -- ($ (N22) + .33*(-\dx, -\dy) $);
\draw[dash pattern=on 1pt off 2pt, thick] ($ (N22) + .66*(\dx, -\dy) $) -- (N34);
\draw[dash pattern=on 1pt off 2pt, thick] (N22) -- ($ (N22) + .33*(\dx, -\dy) $);
  %\draw[-, thick] (N22) -- (N34);

\coordinate (N41) at ($ (N31) + (-\dx, -\dy) $);
\node[ label={[label distance=\sepDA]below:{\lblDA}}] at (N41) {};
\draw[-, thick] (N31) -- (N41);

\coordinate (N42) at ($ (N31) + (\dx, -\dy) $);
\node[ label={[label distance=\sepDB]below:{\lblDB}}] at (N42) {};
\draw[-, thick] (N31) -- (N42);

\coordinate (N47) at ($ (N34) + (-\dx, -\dy) $);
\node[ label={[label distance=\sepDG]below:{\lblDG}}] at (N47) {};
\draw[-, thick] (N34) -- (N47);

\coordinate (N48) at ($ (N34) + (\dx, -\dy) $);
\node[ label={[label distance=\sepDH]below:{\lblDH}}] at (N48) {};
\draw[-, thick] (N34) -- (N48);

\draw[dash pattern=on 1pt off 2pt, thick] ($ (N42) + .6*(\dx, 0)$) -- ($ (N47) + .6*(-\dx, 0) $);
\draw[dash pattern=on 1pt off 2pt, thick] ($ (N31) + .9*(\dx, 0)$) -- ($ (N34) + .9*(-\dx, 0) $);

\end{tikzpicture}

答案1

提到的问题源于您使用了every node/.stylecircle并且标签实际上是节点,因此如果您增加标签文本,则节点会增加其半径,从而将树向下推。

下面我展示了针对上述两个问题的可能解决方案:

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{calc,positioning}

\newcommand\NodeC[3]{%
  \temporal<2>{#1}{#2}{#3}
}

\begin{document}

\begin{frame}
\frametitle<1-3>{A tree with changing labels}
\begin{tikzpicture}[scale=0.8,
  remember picture,
  overlay,
  node distance=1.5cm and 0.75cm,
  mynode/.style={
    fill=black,
    circle,
    inner sep=0pt,
    circle,
    minimum size=.2cm}
]

% The nodes and some auxiliary coordinates
\node[mynode] at ([yshift=-0.3\textheight]current page.north) (root) {};
\node[mynode,below left=of root] (L) {};
\node[mynode,below right=of root] (R) {};
\node[mynode,below left=of L] (LL) {};
\node[mynode,below right=of R] (RR) {};
\node[mynode,below left=of LL] (LLL) {};
\node[mynode,below right=of LL] (LLR) {};
\node[mynode,below left=of RR] (RRL) {};
\node[mynode,below right=of RR] (RRR) {};

\coordinate[below right=of L] (LR);
\coordinate[below left=of R] (RL);

\path (L) -- coordinate[pos=0.3333] (aux1) coordinate[pos=0.6666] (aux2) (LL);
\path (R) -- coordinate[pos=0.3333] (aux3) coordinate[pos=0.6666] (aux4) (RR);
\path (L) -- coordinate[pos=0.3333] (aux5) (LR);
\path (R) -- coordinate[pos=0.3333] (aux6) (RL);

% Uncomment the follwing lines if you want to see 
% placement of the aux. coordinates
%\foreach \i in {1,...,6}
  %\node at (aux\i) {aux\i};

% The solid lines
\draw (L) -- (root) -- (R);
\draw (LLL) -- (LL) -- (LLR);
\draw (RRL) -- (RR) -- (RRR);

% The dashed lines
\draw[dash pattern=on 1pt off 2pt, thick] (L) -- (aux1);
\draw[dash pattern=on 1pt off 2pt, thick] (aux2) -- (LL);
\draw[dash pattern=on 1pt off 2pt, thick] (L) -- (aux5);
\draw[dash pattern=on 1pt off 2pt, thick] (R) -- (aux3);
\draw[dash pattern=on 1pt off 2pt, thick] (aux4) -- (RR);
\draw[dash pattern=on 1pt off 2pt, thick] (R) -- (aux6);
\draw[dash pattern=on 1pt off 2pt, thick] (L|-LL) -- (R|-RR);
\draw[dash pattern=on 1pt off 2pt, thick] ([xshift=5pt]aux5|-LLR) -- ([xshift=-5pt]aux6|-RRL);

% The labels
\node[above=3pt of root.north] 
  {\NodeC{$\vec{\phi}_{1,1}$}{$\vert \langle \vec{\phi}_{1,1}, \vec{x} \rangle \vert > \tau_1$ ?}{$\dfrac{a}{b}$}};

% level 1
\node[left=3pt of L,anchor=east] 
  {\NodeC{$\vec{\phi}_{2,1}$}{$\vert \langle \vec{\phi}_{2,1}, \vec{x} \rangle \vert > \tau_2$ ?}{$\dfrac{a}{b}$}};
\node[right=3pt of R,anchor=west] 
  {\NodeC{$\vec{\phi}_{2,2}$}{$\tau_2 < \vert \langle \vec{\phi}_{2,2}, \vec{x} \rangle \vert$ ?}{$\dfrac{a}{b}$}};

%level N
\node[left=3pt of LL,anchor=east] 
  {\NodeC{$\vec{\phi}_{\log_2\mkern-5mu N,1}$}{$\vert \langle \vec{\phi}_{\log_2\mkern-5mu N,1}, \vec{x}\rangle \vert > \tau_{\log_2\mkern-5mu N}$ ?}{$\dfrac{a}{b}$}};
\node[right=3pt of RR,anchor=west] 
  {\NodeC{$\vec{\phi}_{\log_2\mkern-5mu N,N\! /\! 2}$}{$\tau_{\log_2\mkern-5mu N} < \vert \langle \vec{\phi}_{\log_2\mkern-5mu N,N\! /\! 2}, \vec{x} \rangle \vert$ ?}{$\dfrac{a}{b}$}};

% leaves
\node[below=3pt of LLL]  
  {\NodeC{$\vec{\psi}_1$}{$\langle \vec{\psi}_1, \vec{x} \rangle$}{$\dfrac{a}{b}$}};
\node[below=3pt of LLR]  
  {\NodeC{$\vec{\psi}_2$}{$\langle \vec{\psi}_1, \vec{x} \rangle$}{$\dfrac{a}{b}$}};
\node[below=3pt of RRL]  
  {\NodeC{$\vec{\psi}_{N-1}$}{$\langle \vec{\psi}_{N-1}, \vec{x} \rangle$}{$\dfrac{a}{b}$}};
\node[below=3pt of RRR]  
  {\NodeC{$\vec{\psi}_N$}{$\langle \vec{\psi}_N, \vec{x} \rangle$}{$\dfrac{a}{b}$}};
\end{tikzpicture}
\end{frame}

\end{document}

在此处输入图片描述

一些评论

  1. \input我没有在不同的框架中对树进行三次标记,而是使用叠加规范在三张幻灯片上只显示一棵树并更改标签。

  2. 我曾经\temporal为这三张幻灯片贴了不同的标签。

  3. 使用tikz-qtree或者forest,代码可以大大简化。

  4. 我通过移动锚点将根放置在绝对坐标处current page.north

  5. 其它节点均使用该positioning库进行放置。

相关内容