Text FormattingSection Positioning between floating Figures

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
latex_newbie
Posts: 7
Joined: Tue Jul 03, 2012 3:09 pm

Section Positioning between floating Figures

Post by latex_newbie »

Code: Select all

\documentclass{article}
\usepackage{tkz-euclide}
\usetikzlibrary{trees}

\begin{document}
\section{Tikz examples}
\begin{figure}[h]
\begin{tikzpicture}
    % The triangle
    \tkzDefPoint(2,2){A}
    \tkzDefPoint(5,-2){B}
    \tkzDefPoint(1,-2){C}
    \tkzDrawSegments(A,B B,C C,A)
\end{tikzpicture}
\end{figure}

\begin{figure}[h]
{...]
\end{figure}

[...]

\section{Parallel lines and angles}

[...]

I have a documet with a few diagrams made with Tikz. There's the 1st section with 7 diagrams, which end on page 4. Then I make another section and add a few more examples. Unfortunately, I don't understand why is the section title "Parallel lines and angles" displayed on the bottom of the 1st page, instead of on the top of the new 5th page.

I'm browsing through various "latex-for-beginners" and tikz manuals but I still cannot find anything that would explain why this happens. When I have an article with only word problems, then the \sections work perfectly fine.

Thank you in advance for any tips on this one.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Section Positioning between floating Figures

Post by Stefan Kottwitz »

figure environments are floating through the text, for best positioning and optimal line breaks. You can customize it, the easierst way is to change h to !htbp for very relaxed positoning. Try:

Code: Select all

\begin{figure}[!htbp]
...
For an explanation, have a look at: Order of appearance of tables and figures.

If you don't want floating at all, remove the figure environment, i.e. write

Code: Select all

\section{Tikz examples}
\begin{tikzpicture}
    % The triangle
    \tkzDefPoint(2,2){A}
    \tkzDefPoint(5,-2){B}
    \tkzDefPoint(1,-2){C}
    \tkzDrawSegments(A,B B,C C,A)
\end{tikzpicture}

\begin{tikzpicture}
...
\end{tikzpicture}

[...]

\section{Parallel lines and angles}
Stefan
LaTeX.org admin
latex_newbie
Posts: 7
Joined: Tue Jul 03, 2012 3:09 pm

Section Positioning between floating Figures

Post by latex_newbie »

Hm, no, unfortunately it didn't work. :(

I changed all "[h]s" to "[htbp]s" yet still the title of my second section is displayed on the bottom of my 1st page.

As for floating, I figured I had to use it, otherwise there were no spaces between the diagrams and it just looked ugly. The commands for breaking lines or creating blank vertical spaces ("\\", "\newline", "vspace{12pt}", etc.) didn't want to work and I accidentally noticed that using figures seem to fix it.

But this impression lasted only till I tried to include section in my document.

What's curious, the example from the popular TikZ and PGF Manual by Till Tantau doesn't work inside inside the figure environment either (at least not within my document, if I put it only this piece of code into a new document without other graphics it's OK).

Here's the code:

Code: Select all

\section{Parallel lines and angles}
\begin{figure}[htbp]
\begin{tikzpicture}
  \draw[fill=yellow] (0,0) -- (60:.75cm) arc (60:180:.75cm);
  \draw(120:0.4cm) node {$\alpha$};

  \draw[fill=green!30] (0,0) -- (right:.75cm) arc (0:60:.75cm);
  \draw(30:0.5cm) node {$\beta$};

  \begin{scope}[shift={(60:2cm)}]
    \draw[fill=green!30] (0,0) -- (180:.75cm) arc (180:240:.75cm);
    \draw (30:-0.5cm) node {$\gamma$};

    \draw[fill=yellow] (0,0) -- (240:.75cm) arc (240:360:.75cm);
    \draw (-60:0.4cm) node {$\delta$};
  \end{scope}

  \begin{scope}[thick]
    \draw (60:-1cm) node[fill=white] {$E$} -- (60:3cm) node[fill=white] {$F$};
    \draw[red]                   (-2,0) node[left] {$A$} -- (3,0) 
                                        node[right]{$B$};
    \draw[blue,shift={(60:2cm)}] (-3,0) node[left] {$C$} -- (2,0) 
                                        node[right]{$D$};
  
    \draw[shift={(60:1cm)},xshift=4cm]
    node [right,text width=6cm,rounded corners,fill=red!20,inner sep=1ex]
    {
      When we assume that $\color{red}AB$ and $\color{blue}CD$ are
      parallel, i.\,e., ${\color{red}AB} \mathbin{\|} \color{blue}CD$,
      then $\alpha = \delta$ and $\beta = \gamma$.
    };
  \end{scope}
\end{tikzpicture}
\end{figure}
However, my intention is to include such graphs into my fairly long article with lots of other diagrams. I don't want them to overlap or be too close to each other and I do want them to be separated by sections depending on their theme.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Section Positioning between floating Figures

Post by Stefan Kottwitz »

Alternatively, you could use the placeins package for placing float barriers before sections:

Code: Select all

\usepackage[section]{placeins}
Stefan
LaTeX.org admin
Post Reply