垂直空间和 tikz 图片的问题

垂直空间和 tikz 图片的问题

我需要将以下内容包含tikzpicture在 latex 文档中。最终结果在图片前后提供了额外的垂直空间。因此标题距离图片本身很远。我不知道这种现象的原因。你能帮助我吗?

我附上文件内容:

\documentclass [12pt]{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usetikzlibrary{shapes}
\usepackage{graphics}
\usepackage{scalefnt}
\usepackage[hang,small,bf]{caption}
\setlength{\captionmargin}{25pt}
\usetikzlibrary{intersections}
\usetikzlibrary{fit}
\usetikzlibrary{arrows}
\usetikzlibrary{external}
\tikzexternalize % activate!


\begin{document}

\begin{figure}[h!]
\begin{center}
\resizebox{\linewidth}{!}{
\begin{tikzpicture}[dot/.style={fill=red,circle}]
\def\parts{3}
\def\width{\parts}
\def\height{1}
\def\centerx{10}
\def\centery{7}
\tikzstyle{edge from parent}=[->,thick,draw,>=stealth']
% Rejilla de ayuda\
\draw [help lines] grid (20,7);
% Se pinta ahora el RPT
% Este es el nodo raíz, con Split para X1
\node[] (x1) at (\centerx,\centery) {$X_1$};
% Se pinta el List Node a la izquierda
\node[name path=ell1,draw,ellipse,minimum width=\parts cm,minimum height=\height cm] (ell1) at (\centerx-2.5,\centery-2) {};
\pgfmathsetmacro{\divisions}{\parts-1}
\pgfmathsetmacro{\anc}{\width/\parts}
\pgfmathsetmacro{\centrocelda}{\anc/2}
% Se trazan las lineas divisorias
\foreach \i in {1,...,\divisions} {
  \pgfmathsetmacro{\x}{\centerx-2.5+\width/2-(\i*\anc)}
  %\node [] (p2) at(\x,\x){\x};
  %\node [] (p3) at(-10,-\i){\i.\x};  
  \coordinate [] (A) at (\x,10);
  \coordinate [] (B) at (\x,-10);
  \path[name path=A--B] (A) -- (B);
  \path [name intersections={of=ell1 and A--B}];
  \draw [-] (intersection-1) -- (intersection-2);
}
% Se hacen nodos en los centros de las divisiones para luego
% poder enlazar facilmente
\pgfmathsetmacro{\xfinal}{\width/2-(\centrocelda)}
\foreach \i in {0,...,\divisions}{
  \pgfmathsetmacro{\x}{\xfinal-(\i*\anc)}
  \coordinate [] (c\i) at (\centerx-2.5+\x,\centery-2);
  \node [] (u\i) at(c\i){};
}
% Se une la raiz con el nodo lista
\path[->,thick,>=stealth'] (x1.south) edge node[left]{$x_{11}$} (ell1);
%Ahora se pintan los hijos del nodo lista de la izquierda
\node[draw,ellipse] (fi1) at (\centerx-3.5,\centery-4) {$f_1$};
\node[draw,ellipse] (fi2) at (\centerx-2.5,\centery-4) {$f_2$};
\node[draw,ellipse] (fi3) at (\centerx-1.5,\centery-4) {$f_3$};
% Se unen a ell1
\path[->,thick,>=stealth'] (u0.south) edge node[]{} (fi3);
\path[->,thick,>=stealth'] (u1.south) edge node[]{} (fi2);
\path[->,thick,>=stealth'] (u2.south) edge node[]{} (fi1);
% Se pinta el List Node a la derecha
\node[name path=ell2,draw,ellipse,minimum width=\parts cm,minimum height=\height cm] (ell2) at (\centerx+2.5,\centery-2) {};
\pgfmathsetmacro{\divisions}{\parts-1}
\pgfmathsetmacro{\anc}{\width/\parts}
\pgfmathsetmacro{\centrocelda}{\anc/2}
% Se trazan las lineas divisorias
\foreach \i in {1,...,\divisions} {
  \pgfmathsetmacro{\x}{\centerx+2.5+\width/2-(\i*\anc)}
  %\node [] (p2) at(\x,\x){\x};
  %\node [] (p3) at(-10,-\i){\i.\x};  
  \coordinate [] (A) at (\x,10);
  \coordinate [] (B) at (\x,-10);
  \path[name path=A--B] (A) -- (B);
  \path [name intersections={of=ell2 and A--B}];
  \draw [-] (intersection-1) -- (intersection-2);
}
% Se hacen nodos en los centros de las divisiones para luego
% poder enlazar facilmente
\pgfmathsetmacro{\xfinal}{\width/2-(\centrocelda)}
\foreach \i in {0,...,\divisions}{
  \pgfmathsetmacro{\x}{\xfinal-(\i*\anc)}
  \coordinate [] (c\i) at (\centerx+2.5+\x,\centery-2);
  \node [] (u\i) at(c\i){};
}
% Se une la raiz con el nodo lista
\path[->,thick,>=stealth'] (x1.south) edge node[right]{$x_{12}$} (ell2);
%Ahora se pintan los hijos del nodo lista de la derecha
\node[draw,ellipse] (fi4) at (\centerx+1.5,\centery-4) {$f_4$};
\node[draw,ellipse] (fi5) at (\centerx+2.5,\centery-4) {$f_5$};
\node[draw,ellipse] (fi6) at (\centerx+3.5,\centery-4) {$f_6$};
% Se unen a ell1
\path[->,thick,>=stealth'] (u0.south) edge node[]{} (fi6);
\path[->,thick,>=stealth'] (u1.south) edge node[]{} (fi5);
\path[->,thick,>=stealth'] (u2.south) edge node[]{} (fi4);
%Se ponen las etiquetas con la información auxiliar
\node[] (et1) at (\centerx-3,\centery-5) {$S_1=\{X_1\}$};
\node[] (et2) at (\centerx-3,\centery-5.5) {$S_2=\{X_3\}$};
\node[] (et3) at (\centerx-3,\centery-6) {$Z_1=\{X_5,X_6\}$};
\node[] (et4) at (\centerx-3,\centery-6.5) {$Z_2=\{X_2,X_4,X_7\}$};
\node[] (et5) at (\centerx-3,\centery-7) {$f_1=p^{R(X_1=x_{11})}(X_5,X_6,X_3)$};
\node[] (et6) at (\centerx+2,\centery-5) {$f_2=p^{R(X_1=x_{11})}(X_2,X_4,X_7,X_3)$};
\node[] (et7) at (\centerx+2,\centery-5.5) {$f_3=S^{R(X_1=x_{11})}$};
\node[] (et9) at (\centerx+2,\centery-6) {$f_4=p^{R(X_1=x_{12})}(X_5,X_6,X_3)$};
\node[] (et10) at (\centerx+2,\centery-6.5) {$f_5=p^{R(X_1=x_{12})}(X_2,X_4,X_7,X_3)$};
\node[] (et11) at (\centerx+2,\centery-7) {$f_6=S^{R(X_1=x_{12})}$};
\end{tikzpicture}
}
\end{center}
\caption{RPT (left) encoding of a Bayesian network distribution (right)}
\label{fig:rptNet}
\end{figure}

\end{document}

多谢

答案1

这是由您用来查找交点的节点A和引起B的:即使没有绘制它们,它们也会影响边界框并添加不必要的空间。有两种方法可以解决此问题:

  • 要么使用较少的极值点(例如,会起作用),\coordinate (A) at (\x,7);要么\coordinate (B) at (\x,0);

  • 将有问题的行括起来\begin{pgfinterruptboundingbox} ... \end{pgfinterruptboundingbox

以下是显示这两项调整的代码:

\documentclass [12pt]{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usetikzlibrary{shapes}
\usepackage{graphics}
\usepackage{scalefnt}
\usepackage[hang,small,bf]{caption}
\setlength{\captionmargin}{25pt}
\usetikzlibrary{intersections}
\usetikzlibrary{fit}
\usetikzlibrary{arrows}


\begin{document}

\begin{figure}[h!]
\resizebox{\linewidth}{!}{
\begin{tikzpicture}[dot/.style={fill=red,circle}]
\def\parts{3}
\def\width{\parts}
\def\height{1}
\def\centerx{10}
\def\centery{7}
\tikzstyle{edge from parent}=[->,thick,draw,>=stealth']
% Rejilla de ayuda\
% Se pinta ahora el RPT
% Este es el nodo raíz, con Split para X1
\node[] (x1) at (\centerx,\centery) {$X_1$};
% Se pinta el List Node a la izquierda
\node[name path=ell1,draw,ellipse,minimum width=\parts cm,minimum height=\height cm] (ell1) at (\centerx-2.5,\centery-2) {};
\pgfmathsetmacro{\divisions}{\parts-1}
\pgfmathsetmacro{\anc}{\width/\parts}
\pgfmathsetmacro{\centrocelda}{\anc/2}
% Se trazan las lineas divisorias
\begin{pgfinterruptboundingbox}
\foreach \i in {1,...,\divisions} {
  \pgfmathsetmacro{\x}{\centerx-2.5+\width/2-(\i*\anc)}
  %\node [] (p2) at(\x,\x){\x};
  %\node [] (p3) at(-10,-\i){\i.\x};  
  \coordinate [] (A) at (\x,10);
  \coordinate [] (B) at (\x,-10);
  \path[name path=A--B] (A) -- (B);
  \path [name intersections={of=ell1 and A--B}];
  \draw [-] (intersection-1) -- (intersection-2);
}
\end{pgfinterruptboundingbox}
% Se hacen nodos en los centros de las divisiones para luego
% poder enlazar facilmente
\pgfmathsetmacro{\xfinal}{\width/2-(\centrocelda)}
\foreach \i in {0,...,\divisions}{
  \pgfmathsetmacro{\x}{\xfinal-(\i*\anc)}
  \coordinate [] (c\i) at (\centerx-2.5+\x,\centery-2);
  \node [] (u\i) at(c\i){};
}
% Se une la raiz con el nodo lista
\path[->,thick,>=stealth'] (x1.south) edge node[left]{$x_{11}$} (ell1);
%Ahora se pintan los hijos del nodo lista de la izquierda
\node[draw,ellipse] (fi1) at (\centerx-3.5,\centery-4) {$f_1$};
\node[draw,ellipse] (fi2) at (\centerx-2.5,\centery-4) {$f_2$};
\node[draw,ellipse] (fi3) at (\centerx-1.5,\centery-4) {$f_3$};
% Se unen a ell1
\path[->,thick,>=stealth'] (u0.south) edge node[]{} (fi3);
\path[->,thick,>=stealth'] (u1.south) edge node[]{} (fi2);
\path[->,thick,>=stealth'] (u2.south) edge node[]{} (fi1);
% Se pinta el List Node a la derecha
\node[name path=ell2,draw,ellipse,minimum width=\parts cm,minimum height=\height cm] (ell2) at (\centerx+2.5,\centery-2) {};
\pgfmathsetmacro{\divisions}{\parts-1}
\pgfmathsetmacro{\anc}{\width/\parts}
\pgfmathsetmacro{\centrocelda}{\anc/2}
% Se trazan las lineas divisorias
\foreach \i in {1,...,\divisions} {
  \pgfmathsetmacro{\x}{\centerx+2.5+\width/2-(\i*\anc)}
  \coordinate [] (A) at (\x,7);
  \coordinate [] (B) at (\x,0);
  \path[name path=A--B] (A) -- (B);
  \path [name intersections={of=ell2 and A--B}];
  \draw [-] (intersection-1) -- (intersection-2);
}
% Se hacen nodos en los centros de las divisiones para luego
% poder enlazar facilmente
\pgfmathsetmacro{\xfinal}{\width/2-(\centrocelda)}
\foreach \i in {0,...,\divisions}{
  \pgfmathsetmacro{\x}{\xfinal-(\i*\anc)}
  \coordinate [] (c\i) at (\centerx+2.5+\x,\centery-2);
  \node [] (u\i) at(c\i){};
}
% Se une la raiz con el nodo lista
\path[->,thick,>=stealth'] (x1.south) edge node[right]{$x_{12}$} (ell2);
%Ahora se pintan los hijos del nodo lista de la derecha
\node[draw,ellipse] (fi4) at (\centerx+1.5,\centery-4) {$f_4$};
\node[draw,ellipse] (fi5) at (\centerx+2.5,\centery-4) {$f_5$};
\node[draw,ellipse] (fi6) at (\centerx+3.5,\centery-4) {$f_6$};
% Se unen a ell1
\path[->,thick,>=stealth'] (u0.south) edge node[]{} (fi6);
\path[->,thick,>=stealth'] (u1.south) edge node[]{} (fi5);
\path[->,thick,>=stealth'] (u2.south) edge node[]{} (fi4);
%Se ponen las etiquetas con la información auxiliar
\node[] (et1) at (\centerx-3,\centery-5) {$S_1=\{X_1\}$};
\node[] (et2) at (\centerx-3,\centery-5.5) {$S_2=\{X_3\}$};
\node[] (et3) at (\centerx-3,\centery-6) {$Z_1=\{X_5,X_6\}$};
\node[] (et4) at (\centerx-3,\centery-6.5) {$Z_2=\{X_2,X_4,X_7\}$};
\node[] (et5) at (\centerx-3,\centery-7) {$f_1=p^{R(X_1=x_{11})}(X_5,X_6,X_3)$};
\node[] (et6) at (\centerx+2,\centery-5) {$f_2=p^{R(X_1=x_{11})}(X_2,X_4,X_7,X_3)$};
\node[] (et7) at (\centerx+2,\centery-5.5) {$f_3=S^{R(X_1=x_{11})}$};
\node[] (et9) at (\centerx+2,\centery-6) {$f_4=p^{R(X_1=x_{12})}(X_5,X_6,X_3)$};
\node[] (et10) at (\centerx+2,\centery-6.5) {$f_5=p^{R(X_1=x_{12})}(X_2,X_4,X_7,X_3)$};
\node[] (et11) at (\centerx+2,\centery-7) {$f_6=S^{R(X_1=x_{12})}$};
\end{tikzpicture}
}
\caption{RPT (left) encoding of a Bayesian network distribution (right)}
\label{fig:rptNet}
\end{figure}

\end{document}

相关内容