83

How do I align an image to center?

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}
fmatt
  • 103
epsilon8
  • 5,961

9 Answers9

89

It is enough to use the center enviroment:

\begin{center}
    \includegraphics{yourimage}
\end{center}
gcedo
  • 2,699
  • 23
  • 20
33

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

Tobi
  • 56,353
28

You could also do something like this:

\begin{figure}[!h]
    \caption{My caption}
    \centering
    \includegraphics[width=50mm]{theImage}
    \label{fig:label}
\end{figure}
Freefri
  • 381
23

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.

Werner
  • 603,163
  • 11
    Third way \centerline{\includegraphics...}, perhaps you want to add it. Fourth way {\centering \includegraphics...\par} – yannisl 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 07 '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 07 '15 at 17:18
19

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.

Martin Scharrer
  • 262,582
1

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

Michel
  • 161
  • 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
1

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.

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

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)

The V
  • 1,401
  • 1
    Example given doesn't work as written. A node must have a label text. When looking to adapt a solution to one's own problem, we shouldn't be stuck just trying to debug the solution. – Evan Langlois Jan 24 '23 at 02:42
  • No, a node does not need label text, it can be empty, so long as the curly brackets are there. I guess I should have added something other than \includegraphics{} in the node.

    The example runs just fine, though you are right, I should have added both the package (tikz) and something to indicate the \includegraphics command needs to reference an image. Add tikz in the preamble and a path to the \includegraphics{} command and there is no issue.

    – The V Feb 09 '23 at 07:56