1

My tikz figure goes out of the page, although its size is lower than the page. Is there a way to choose where to place the first node? Below is the figure and the script. I already tried to remove some parts of the code to make a minimal reproducing example, but if I remove more, the whole figure appears within the page. tikz figure

Script:

\documentclass[landscape]{article}
\usepackage{tikz}
\usepackage[nomessages]{fp}
\usepackage{changepage}
\usetikzlibrary{positioning}


\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{xcolor}

\pagestyle{empty}

\begin{document}
\begin{figure} 
\begin{tikzpicture}[
node distance=1 cm and 0.2cm,
ar/.style={->,>=latex,thick},
mynode/.style={circle,
  line width=1pt,
  text width=1.5cm,
  minimum size=0.2 cm,
  align=center
  }
]

%------------------------------------------------------------------------------
%------------------------------------------------------------------------------
%First level
\node[mynode,draw,line width=2pt,text width=1.5cm] (l1) {Baseline prevalence};
%Second level
\node[mynode,draw,line width=2pt,text width=1.5cm, below left=2cm and 4cm of l1] (l2_1) {Study 1};
\node[mynode,draw,line width=2pt,text width=1.5cm, below left=2cm and -1cm of l1] (l2_2) {Study 2 };
\node[mynode,draw,line width=2pt,text width=1.5cm, below right=2cm and 5.5cm of l1] (l2_3) {Study I };
%Third level
\node[mynode,draw=black,line width=1pt,text width=0.8cm, below left=2cm and 1cm of l2_1] (l3_1) {$M_1$};
\node[mynode,draw,line width=1pt,text width=0.8cm, below left=2cm and -0.5cm of l2_1] (l3_2) {$M_2$};
\node[mynode,draw,line width=1pt,text width=0.8cm, below left=2cm and -3cm of l2_1] (l3_3) {$M_N$};
\node[mynode,draw=black,line width=1pt,text width=0.8cm, below left=2cm and 0cm of l2_2] (l3_4) {$M_1$};
\node[mynode,draw,line width=1pt,text width=0.8cm, below left=2cm and -1.5cm of l2_2] (l3_5) {$M_2$};
\node[mynode,draw,line width=1pt,text width=0.8cm, below left=2cm and -4cm of l2_2] (l3_6) {$M_N$};
\node[mynode,draw=black,line width=1pt,text width=0.8cm, below left=2cm and 0cm of l2_3] (l3_7) {$M_1$};
\node[mynode,draw,line width=1pt,text width=0.8cm, below left=2cm and -1.5cm of l2_3] (l3_8) {$M_2$};
\node[mynode,draw,line width=1pt,text width=0.8cm, below left=2cm and -4cm of l2_3] (l3_9) {$M_N$};
%Fourth level
\node[mynode,draw= white,line width=1pt,text width=0.8cm, below =1.5cm of l3_1] (l4_1) {$k^{1}_{1}$, $n_1$};
\node[mynode,draw= white,line width=1pt,text width=0.8cm, below =1.5cm of l3_2] (l4_2) {$k^{1}_{2}$, $n_1$};
\node[mynode,draw= white,line width=1pt,text width=0.8cm, below =1.5cm of l3_3] (l4_3) {$k^{1}_{3}$, $n_1$};
\node[mynode,draw= white,line width=1pt,text width=0.8cm, below =1.5cm of l3_4] (l4_4) {$k^{2}_{1}$, $n_2$};
\node[mynode,draw= white,line width=1pt,text width=0.8cm, below =1.5cm of l3_5] (l4_5) {$k^{2}_{2}$, $n_2$};
\node[mynode,draw= white,line width=1pt,text width=0.8cm, below =1.5cm of l3_6] (l4_6) {$k^{2}_{3}$, $n_2$};
\node[mynode,draw= white,line width=1pt,text width=0.8cm, below =1.5cm of l3_7] (l4_7) {$k^{I}_{1}$, $n_I$};
\node[mynode,draw= white,line width=1pt,text width=0.8cm, below =1.5cm of l3_8] (l4_8) {$k^{I}_{2}$, $n_I$};
\node[mynode,draw= white,line width=1pt,text width=0.8cm, below =1.5cm of l3_9] (l4_9) {$k^{I}_{3}$, $n_I$};

