3

I am using image in my report as follows to make it top centered.

  \documentclass{article}
  \usepackage{lipsum}% http://ctan.org/pkg/lipsum
  \usepackage{graphicx,caption}% http://ctan.org/pkg/{graphicx,caption}
  \begin{document}
  \lipsum[1-3]
  \begin{figure}[h!]
  \includegraphics[width=0.7\linewidth]{example-image-a}
  \caption{This is a caption}
  \end{figure}
  \end{document}

enter image description here

But this does not work. If there is one image in page, image is alwasys centered only. How can I make it top centered?

manish
  • 9,111
  • 2
    How big is the image? If it is "too big" vertically (even though it may fit within the page with a caption, it could be pushed to a so-called page of floats (with only the float, vertically centred, on it). – Werner Apr 26 '13 at 02:05
  • Image is small (10% of page size). It appears to be in center. I need it to be top centered with blank space left after that. – manish Apr 26 '13 at 02:10
  • 1
    Do you have anything else before and/or after the image? Does your image have/require a caption? – Werner Apr 26 '13 at 02:15
  • What do you mean by top centered? Do you want it on the top of the page? Should you use [t] as the option? – Sigur Apr 26 '13 at 02:25
  • Yes there is caption after image (updated in my question) – manish Apr 26 '13 at 02:25
  • I m using t as one option in \begin{figure}[ht!]. Is there any place where do i need to use it. – manish Apr 26 '13 at 02:27
  • 1
    As far as I know the order of arguments is important. If you use first h it tries to insert the image here (near the position where the command was inserted). If is not possible then it tries the second option. – Sigur Apr 26 '13 at 02:28
  • 1
    @Sigur nop. The order is not relevant. – Gonzalo Medina Apr 26 '13 at 02:30
  • @GonzaloMedina, really? This is new to me. I read many years ago this information. Probably a bad documentation. Thanks to teach. – Sigur Apr 26 '13 at 02:31
  • I tried th! but no change. – manish Apr 26 '13 at 02:34
  • Have you tried replacing the location specifier [ht!] with [t!]? – Mico Apr 26 '13 at 02:38
  • 1
    @Sigur see the link in Werner's answer. It has everything one needs to know about positioning of floats. – Gonzalo Medina Apr 26 '13 at 02:39

2 Answers2

4

Figure placement can be tricky and depends on what is surrounding it. As a major resource behind TeX's algorithm, read How to influence the position of float environments like figure and table in LaTeX?

In this instance, you could use the following setup:

enter image description here

\documentclass{article}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{graphicx,caption}% http://ctan.org/pkg/{graphicx,caption}
\begin{document}
\lipsum[1-2]

\newpage
\noindent%
\begin{minipage}{\linewidth}
  \centering
  \includegraphics[width=0.7\linewidth]{example-image-a}
  \captionof{figure}{This is a caption}
\end{minipage}

\vfill

\newpage

\lipsum[3-4]
\end{document}

The idea is to avoid using a floating environment like figure, since you don't want TeX to make the placement. Instead something like a minipage works with a "fake" \caption.

Technically you don't need the entire caption package, as \captionof is all you're using here. So, using the capt-of package would also suffice. Also, there's no need here for the minipage environment. However, to contain the \centering alignment to a group, the minipage helps.

lipsum mere provides dummy text, Lorem Ipsum style.


For a more automated way of inserting the figure top-aligned on a separate page, use the afterpage package to insert it. The following MWE shows this:

enter image description here

\documentclass{article}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{afterpage,graphicx,caption}% http://ctan.org/pkg/{afterpage,graphicx,caption}
\begin{document}
\lipsum[1-2]

\afterpage{%
  \noindent%
  \begin{minipage}{\linewidth}
    \centering
    \includegraphics[width=0.7\linewidth]{example-image-a}
    \captionof{figure}{This is a caption}
  \end{minipage}
  \newpage%
}

\lipsum[3-7]
\end{document}
Werner
  • 603,163
  • Can we have floating image top centered in the avialable space? – manish Apr 26 '13 at 03:53
  • @user15662: Yes, but you need to provide more detail as to your current configuration. Please provide a minimal working example (MWE) that illustrates your current problem. From that one would be able to suggest alternatives and provide a solution. – Werner Apr 26 '13 at 04:08
  • i have uploaded MWE here. http://textuploader.com/?p=6&id=6CCLZ – manish Apr 26 '13 at 05:14
  • I have updated my question also pls help me. – manish Apr 26 '13 at 05:16
  • @user15662: Moreover, your MWE is just mine with the use of figure, which I suggested you avoid. I'm not sure you read my answer it seems. As you can see from How to influence the position of float environments like figure and table in LaTeX? (I included this link in my post), there are many things that influence float placement. As such, using h! as a float specifier does not guarantee here or even top placement. Please provide an adequate MWE that clearly shows your current problem, otherwise it would be difficult to help you successfully. – Werner Apr 26 '13 at 05:34
  • I have many section before and after like this. In each section i m using floating image to make a generalized report for variable content. if i avoid figure, every time i need to edit my latex (Sweave) code to fit the images on pages based on text along with image in each section. So kindly pls suggest some solution related to this pblm. – manish Apr 26 '13 at 06:39
  • @user15662: Insert the content using the help of afterpage. I've added an example of this at the end of my answer. – Werner Apr 26 '13 at 18:20
2

A better alternative, if you never want pages with a single float to be centered, is to include this three line patch in your preamble. Problem solved.

\documentclass{article}
  \usepackage{lipsum}% http://ctan.org/pkg/lipsum
  \usepackage{graphicx,caption}% http://ctan.org/pkg/{graphicx,caption}

 \makeatletter
\setlength{\@fptop}{0pt}
\makeatother

  \begin{document}
  \lipsum[1-3]
  \begin{figure}
  \includegraphics[width=0.7\linewidth]{example-image-a}
  \caption{This is a caption}
  \end{figure}
  \end{document}
  • You have set a parameter that affects float pages but then prevented (using h) the figure to use a float page (other than in an emergency dump of floats at \clearpage or end of document.) Best to remove the optional argument or use [p] to force the figure to use the setting. (but that is the correct way to make float pages top aligned) – David Carlisle Apr 26 '13 at 11:30
  • Indeed. I just added the necessary preamble to the existing MWE. Changed now however. – Aubrey Blumsohn Apr 26 '13 at 11:35