46

How do I align an image to centre?

In a beamer presentation I have a 2-3 items on a slide followed by an image. The image is not wide enough to cover whole slide, so it leaves around 30% space from right. How do I adjust it to leave enough space on both sides?

\begin{frame}
\frametitle{Outlook}
\begin{itemize}
\item apps
\item apps
\item apps
\includegraphics[scale=0.3]{P2P}    
\end{itemize}
\end{frame}
49

It is enough to use the center enviroment:

\begin{center}
    \includegraphics{yourimage}
\end{center}
19

The third way is to use the figure environment, which is the best way in my opinion, since it provides the right mark-up. (beamer centers the figures by default.)

\begin{figure}
   \includegraphics{<your image>}
\end{figure}

In this case on may also add a caption with \caption{<text>}

  • +1 For the info about automatic centering in beamer – Shadow Jan 5 '17 at 6:47
18

Here are two ways of achieving this:

\begin{center}
  \includegraphics{<your image>}
\end{center}

or

\hfill\includegraphics{<your image>}\hspace*{\fill}

The former may add some vertical whitespace, while the latter centers its contents on the line used.

  • 10
    Third way \centerline{\includegraphics...}, perhaps you want to add it. Fourth way {\centering \includegraphics...\par} – Yiannis Lazarides Apr 30 '12 at 21:00
  • 3
    I personally think \centerline is just perfect to center single boxes like images. @YiannisLazarides – Martin Scharrer Apr 30 '12 at 22:42
  • I tend to prefer the opening-closing environments. So, I do not like much to use {\centering ...} but prefer \bgroup \centering ... \egroup. – cacamailg Dec 7 '15 at 10:30
  • 2
    @cacamailg: For what it's worth, they are equivalent, as you'll find \let\bgroup={ \let\egroup=} in the LaTeX kernel. – Werner Dec 7 '15 at 17:18
13

One simple way to center an image is to use the adjustbox package with the export option. It provides the center=<width> key to \includegraphics, which centers the image around the given width. It defaults to the \linewidth, so use:

\usepackage[export]{adjustbox}
% ....

\includegraphics[scale=0.3,center]{P2P} 

Inside a itemize this will center the image relative to the itemize text width, not to the frame.

  • how about to the left and top? – Vass Feb 11 '17 at 3:50
  • At least this one works !! – tinmarino May 4 at 14:18
12

You could also do something like this:

\begin{figure}[!h]
    \caption{My caption}
    \centering
    \includegraphics[width=50mm]{theImage}
    \label{fig:label}
\end{figure}
  • Thanks a lot, dude. I tried all the above ways, didn't work. Yours works like a charm. – AhmedWas Nov 4 '17 at 8:23
  • 1
    Easy and works! – Salech Rubenstein Nov 26 '17 at 21:33
  • Just a note here: \centering should come before \includegraphics – Simon Baars Jun 12 at 12:10
1

\centering\includegraphics{...} — when the figure is smaller than the text width \centerline{\includegraphics{...}} — when the figure is wider than the text width

  • 3
    Welcome to TeX.SX! Using just \centering\includegraphics{...} would make the rest of the document from this point on centered if you don't take care of the grouping. Maybe you should add something explaining this too. And I think that nowadays there are better alternatives than \centerline... – Phelype Oleinik Jun 15 '18 at 11:39
0

I always use:

\begin{center}
  \includegraphics[width=\textwidth]{image}
\end{center}

That's almost perfect for raw screenshots, as images will not overflow the visible content.

0

This helps me

\begin{figure}[!h]
\includegraphics[width= 365pt]{./figures/xyz.png}
\centering
\caption{The picture shows xyz architecture}
\label{fig:xyz}
\end{figure}

You can of course change the width and other things as per your case. The only major part is \centering which helps here.

  • Welcome to TeX.SE! I think \centering should go before \includegraphics. – user156344 Mar 27 at 4:53
  • Thanks for the welcome note. I am not sure, if this has any impact. However, in my case it works perfectly fine. – Niraj D Pandey Mar 28 at 13:07
0

You can also use tikz.

\begin{tikzpicture}[remember picture,overlay]
   \node at (current page.center) node[anchor=center]{\includegraphics{}};
\end{tikzpicture}

will place your image on the center of the page. This does not account for any header, however. If you have a header that is 1 cm tall you can move the picture to compensate thus:

\begin{tikzpicture}[remember picture,overlay]
   \node at ($(current page.center) +(0,-1cm)$) node[anchor=center]{\includegraphics{}};
\end{tikzpicture}

You can also anchor the node to any corner of side of the page, thus achieving absolute positioning with complete control rather effectively.

(I realize this question is old, but the solution is versatile and offers excellent positioning control)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.