%letter near boxes
\node[mynode,line width=0pt,text width=1cm,left=-0.2cm of l2_1] (l2_1_a) {\scalebox{1.2}{\textcolor{blue}{$x_1,t_1$}}};
\node[mynode,line width=0pt,text width=1cm,left=-0.2cm of l2_2] (l2_2_a) {\scalebox{1.2}{\textcolor{blue}{$x_2,t_2$}}};
\node[mynode,line width=0pt,text width=1cm,left=-0.2cm of l2_3] (l2_3_a) {\scalebox{1.2}{\textcolor{blue}{$x_I,t_I$}}};




\node[mynode, line width=2pt,text width=1.5cm, right= 1cm of l2_2] (l2_4) {\Huge...};
\node[mynode,  line width=2pt,text width=0.8cm, right=0cm of l3_2] (l3_11) {\huge...};
\node[mynode,  line width=2pt,text width=0.8cm, right=0cm of l3_5] (l3_12) {\huge...};
\node[mynode,  line width=2pt,text width=0.8cm, right=0cm of l3_8] (l3_13) {\huge...};



\node[mynode,line width=0pt,text width=4cm,above=-1.8cm of l1] (l1_a) {\scalebox{1.5}{{\textcolor{green}{$\alpha^1$, $\alpha^2$,$\cdots$, $\alpha^M$}}}};
%%---------------------------------------------------------------------------------------------
%Comments on the left
\node[mynode,line width=2pt,text width=2.5cm, left =2cm of l2_1] (comment_2) {Study characteristics};
\node[mynode,line width=2pt,text width=2.5cm, above =-1.2cm of comment_2] (comment_1) {Study-level heterogeneity};
\node[mynode,line width=2pt,text width=2.5cm, below =-1.4cm of comment_2] (comment_3) {Mutation-level heterogeneity};
\node[mynode,line width=2pt,text width=2.5cm, below =-0cm of comment_2] (comment_4) {Observed prevalences};


%Equations below
\node[rectangle,line width=0pt,text width=6cm, text height=2cm, below =0cm of l4_4] (equation) {
\scalebox{1.8}{$k_i^m\sim Bin(p_i^m,n_i)$}\\[0.5cm]
\scalebox{1.8}{$\theta_i^m:=\text{logit}(p_i^m)$}\\[0.5cm]
\scalebox{1.8}{$\theta^{m}_{i}=\textcolor{green}{\alpha^{m}}+\textcolor{blue}{\beta x_i}+\textcolor{red}{u_i}+\textcolor{violet}{v_i^m}$}\\[0.5cm]
\scalebox{1.8}{$\textcolor{red}{u_i} \sim \mathcal{N}(0,\tau^2)$}\\[0.5cm]
\scalebox{1.8}{$\textcolor{violet}{v_i^m}\sim \mathcal{N}(0,\sigma_m^2)$}
};

%%---------------------------------------------------------------------------------------------
%arrows
%1st to 2nd level
\draw[ar,black,->] 
  (l1) -- node[left] {\textcolor{red}{$\tilde{u}_{1}$}} (l2_1);
\draw[ar,black,->] 
  (l1) -- node[left] {\textcolor{red}{$\tilde{u}_{2}$}} (l2_2);
\draw[ar,black,->] 
  (l1) -- node[left] {\textcolor{red}{$\tilde{u}_{I}$}} (l2_3);
  
%2nd to 3rd
\draw[ar,black,->] 
  (l2_1) -- node[left] {\textcolor{violet}{$\tilde{v}_{1}^{1}$}} (l3_1);
\draw[ar,black,->] 
  (l2_1) -- node[left] {\textcolor{violet}{$\tilde{v}_{1}^{2}$}} (l3_2);
\draw[ar,black,->] 
  (l2_1) -- node[left] {\textcolor{violet}{$\tilde{v}_{1}^{N}$}} (l3_3);

\draw[ar,black,->] 
  (l2_2) -- node[left] {\textcolor{violet}{$\tilde{v}_{2}^{1}$}} (l3_4);
