How can I get my table or figure to stay where they are, instead of going to the next page?

We can still influence where we'd like the figure to appear, though. If you add [hbt!] after the \begin , like this:

\beginfigure>[hbt!]

this then tells LaTeX to place the image right here (as close to the position in the source code as possible); or if that's not possible (e.g. it's too large to fit on the current page) at the top of the next page, or bottom of the next page. The exclamation mark ! is important: it tells LaTeX "please really respect my choices here".

However, if your figure is quite tall and there is not enough space on the current page (e.g. if it's already near the end of the current page), then the figure will still be moved to the top of the next page. In this case you might try moving your \begin[hbt!]. \end code a few paragraphs earlier in the code, where there is still more available on the current page at that point.

If that still doesn't work, and your figures/tables have then floated on ahead to appear after the next \section heading, you may prefer them to still appear at the end of the current section. You may then issue a \clearpage before the next \section to insert a pagebreak, which will also output all floats in the queue, before the next \section heading is printed on a new page.

Alternatively, have a look at the placeins package, where you can add \FloatBarrier before the next \section —or automate this by using the section package option, i.e. write \usepackage[section] in your preamble.

Overleaf guides

LaTeX Basics

Mathematics

Figures and tables

References and Citations

Languages

Document structure

Formatting

Fonts

Presentations

Commands

Field specific

Class files

Advanced TeX/LaTeX