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)

7
  • 1
    insert \centering after figure
    – user2478
    Commented Jul 14, 2011 at 20:10
  • 1
    Yes, that works. But I want to do this without adding anything to every figure in document.
    – Ichibann
    Commented Jul 14, 2011 at 20:11
  • Regarding your 2nd question: Use either the caption or the hypcap package.
    – user2574
    Commented Jul 14, 2011 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
    Commented Jul 14, 2011 at 21:24
  • caption and hypcap... none of them helped...
    – Ichibann
    Commented Jul 14, 2011 at 22:42

5 Answers 5

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.

4
  • What does draft do? I was told that graphicx is better. How to use draft in graphicx?
    – Ichibann
    Commented Jul 14, 2011 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
    Commented Jul 14, 2011 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. Commented Jul 14, 2011 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
    Commented Jul 14, 2011 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.

0
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}
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
}
0
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}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .