[Tex/LaTex] TikZ figure always positioned at the center of a new page

floatslncspositioningtikz-pgf

I run into this rather strange problem when using TikZ with LyX 2. I have this graph programmed using TikZ, and it is supposed to follow a paragraph of text and be positioned at the top of next new page. Instead, it is positioned exactly at the center of the new page, leaving a chunk of space between the text and itself. In my document, I place the tex code for the figure immediately after the text itself with a space, and the TikZ code is as follows.

\usetikzlibrary{shapes}
\usetikzlibrary{fit}
\usetikzlibrary{calc}
\usetikzlibrary{arrows}
\begin{figure}[ht!]
    \centering
    \begin{tikzpicture} [label distance=2mm,
        vertice/.style={circle, fill=gray!30, thick, inner sep=0pt, minimum size=7mm},
        dot/.style={shape=circle, fill=black, minimum size=2pt, inner sep=0pt, outer sep=2pt}]
        \node [vertice] (a) at (0,7) {$t_1$};
        \node [vertice] (b) at (0,6) {$t_2$};
        \node [vertice] (c) at (0,5) {$t_3$};
        \node [dot] (d) at ($(c) + (270:1)$) {};
        \node [dot] (e) at ($(d) + (270:0.5)$) {};
        \node [dot] (f) at ($(e) + (270:0.5)$) {};
        \node[draw, ellipse, label=below:$T$, fit=(a) (b) (c) (d) (e) (f)] {};\node [vertice] (h) at (5,7) {$a_1$};
        \node [vertice] (i) at (5,6) {$a_2$};
        \node [vertice] (j) at (5,5) {$a_3$};
        \node [dot] (k) at ($(j) + (270:1)$) {};
        \node [dot] (l) at ($(k) + (270:0.5)$) {};
        \node [dot] (m) at ($(l) + (270:0.5)$) {};
        \node[draw, ellipse, label=below:$A$, fit=(h) (i) (j) (k) (l) (m)] {};

        \path (h) edge [-stealth, auto, swap] node {$w_1$} (a)
                  edge [-stealth, auto] node[below, sloped] {$w_2$} (e)
            (i) edge [-stealth, auto, swap] node {$w_3$} (b)
            (j) edge [-stealth, auto] node[below, sloped] {$w_4$} (a)
                  edge [-stealth, auto] node[above, sloped] {...} (f)
              (k) edge [-stealth, auto] node[above, sloped] {...} (c)
              (l) edge [-stealth, auto] node[below, sloped] {...} (d)
              (m) edge [-stealth, auto] node {...} (f);
    \end{tikzpicture}
    \caption{A graph $G$}
\end{figure}

Any suggestion how it can be fixed?

I am using Springer LNCS document class, if that makes any difference.

Best Answer

If the figure happens to be on the very last page with no text following it it is centered as illustrated by the MWE below (if you comment out the \setlength line).

As per Vertical layout of float pages the values that control the distances between floats are:

\@fptop defines the distance from the top of the page to the top of the first float,

\@fpsep defines the separation between floats, and

\@fpbot defines the distance from the bottom of the last float on the page to the bottom of the page.

and the default values are:

\@fptop = 0pt + 1fil
\@fpsep = 8pt + 2fil
\@fpbot = 0pt + 1fil

So, setting \@fptop to something without the 1fill, achieves the desired result.

\documentclass{article}
\usepackage[demo]{graphicx}% Remove [demo] option in real use
\usepackage[showframe]{geometry}
\usepackage{lipsum}

\makeatletter% Set distance from top of page to first float
\setlength{\@fptop}{5pt}
\makeatother

\begin{document}
\lipsum[1-5]
\begin{figure}[ht!]
    \centering
    \includegraphics{foo}
    \caption{A graph $G$}
\end{figure}
\end{document}
Related Question