# Why Tikz picture doesn't position after text?

I'm writing a report that is divided into sections. Each section has figures that I produced in MATLAB and used the matlab2tikz function to get the .tex file of the tikz figure. The problem is that I want the figure to be right after the text. I place the .tex file in a \begin{figure} but it doesn't matter what positions specifiers (I used [htpb]) I use or how much I reduce its height (the white space left is larger than the figure), the figure is positioned on a page of its own. The position specifiers work fine on simpler documents, but I don't know why they don't work in this case and none of the other answers to similar questions seem to work.

\documentclass[letterpaper,12pt]{article}
\usepackage{tabularx} % extra features for tabular environment
\usepackage{amsmath}  % improve math presentation
\usepackage{graphicx} % takes care of graphic including machinery
\usepackage[margin=1in,letterpaper]{geometry} % decreases margins
\usepackage{cite} % takes care of citations
\usepackage{natbib}  % a common set for bibliography
\usepackage{booktabs} % For table presentation
\usepackage[final]{hyperref} % adds hyper links inside the generated pdf file
\usepackage[export]{adjustbox}[2011/08/13] %for figures wider than \textwidth
\usepackage[toc]{appendix}
\usepackage[section]{placeins} %floats appear in their respective section
\usepackage[most]{tcolorbox} %boxes around question text
% For code readability %%%%%%%%%%%%%%%%%%%%%
\usepackage{mleftright}
\usepackage{amssymb}
\usepackage{listings}
% TIKZ==============================
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfgantt}
\usepackage{pdflscape}
\pgfplotsset{compat=newest}
\pgfplotsset{plot coordinates/math parser=false}
\usepackage[mode=build]{standalone}
%===================================
\usepackage{float}
\usepackage{xcolor}

\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}

\lstdefinestyle{mystyle}{
backgroundcolor=\color{backcolour},
commentstyle=\color{codegreen},
keywordstyle=\color{magenta},
numberstyle=\tiny\color{codegray},
stringstyle=\color{codepurple},
basicstyle=\ttfamily\footnotesize,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
keepspaces=true,
numbers=left,
numbersep=5pt,
showspaces=false,
showstringspaces=false,
showtabs=false,
tabsize=2
}

\lstset{style=mystyle}
%++++++++++++++++++++++++++++++++++++++++
\newcommand\diam{0.008} %m

\begin{document}

