29

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)

lockstep
  • 250,273
Ichibann
  • 8,381
  • 1
    insert \centering after figure –  Jul 14 '11 at 20:10
  • 1
    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. –  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
  • Please don't ask two questions in one post. Centering the figures and fixing the hyperlinks are two different things. I suggest removing the second question here and reask it as a new question. – Martin Scharrer Jul 17 '11 at 09:47
  • @Ichibann: Also try to create a truly minimal example -- one that doesn't load packages not relevant for the question and, more important, that doesn't rely on external files availabele for you, but not for other users. – lockstep Jul 17 '11 at 10:04

5 Answers5

32

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
\g@addto@macro\@floatboxreset\centering
\makeatother

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

lockstep
  • 250,273
  • 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
  • 1
    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
21

Add the following to your document preamble:

\makeatletter
\g@addto@macro\@floatboxreset{\centering}
\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.

Werner
  • 603,163
13

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

\documentclass{memoir}
\setfloatadjustment{figure}{\centering}
\begin{document}
  \begin{figure}  
    \includegraphics{image}
    \caption[LoF caption]{Regular caption}
    \label{fig:image}
  \end{figure}
\end{document}
Werner
  • 603,163
9

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
}
Martin Scharrer
  • 262,582
4

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
\usepackage{adjustbox}
\adjustboxset*{center}
\begin{document}

\lipsum[1]

\begin{figure}
\adjincludegraphics[width=5cm]{a.jpg}
\caption[Long caption]{Caption}
\label{pic-a}
\end{figure}

\lipsum[2]

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

\lipsum[3]

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


\end{document}
Martin Scharrer
  • 262,582