\draw[ar,black,->] 
  (l2_2) -- node[left] {\textcolor{black}{$\tilde{v}_{2}^{2}$}} (l3_5);
\draw[ar,black,->] 
  (l2_2) -- node[left] {\textcolor{violet}{$\tilde{v}_{2}^{N}$}} (l3_6);
  
  \draw[ar,black,->] 
  (l2_3) -- node[left] {\textcolor{violet}{$\tilde{v}_{I}^{1}$}} (l3_7);
\draw[ar,black,->] 
  (l2_3) -- node[left] {\textcolor{violet}{$\tilde{v}_{I}^{2}$}} (l3_8);
\draw[ar,black,->] 
  (l2_3) -- node[left] {\textcolor{violet}{$\tilde{v}_{I}^{N}$}} (l3_9);



%3rd to 4th
 \draw[dashed] 
  (l3_1) -- node[above] {} (l4_1);
 \draw[dashed] 
  (l3_2) -- node[above] {} (l4_2);
 \draw[dashed] 
  (l3_3) -- node[above] {} (l4_3);
  \draw[dashed] 
  (l3_4) -- node[above] {} (l4_4);
 \draw[dashed] 
  (l3_5) -- node[above] {} (l4_5);
 \draw[dashed] 
  (l3_6) -- node[above] {} (l4_6);
 \draw[dashed] 
  (l3_7) -- node[above] {} (l4_7);
 \draw[dashed] 
  (l3_8) -- node[above] {} (l4_8);
 \draw[dashed] 
  (l3_9) -- node[above] {} (l4_9);

\end{tikzpicture}
\end{figure}

\end{document}
4
  • Add \usepackage[margin=1cm]{geometry}
    – AndréC
    Aug 11 '20 at 11:32
  • To center something that is wider than the textblock you can use a \makebox as described in tex.stackexchange.com/a/16584. Perhaps make the diagram narrower in addition, e.g. by reducing the distance between the nodes and reducing the width of the nodes. By the way, when you have line width=1pt,text width=1.5cm in the mynode style, you don't need them in every node. (@AndréC While that in a way might do the trick for this one figure, if it is part of a larger document I doubt having 1cm margins is a good idea ...) Aug 11 '20 at 11:59
  • @TorbjørnT. That's why I made a comment (and not an answer), because we don't know whether the document is one or several pages long.
    – AndréC
    Aug 11 '20 at 12:03
  • You received two answers ... no one solve your problem?
    – Zarko
    Sep 3 '20 at 14:27
0

Your image present tree diagram. Why you not draw as tree? For example using forest package:

\documentclass[landscape]{article}
\usepackage{geometry}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\usepackage{forest}
\usetikzlibrary{arrows.meta,
                calc,
                positioning}

\usepackage{amsmath, amssymb}
\tikzset{
node distance = 4mm and 2mm,
   ELS/.style = {% Edge Label Style
                 pos=0.5, fill=white, inner sep=2pt, 
                 node font=\footnotesize, anchor=center},
        }

\begin{document}
\pagestyle{empty}
    \begin{figure}
    \centering
