# Why tikzpicture appears on a separated page?

I am beginner in tikz environment, so I am really sorry if this question seems quite naive. I had a flow chart drawn using tikzpicture, as you see its not that complicated. The problem is I want the flow chart do be directly after the last text, not on a separated page! It appears on a separated page anyway, regardless of its size! Any one could kindly help?

This is my code...

\documentclass[English,MSC,Times,NoIndent,IEEE]{mfthesis}
\usepackage{layout}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric,arrows}
\usepackage{ragged2e}
\usepackage{times}
\usepackage{float}
\usepackage{amsmath}
\usepackage{mathtools}

\begin{document}

a
b
c
d
e
f
g
h
i
j
k
l
m

\tikzstyle{rect} = [draw, rectangle, fill=gray!10, text width=6em, text centered, minimum height=2em]
\tikzstyle{elli} = [draw, ellipse, fill=white!20, node distance=3cm, minimum height=2em]
\tikzstyle{circ} = [draw, circle, fill=white!20, node distance=3cm, minimum width=8pt, inner sep=10pt]
\tikzstyle{diam} = [draw, diamond, fill=gray!20, text width=6em, text badly centered, outer sep=0, inner sep=0pt]
\tikzstyle{line} = [draw, -latex']
\begin{figure}
\begin{center}
\begin{tikzpicture}
% Place nodes
\node [rect, rounded corners=10pt, line width=0.5mm, node distance = 2cm] (step1) {start};
\node [rect, below of=step1, line width=0.5mm, node distance = 2cm] (step2) {Measure $i_{dq}(k),\theta(k)$};
\node [rect, below of=step2, line width=0.5mm, node distance = 2cm] (step3) {n$=$0};
\node [rect, below of=step3, line width=0.5mm, node distance = 2cm] (step4) {n$=$n$+$1};
\node [rect, below of=step4, text width=10em, minimum height=3em, line width=0.5mm, node distance = 2cm] (step5) {Calculate $i_{dq}^p(k+1)$ from Eqns 3.13, 3.14};
\node [rect, below of=step5, text width=14em, line width=0.5mm, node distance = 2cm](step6) {Calculate cost function\\ $g=(i_d^*-i_d^p(k+1))^2+(i_q^*-i_q^p(k+1))^2$};
\node [diam, below of=step6, line width=0.5mm, node distance=3cm] (step7) {n$>$7};
\node [rect, below of=step7, text width=10em, line width=0.5mm, node distance=3cm] (step8) {Apply switching state};
\node [rect, minimum height=3em, left of=step5, line width=0.5mm, node distance=5cm] (step9) {Next sampling period};
% Draw edges
\path [line, line width=0.75mm] (step1) -- (step2);
\path [line, line width=0.75mm] (step2) -- (step3);
\path [line, line width=0.75mm] (step2) -- (step3);
\path [line, line width=0.75mm] (step3) -- (step4);
\path [line, line width=0.75mm] (step4) -- (step5);
\path [line, line width=0.75mm] (step5) -- (step6);
\path [line, line width=0.75mm] (step6) -- (step7);
\path [line, line width=0.75mm] (step7) -- node [right,near start]{True}(step8);
\path [line, rounded corners, line width=0.75mm] (step7) --++ (4,0) node [above,near start]{False}|-(step4);
\path [line, rounded corners, line width=0.75mm] (step8) -| (step9);
\path [line, rounded corners, line width=0.75mm] (step9) |- (step2);
\end{tikzpicture}
\caption{Flow diagram of motor current control using FS-MPC}
\label{Fig.4.9}
\end{center}
\end{figure}

\end{document}

• A figure is a float, and as such tends to float. There is the float package, which you load, with which you can use \begin{figure}[H]... but I do not recommend it. Rather refer to the figure in the text and let it float. Please also note that \tikzstyle is deprecated. Also your way of relative positioning of TikZ nodes is.
– user194703
Apr 24, 2020 at 19:56
• @Schrödinger'scat Thank you. I understood the float package issue. But regarding my way of using tikzstyle and relative positioning of TikZ... how do you recommend my script to look like? could you please write a couple of lines describing? Apr 24, 2020 at 20:17
• See here for a discussion of \tikzstyle and use the positioning library for positioning. After loading this library you need to change keys to the type below of=step1, to below=of step1,, and adjust the distances.
– user194703
Apr 24, 2020 at 20:20
• Use of \begin{center} ... \end{center} adds more height than necessary and can make a float too large to fit in the space available. Use \centering instead. Apr 24, 2020 at 23:21

It's because you've put the tikzpicture inside a figure environment. Figure environments are floats, which means that LaTeX will move them to the next page if it thinks that doing so gives the best page layout.

There are options you can use to influence what LaTeX does with floats - see

https://www.overleaf.com/learn/latex/Positioning_of_Figures

for good information on this. Or you could just not use the figure environment, and use an alternative to \caption for the title of your tikzpicture.

• Thank you. I removed the figure environment, and the float effect has disappeared. But the caption has disappeared, too! Also I want picture'slabel to appear because I want it exist in the list of figures??? Apr 24, 2020 at 20:13

You have two problems:

• you not use figure placement option, I suggest you to use htbp (see wiki/LaTeX/Floats,Figures and Captions)
• your figure is huge and occupancy more space as is (default) reserved for floats on each page. This you can solve on two ways:
• reduce image size, for example as shown in MWE below (it use consistent smaller distance between nodes)
• change default settings for amount of text on page (see Controling LaTeX floats)
\documentclass{article}%[English,MSC,Times,NoIndent,IEEE]{mfthesis}
\usepackage{layout}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
chains,
positioning,
shapes.geometric}
\usepackage{ragged2e}
\usepackage{times}
\usepackage{float}
%\usepackage{amsmath}

