35

I just got a short algorithm on my page, and it keeps getting aligned at the center of the page. But I want to have it aligned at the top, what do I have to do?

lockstep
  • 250,273
chris
  • 655

3 Answers3

51

A comment up front: I will assume that your document employs a single-column layout.

To display a short algorithm (an object that LaTeX labels a "float") on a page by itself and to align it along the top of the page (instead of getting it to be centered vertically), try adding the following instructions to your document's preamble:

\makeatletter
\setlength{\@fptop}{0pt}
\setlength{\@fpbot}{0pt plus 1fil}
\makeatother

Happy TeXing! Note that by inserting these instructions in the preamble, their scope is global, i.e., they cover the placement of floats on all floats-only pages.


Addendum: I've been asked to explain what exactly this code snippet does. The LaTeX "kernel" (see latex.ltx, ca. line 7260) sets up several parameters to help position a float (or floats) on a floats-only page. Among these, the parameters \@fptop and @fpbot -- short for "floating page top" and "floating page bottom", I suppose -- govern how much space is inserted above the top float and below the bottom float on a floats-only page, in a document with a single-column page layout. The default value for both parameters is 0\p@ \@plus 1fil. (\p@ is defined earlier in latex.ltx to be equal to 0pt.) The component 1fil is "infinitely stretchable glue" (in TeX jargon), i.e., it will expand to take up all available space within its scope (here, the vertical page dimension not already occupied by other material -- such as the float itself!). Thus, the float(s) will be centered vertically by default on the floats-only page, and (happily) no unnecessary warnings about under-full pages will be issued by LaTeX.

To force the first float on a floats-only page to be top-aligned, then, it should suffice to issue the command

\setlength{\@fptop}{0pt}

The second instruction, \setlength{\@fpbot}{0pt plus 1fil}, is there mostly to "play it safe", just in case some package has been loaded that fiddles with the \@fptop and \@fpbot parameters in a way that would make LaTeX start issuing warnings about under-full pages on floats-only pages should you reset just the \@fptop parameter...

Second addendum, June 2022: For a two-column page layout and full-width floats (such as table* and figure*), the relevant parameters are called \@dblfptop and \@dblfpbot, respectively.

Mico
  • 506,678
  • That was exactly what I was looking for, thank you – chris Sep 16 '11 at 18:47
  • @Mico Can I invoke this locally too, for example for 1 table? – TomM Dec 28 '13 at 01:00
  • @Mico Sorry cannot edit my post anymore: I wanted to say for 1 table or for parts of the document, eg the appendix. – TomM Dec 28 '13 at 01:18
  • @Mico I just tested it. Unfortunately it doesn't work in scrreprt :( – TomM Dec 28 '13 at 12:16
  • 2
    TomM: You're right! I will delete my previous, incorrect comment. (By the way, simple "localizing" the redefinitions of \@fptop and \@fpbot fails not only with scrreprt but also with the "standard" LaTeX document classes article, report, and book.) May I suggest you create a new posting in which you'd ask for a solution to the objective of localizing the scope of these parameters? – Mico Dec 30 '13 at 14:10
  • @Mico I found a different solution. I am using the float package now with the parameter [H] for floats, which puts them on top. Then any vertically distance can be adjusted manually. Did you find a better solution? Then I'd make a thread for sure. – TomM Dec 30 '13 at 14:42
  • How about if you want to have two floats vertically aligned to top? This solution only aligns the first float, the second is then placed in the centre of the remaining space. – reggian Mar 15 '16 at 09:56
  • @Reggian -- Welcome to TeX.SE! May I ask you to post a new query to express your question? That way, it will be seen by a lot more people. Thanks. – Mico Mar 15 '16 at 15:02
  • Is it possible to apply this hack to specific figures only? – Viesturs Apr 20 '19 at 19:25
  • @Viesturs - The short answer is, “probably yes.” A definitive answer will depend strongly on the document class in use. May I ask you to post a new query? – Mico Apr 20 '19 at 21:25
  • 1
    Done - https://tex.stackexchange.com/questions/485799/force-float-to-top-of-page-selectively – Viesturs Apr 21 '19 at 05:48
1

I think you can first try to use the "H" option in the table formatting first. This can work if your table is placed just in the place your actually want.

\usepackge{float}  %load this package first
\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{figure_name}
    \caption{the figure name}
    \label{the label info}
\end{figure}

If this does not work, try more complicated methods :D

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center. – Community Sep 18 '21 at 11:31
-3

After the graphics or listing, add this code:

\vfill
\rule[\baselineskip]{0pt}{\baselineskip}
Werner
  • 603,163