\begin{forest}
    for tree = {
        ellipse, draw, line width=1pt,
        inner xsep=1pt,
        align= center,
    if n=3{draw=none,minimum width=3em, font=\huge}{},
if level = 2{math content}{},
if level = 3{rectangle, draw=none}{},
EL/.style={edge label={node[ELS]{$#1$}}},
%
        edge={-Stealth, semithick},
        s sep=2mm,
if level = 0{l sep=6mm}{l sep=18mm}
                }
%First level
[Baseline\\ prevalence,
 label={[color=teal,font=\Large]{$\alpha^1$, $\alpha^2$,$\cdots$, $\alpha^M$}},
 name=A
    [Study 1, EL=\color{red}\tilde{u}_{1},
     label={[color=blue,font=\Large]left:{$x_1,t_1$}}, 
     name=B
        [M_1, name=C, EL=\color{purple}\tilde{u}^{1}_{1},
            [{$k^{1}_{1}$,\\ $n_1$},edge=dashed, name=D]
        ]
        [M_2, EL=\color{purple}\tilde{u}^{2}_{1}
            [{$k^{2}_{1}$,\\ $n_1$}, edge=dashed]
        ]
        [\dots,no edge]
        [M_N, EL=\color{purple}\tilde{u}^{N}_{1} 
            [{$k^{N}_{1}$,\\ $n_1$}, edge=dashed]
        ]
    ]
    [Study 2, EL=\color{red}\tilde{u}_{2},
     label={[color=blue,font=\Large]left:{$x_2,t_2$}},
        [M_1, EL=\color{purple}\tilde{u}^{1}_{2}
            [{$k^{2}_{2}$,\\ $n_2$}, edge=dashed]
        ]
        [M_2, EL=\color{purple}\tilde{u}^{2}_{2}
            [{$k^{2}_{2}$,\\ $n_2$}, edge=dashed]
        ]
        [\dots,no edge]
        [M_N, EL=\color{purple}\tilde{u}^{N}_{2}
            [{$k^{N}_{2}$,\\ $n_2$}, edge=dashed]
        ]
    ]
    [\dots, inner sep=3em, no edge]
    [Study I, EL=\color{red}\tilde{u}_{I},
     label={[color=blue,font=\Large]left:{$x_I,t_I$}}
        [M_1, EL=\color{purple}\tilde{u}^{1}_{I}
            [{$k^{1}_{I}$,\\ $n_2$}, edge=dashed]
        ]
        [M_2, EL=\color{purple}\tilde{u}^{2}_{I}
            [{$k^{2}_{I}$,\\ $n_2$}, edge=dashed]
        ]
        [\dots,no edge]
        [M_N, EL=\color{purple}\tilde{u}^{N}_{I}
            [{$k^{N}_{I}$,\\ $n_2$}, edge=dashed]
        ]
    ]
]
    \begin{scope}[nodes={rectangle, align=right}]
\node[left=of C.west]               {Observed\\ prevalences};
\node[left=of $(C.west)!0.5!(B -| C.west)$]   {Mutation-level\\ heterogeneity};
\node[left=of C.west |- B]          {Study\\ characteristics};
\node[left=of $(B -| C.west)!0.5!(A -| C.west)$]    {Study-level\\ heterogeneity};
    \end{scope}
%Equations below
\node[align=center, font=\Large, below=of D.south -| A] 
{
$k_i^m \sim Bin(p_i^m,n_i)$    \\
%
$\begin{aligned}
\theta_i^m      & \colon=\text{logit}(p_i^m)
&   
\theta^{m}_{i}  & =\textcolor{teal}{\alpha^{m}}
        +\textcolor{blue}{\beta x_i}
        +\textcolor{red}{u_i}
        +\textcolor{violet}{v_i^m}  \\
\textcolor{red}{u_i}
                & \sim \mathcal{N}(0,\tau^2)
& 
\textcolor{violet}{v_i^m}
                & \sim \mathcal{N}(0,\sigma_m^2)
\end{aligned}$
};
\end{forest}
    \end{figure}
\end{document}

enter image description here

As you can see, diagram is narrower as was obtained with your code, distance between codes are simple to determine by options s sep and lsep, and consequently it is perfectly centered in text area. Another advantage is shorter (and consistent) code :-)

0

Your problem comes from the use of landscape without taking care of the page layout afterward. The two images below represent the output of my file and of yours, respectively, with the instruction \usepackage{showframe} added. This instruction makes visible the page layout. enter image description here enter image description here

As you can see, your problem is easily fixed. Now, I have started to modify (slightly) your code to make it easier to read and use. This was before my understanding of the problem. The part that inteserts you most is at the beginning of the code below.

\documentclass[landscape]{article}
%%% 297 x 210 mm  --- 1mm=2.84 pts, so the page in pts is 843 x 596
\setlength{\textwidth}{660pt}  % 8
\setlength{\oddsidemargin}{20pt}  % 3
\setlength{\marginparwidth}{60pt}  % 10
\setlength{\textheight}{440pt}  % 7
\setlength{\voffset}{-10pt}  % 2
\setlength{\topmargin}{1pt}  % 4
\setlength{\headsep}{15pt}  % 6

\usepackage{showframe}

\usepackage[nomessages]{fp}
\usepackage{changepage}

\usepackage{tikz}
\usetikzlibrary{positioning}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{xcolor}

\pagestyle{empty}

\begin{document}

\tikzset{%
  node distance=1 cm and 0.2cm,
  every node/.style={circle, minimum size=0.2cm, align=center},
  twonode/.style={draw, line width=2pt, text width=1.5cm},
  threenode/.style={draw, line width=1pt, text width=0.8cm},
  fournode/.style={text width=0.8cm},
  twol/.style={left, rectangle, inner sep=2pt},
  ar/.style={->, >=latex, thick}
}

\begin{figure}
  \begin{tikzpicture}[
  mynode/.style={circle,
    line width=1pt,
    text width=1.5cm,
    minimum size=0.2 cm,
    align=center}
  ]

  % First level
  \node[draw, line width=2pt, text width=1.5cm] (l1)
  [label={[rectangle, scale=1.5, color=green!50!black]
    90:$\alpha^1, \alpha^2,\cdots, \alpha^M$}]
  {Baseline prevalence};
  
  % Second level
  \node[twonode, below left=2cm and 4cm of l1] (l2_1)
  [label={[twol]180:$x_1,t_1$}] {Study 1};
  \node[twonode, below left=2cm and -1cm of l1] (l2_2)
  [label={[twol]180:$x_2,t_2$}] {Study 2};
  \node[twonode, below right=2cm and 5.5cm of l1] (l2_3)
  [label={[twol]180:$x_3,t_3$}] {Study I};
  
  % Third level
  \node[threenode, below left=2cm and 1cm of l2_1] (l3_1) {$M_1$};
  \node[threenode, below left=2cm and -0.5cm of l2_1] (l3_2) {$M_2$};
  \node[threenode, below left=2cm and -3cm of l2_1] (l3_3) {$M_N$};
  \node[threenode, below left=2cm and 0cm of l2_2] (l3_4) {$M_1$};
  \node[threenode, below left=2cm and -1.5cm of l2_2] (l3_5) {$M_2$};
  \node[threenode, below left=2cm and -4cm of l2_2] (l3_6) {$M_N$};
  \node[threenode, below left=2cm and 0cm of l2_3] (l3_7) {$M_1$};
  \node[threenode, below left=2cm and -1.5cm of l2_3] (l3_8) {$M_2$};
  \node[threenode, below left=2cm and -4cm of l2_3] (l3_9) {$M_N$};

  % Fourth level
  \node[fournode, below =1.5cm of l3_1] (l4_1) {$k^{1}_{1}$, $n_1$};
  \node[fournode, below =1.5cm of l3_2] (l4_2) {$k^{1}_{2}$, $n_1$};
  \node[fournode, below =1.5cm of l3_3] (l4_3) {$k^{1}_{3}$, $n_1$};
  \node[fournode, below =1.5cm of l3_4] (l4_4) {$k^{2}_{1}$, $n_2$};
  \node[fournode, below =1.5cm of l3_5] (l4_5) {$k^{2}_{2}$, $n_2$};
  \node[fournode, below =1.5cm of l3_6] (l4_6) {$k^{2}_{3}$, $n_2$};
  \node[fournode, below =1.5cm of l3_7] (l4_7) {$k^{I}_{1}$, $n_I$};
  \node[fournode, below =1.5cm of l3_8] (l4_8) {$k^{I}_{2}$, $n_I$};
  \node[fournode, below =1.5cm of l3_9] (l4_9) {$k^{I}_{3}$, $n_I$};

  \node[text width=1.5cm, right= 1cm of l2_2] (l2_4) {\Huge...};
  \node[text width=0.8cm, right=0cm of l3_2] (l3_11) {\huge...};
  \node[text width=0.8cm, right=0cm of l3_5] (l3_12) {\huge...};
  \node[text width=0.8cm, right=0cm of l3_8] (l3_13) {\huge...};
  
  % Comments on the left
  \node[mynode,line width=2pt,text width=2.5cm, left =2cm of l2_1] (comment_2) {Study characteristics};
  \node[mynode,line width=2pt,text width=2.5cm, above =-1.2cm of comment_2] (comment_1) {Study-level heterogeneity};
  \node[mynode,line width=2pt,text width=2.5cm, below =-1.4cm of comment_2] (comment_3) {Mutation-level heterogeneity};
  \node[mynode,line width=2pt,text width=2.5cm, below =-0cm of comment_2] (comment_4) {Observed prevalences};


%Equations below
\node[rectangle,line width=0pt,text width=6cm, text height=2cm, below =0cm of l4_4] (equation) {
\scalebox{1.8}{$k_i^m\sim Bin(p_i^m,n_i)$}\\[0.5cm]
\scalebox{1.8}{$\theta_i^m:=\text{logit}(p_i^m)$}\\[0.5cm]
\scalebox{1.8}{$\theta^{m}_{i}=\textcolor{green}{\alpha^{m}}+\textcolor{blue}{\beta x_i}+\textcolor{red}{u_i}+\textcolor{violet}{v_i^m}$}\\[0.5cm]
\scalebox{1.8}{$\textcolor{red}{u_i} \sim \mathcal{N}(0,\tau^2)$}\\[0.5cm]
\scalebox{1.8}{$\textcolor{violet}{v_i^m}\sim \mathcal{N}(0,\sigma_m^2)$}
};

%%---------------------------------------------------------------------------------------------
%arrows
%1st to 2nd level
\draw[ar, black,->] 
  (l1) -- node[left] {\textcolor{red}{$\tilde{u}_{1}$}} (l2_1);
\draw[ar, black,->] 
  (l1) -- node[left] {\textcolor{red}{$\tilde{u}_{2}$}} (l2_2);
\draw[ar, black,->] 
  (l1) -- node[left] {\textcolor{red}{$\tilde{u}_{I}$}} (l2_3);
  
%2nd to 3rd
\draw[ar,black,->] 
  (l2_1) -- node[left] {\textcolor{violet}{$\tilde{v}_{1}^{1}$}} (l3_1);
\draw[ar,black,->] 
  (l2_1) -- node[left] {\textcolor{violet}{$\tilde{v}_{1}^{2}$}} (l3_2);
\draw[ar,black,->] 
  (l2_1) -- node[left] {\textcolor{violet}{$\tilde{v}_{1}^{N}$}} (l3_3);

\draw[ar,black,->] 
  (l2_2) -- node[left] {\textcolor{violet}{$\tilde{v}_{2}^{1}$}} (l3_4);
\draw[ar,black,->] 
  (l2_2) -- node[left] {\textcolor{black}{$\tilde{v}_{2}^{2}$}} (l3_5);
\draw[ar,black,->] 
  (l2_2) -- node[left] {\textcolor{violet}{$\tilde{v}_{2}^{N}$}} (l3_6);
  
  \draw[ar,black,->] 
  (l2_3) -- node[left] {\textcolor{violet}{$\tilde{v}_{I}^{1}$}} (l3_7);
\draw[ar,black,->] 
  (l2_3) -- node[left] {\textcolor{violet}{$\tilde{v}_{I}^{2}$}} (l3_8);
\draw[ar,black,->] 
  (l2_3) -- node[left] {\textcolor{violet}{$\tilde{v}_{I}^{N}$}} (l3_9);

%3rd to 4th
 \draw[dashed] (l3_1) -- node[above] {} (l4_1);
 \draw[dashed] (l3_2) -- node[above] {} (l4_2);
 \draw[dashed] (l3_3) -- node[above] {} (l4_3);
 \draw[dashed] (l3_4) -- node[above] {} (l4_4);
 \draw[dashed] (l3_5) -- node[above] {} (l4_5);
 \draw[dashed] (l3_6) -- node[above] {} (l4_6);
 \draw[dashed] (l3_7) -- node[above] {} (l4_7);
 \draw[dashed] (l3_8) -- node[above] {} (l4_8);
 \draw[dashed] (l3_9) -- node[above] {} (l4_9);
\end{tikzpicture}
\end{figure}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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