%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\begin{document}

a
b
c
d
e
f
g
h
i
j
k
l
m

\begin{figure}[htbp]
\centering
\begin{tikzpicture}[
node distance = 8mm and 12mm,
start chain = going below,
base/.style = {draw, thick, fill=gray!10, align=center,
inner xsep=3mm, inner ysep=2mm},
rect/.style = {base},
elli/.style = {ellipse, base},
circ/.style = {circle, fill=graye!10, minimum size=12pt},
diam/.style = {diamond, base, aspect=1.5},
line/.style = {draw, rounded corners, -Stealth, semithick},
]
% Place nodes
\begin{scope}[nodes = {on chain, join=by line}]
\node [rect, rounded corners=10pt] (step1) {start};
\node [rect] (step2) {Measure\\
$i_{dq}(k),\theta(k)$};
\node [rect] (step3) {n$=$0};
\node [rect] (step4) {n$=$n$+$1};
\node [rect] (step5) {Calculate $i_{dq}^p(k+1)$\\
from Eqns 3.13, 3.14};
\node [rect] (step6) {Calculate cost function\\
$\begin{multlined} g = (i_d^*-i_d^p(k+1))^2 \\ +(i_q^*-i_q^p(k+1))^2 \end{multlined}$};
\node [diam] (step7) {n$>$7};
\node [rect] (step8) {Apply switching state};
\end{scope}
\node [rect, left=of step5] (step9) {Next sampling period};
% Draw edges
\path[line] (step8) -| (step9);
\path[line] (step9) |- (step2);
%
\path       (step7) -- node [right,near start]{True}    (step8);
\draw[line] (step7) --++ (3,0) node [above,near start]{False} |-(step4);
\end{tikzpicture}
\caption{Flow diagram of motor current control using FS-MPC}
\label{Fig.4.9}
\end{figure}
\end{document}


Note:

• use of tikzstyle is deprecated. Instead it use tikzset or enclose styles settings in tikzpicutere options as it is done in above MWE
• to have equal distance between nodes, use positioning library and its syntax, for example below=of <coordinate name>
• image code can be noticable shortened by use of the chains library and its macro join=by <style name>
• Thank you. That is very professional, and it worked very well for me. By the way, I have used figure placement [H] (here exactly) for all figures in my thesis, because it gives me full control on figure positioning! Don't you recommend using figure placement in general? if so, what do you recommend yo use instead? Apr 25, 2020 at 23:40
• @AliBashabsheh, you stick with other answer, so expect that it solve all your problems with floats placements ... A, using H prevent LaTeX to help you in design beautiful document where floats is float, where is the best place for them. Its use is desperate try of last resort for their placement. But its use is up to you. That you not need to write at each float (figure, table) placement option, you can add to preamble \makeatletter \def\fps@figure{hbtp} \makeatother. BTW, instead saying "thank you" you could ip-vote my answer. Apr 26, 2020 at 0:12