\title{Laboratory \# 2}
\author{Name}
\date{October 18, 2020}
\maketitle

\section{Introduction}
In this experiment, we studied a round jet by measuring the velocity components at different single points in space using the acoustic Doppler velocimetry technique. Round jets are ubiquitous in nature and are one of the most studied free-shear turbulent flows for this reason. The round jet is a physical system that represents an important example of momentum transport; for this reason, its velocity is the most studied characteristic. Acoustic Doppler velocimetry is one of many techniques used to record instantaneous velocity components at a single point.

\begin{figure}[htpb]
\centering
\input{Figures/U0.tex}
\caption{Normalized $\overline{U}$ velocity profile.}
\label{fig:U0dataplot}
\end{figure}
\FloatBarrier
%\newpage

\subsection{Velocity profiles}

\bibliographystyle{apalike}
\bibliography{bibliography}
\newpage
\appendix
\section{MATLAB codes}

\end{document}


I use a \FloatBarrier to keep the figures within their respective sections. Heres the output from matlab2tiks

% This file was created by matlab2tikz.
%
%The latest updates can be retrieved from
%  http://www.mathworks.com/matlabcentral/fileexchange/22022-matlab2tikz-matlab2tikz
%where you can also make suggestions and rate matlab2tikz.
%
\definecolor{mycolor1}{rgb}{0.00000,0.44700,0.74100}%
%
\begin{tikzpicture}

\begin{axis}[%
width=5.0in,
height=1.0in,
at={(1.011in,0.8in)},
scale only axis,
xmin=-0.581723610346427,
xmax=1.74517083103928,
xlabel style={font=\color{white!15!black}},
xlabel={$z/r_{1/2}$},
ymin=0,
ymax=0.4,
ylabel style={font=\color{white!15!black}},
ylabel={$\overline{U}$ (m/s)},
axis background/.style={fill=white},
xmajorgrids,
ymajorgrids,
legend style={legend cell align=left, align=left, draw=white!15!black}
]
\addplot [color=mycolor1, dashed, line width=1.0pt, mark size=4.0pt, mark=asterisk, mark options={solid, mycolor1}]
table[row sep=crcr]{%
-0.581723610346427  0.367945478013166\\
-0.290861805173213  0.38556030385145\\
0   0.392450367769602\\
0.290861805173213   0.354851219620763\\
0.581723610346426   0.315362891737888\\
0.872585415519639   0.252236554603184\\
1.16344722069285    0.124373889086379\\
1.45430902586606    0.0570082949718708\\
1.74517083103928    0.021156242362299\\
};
\addlegendentry{$\overline{U}$}

\addplot [color=red, line width=1.0pt, only marks, mark size=2.0pt, mark=*, mark options={solid, red}]
table[row sep=crcr]{%
1   0.196225183884801\\
};
\addlegendentry{$\overline{U}(r_{1/2})$}

\addplot [color=red, dotted, line width=2.0pt, forget plot]
table[row sep=crcr]{%
1   0\\
1   0.196225183884801\\
};
\end{axis}

\begin{axis}[%
width=7.778in,
height=5.833in,
at={(0in,0in)},
scale only axis,
xmin=0,
xmax=1,
ymin=0,
ymax=1,
axis line style={draw=none},
ticks=none,
axis x line*=bottom,
axis y line*=left
]
\end{axis}
\end{tikzpicture}%

• Welcome to TeX.SE! Your image is very huge and cant be placed on one page!. What is purpose the second axis in it? To it is not asociated anything, it only make image huge (7.778in, height=5.833in,) . If I remove it, image appear in text as you like to have. Oct 21 '20 at 8:13
• @Zarko The code was generated by matlab2tikz, so possibly something went wrong when exporting from Matlab. Oct 21 '20 at 12:26
• @TorbjørnT., it can be. Unfortunately I newer use matlab2tikz, so I'm not familiar with it I will provide correct code for image asap. Oct 21 '20 at 12:49

## 1 Answer

It seem that your problem lay in tikzpicture code. It contain not used settings for axis, which require image size 7.778in, height=5.833in. This cause huge white space above and right of your image. Consequently, it can't be fit in one page, so it is pushed on separate page where it spill out of page.

So, I would inspect all images generated by matlab2tikz package with tests using standalone document class and if will be necessary manually correct it as I did in your image code bellow:

\documentclass[margin=3.14159]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\definecolor{mycolor1}{rgb}{0.00000,0.44700,0.74100}%

\begin{document}
\begin{tikzpicture}
\begin{axis}[width=5.0in, height=1.0in,
%at={(1.011in,0.8in)},      % <---
scale only axis,
xmin=-0.6,                  % <---
xmax=1.75,                  %
xlabel style={font=\color{white!15!black}},
xlabel={$z/r_{1/2}$},
ymin=0,
ymax=0.4,
ylabel style={font=\color{white!15!black}},
ylabel={$\overline{U}$ (m/s)},
%axis background/.style={fill=white},
xmajorgrids,
ymajorgrids,
legend style={legend cell align=left, align=left, draw=white!15!black}
]
\addplot [color=mycolor1, dashed, line width=1.0pt,
mark size=4.0pt, mark=asterisk, mark options={solid, mycolor1}]
table[row sep=crcr]{%
-0.581723610346427  0.367945478013166\\
-0.290861805173213  0.38556030385145\\
0                   0.392450367769602\\
0.290861805173213   0.354851219620763\\
0.581723610346426   0.315362891737888\\
0.872585415519639   0.252236554603184\\
1.16344722069285    0.124373889086379\\
1.45430902586606    0.0570082949718708\\
1.74517083103928    0.021156242362299\\
};
\addlegendentry{$\overline{U}$}

\addplot [color=red, line width=1.0pt,
only marks, mark size=2.0pt, mark=*, mark options={solid, red}]
table[row sep=crcr]{%
1   0.196225183884801\\
};
\addlegendentry{$\overline{U}(r_{1/2})$}

\addplot [color=red, dotted, line width=2.0pt, forget plot]
table[row sep=crcr]{%
1   0\\
1   0.196225183884801\\
};
\end{axis}
% deleted second axis setings, it cause your problem
\end{tikzpicture}
\end{document}


which gives the following result:

This result you can include in your main document as:

• pdf image with \includegraphics{<name of file>}
• as tex file, which contain your standalone file as you did in your document example

In bot h cases you will get the same result. For example, making MWE (Minimal Working Example) from your document example, result is:

MWE:

\documentclass[letterpaper,12pt]{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\definecolor{mycolor1}{rgb}{0.00000,0.44700,0.74100}%
\usepackage[mode=build]{standalone}

\usepackage{lipsum}

\begin{document}
\title{Laboratory \# 2}
\author{Name}
\date{October 18, 2020}
\maketitle

\section{Introduction}
\lipsum[11]

\begin{figure}[htpb]
\centering
\input{Figures/U0.tex}
\caption{Normalized $\overline{U}$ velocity profile.}
\label{fig:U0dataplot}
\end{figure}

\subsection{Velocity profiles}
\lipsum[12]

\appendix
\section{MATLAB codes}

\end{document}

• Thanks for the answer and the welcome! Oct 22 '20 at 23:17