Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I need to include the next tikzpicture into a latex document. The final result gives me extra vertical space before and after the picture. Therefore the caption is far from the picture itself. I do not know the reason for this behavior. Can you help me?

I attach the content of the document:

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

Thanks a lot

share|improve this question
3  
As a start in order to reduce the (vertical) spacing around your tikzpicture, do not use \begin{center}...\end{center}. See this post for more information on that. –  Werner Jul 18 '11 at 0:07
1  
@mgomez: When asking questions, it is often a good idea to make a minimal working example, i.e. the smallest complete TeX document that still shows the problematic behaviour. In your concrete case, commenting out code chunk by code chunk until the unnecessary space disappears works really well for finding the offending bits of code. Frequently, this will even enable you to solve your problems yourself, but it will at least make it easier for others to help you. –  Jake Jul 18 '11 at 0:18
add comment

1 Answer 1

This is caused by the nodes A and B that you use to find the intersections: Even though they are not drawn, they influence the bounding box and add the unnecessary space. There are two ways you can fix this:

  • Either use less extreme points (\coordinate (A) at (\x,7); and \coordinate (B) at (\x,0); would work, for example) or

  • enclose the lines in question by \begin{pgfinterruptboundingbox} ... \end{pgfinterruptboundingbox.

Below is your code showing both adjustments:

\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}
share|improve this answer
    
Thanks a lot. I was getting crazy with this. mgomez –  mgomez Jul 18 '11 at 8:07
    
@mgomez: Instead of posting a “Thanks” as an additional answer, you should thank Jake by upvoting his answer (with the upward pointing arrow to the left of it; you need 15 reputation points before you can upvote) and accepting it (by clicking on the checkmark). We want to keep the answer space reserved for actual answers, so this non-answer will be removed from public view soon. –  Stefan Kottwitz Jul 18 '11 at 8:31
    
@mgomez: I merged your second accidental create account to the original one now. Please consider registering your account to avoid such things in the future. I also converted your answer post to a comment. –  Martin Scharrer Jul 18 '11 at 8:56
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.