# How can I automatically center an image?

I use:

\documentclss{mwrep}
...
\begin{figure}
\includegraphics{a.jpg}
\caption[Long caption]{Caption}
\label{pic-a}
\end{figure}


How can I make images to be always visible on the center of page? (To be more specific: the center of the image should be between the left and right margin - I use different left and right margins). I don't want to put anything after every \begin{figure} in my document.

Important: I don't want to waste an entire page on a single image at the very center of the page. I want only horizontal alignment. (left <-> right)

-
insert \centering after figure – Herbert Jul 14 '11 at 20:10
Yes, that works. But I want to do this without adding anything to every figure in document. – Ichibann Jul 14 '11 at 20:11
Regarding your 2nd question: Use either the caption or the hypcap package. – Axel Sommerfeldt Jul 14 '11 at 20:54
My example results in a top-aligned float, so you must be doing something different. (And without a minimal example it's hard to tell what exactly you're doing different.) – lockstep Jul 14 '11 at 21:24
caption and hypcap... none of them helped... – Ichibann Jul 14 '11 at 22:42

With regard to your first question: Use the floatrow package -- it centers the content of floats by default.

\documentclass{article}

\usepackage[draft]{graphicx}
\usepackage{floatrow}

\begin{document}

The first paragraph.

\begin{figure}
\includegraphics{a.jpg}
\caption[Long caption]{Caption}
\label{pic-a}
\end{figure}

And the second.

\end{document}


If you don't want to load floatrow for whatever reason, you may instead add the following to your preamble (thanks to egreg for the tip):

\makeatletter
\makeatother


EDIT: The figure in my example only was vertically centered because I had not added any dummy text. Example corrected.

-
What does draft do? I was told that graphicx is better. How to use draft in graphicx? – Ichibann Jul 14 '11 at 20:17
Also, it centers image at the page as whole. I want only to center vertically. There could be more text or images on the same page. – Ichibann Jul 14 '11 at 20:20
@Ichibann It does not require an input file and shows a bounding box with its name instead, hence you can render the document as is. The usage with graphicx is the same. – Harold Cavendish Jul 14 '11 at 20:20
draft makes the example compilable even if there is no file a.jpg in one's working directory. ;-) This feature is also part of graphicx. – lockstep Jul 14 '11 at 20:21

\makeatletter
\makeatother


The start of every float construction ends with a macro \@floatboxreset that looks like this (from latex.ltx):

\def \@floatboxreset {%
\reset@font
\normalsize
\@setminipage
}


Executing the above suggestion adds \centering at the end of \@floatboxreset, effectively changing it to

\def \@floatboxreset {%
\reset@font
\normalsize
\@setminipage
\centering
}


which removes the requirement to specify \centering all the time.

-

You could redefine the figure environment to include the \centering command:

\let\origfigure\figure
\let\endorigfigure\endfigure

\renewenvironment{figure}[1][tbph]{%
\origfigure[#1]%
\centering
}{%
\endorigfigure
}

-

If you're using the memoir documentclass, global float adjustment may be set using: \setfloatadjustment{<float>}{<adjustment>}. For example,

\documentclass{memoir}
\begin{document}
\begin{figure}
\includegraphics{image}
\caption[LoF caption]{Regular caption}
\label{fig:image}
\end{figure}
\end{document}

-

Another way to center all images, is to use \adjincludegraphics from the adjustbox package and declare the center to be used for all of them after any local options. However, this will add center also to all other \adjustbox macros and adjustbox environments. I actually just like to show the principle:

\documentclass{article}

\usepackage{lipsum}% for fummy text only
\usepackage[draft]{graphicx}% remove 'draft' for a real document
\begin{document}

\lipsum[1]

\begin{figure}
\caption[Long caption]{Caption}
\label{pic-a}
\end{figure}

\lipsum[2]

\begin{figure}
\caption[Long caption]{Caption}
\label{pic-b}
\end{figure}

\lipsum[3]

\begin{figure}