279

I have a figure that is wider than the \textwidth of my document. (I don't want to change the \textwidth.) As a result, the leftmost part of the figure is flush with the left margin, while the rightmost part spills into the right margin too much. I'd like to center the figure, so that it will exceed the \textwidth equally on both sides.

lockstep
  • 250,273
zfm
  • 8,987
  • 9
  • 30
  • 21

4 Answers4

329

Put the content of your figure environment into a \makebox[\textwidth][c]{...} macro. This will center its content to the normal text width even if it is wider than that. See also my similar answer to Place figures side by side, spill into outer margin.

The image can also be aligned to the left and right using [l] and [r], which makes the image lap into the right or left margin, respectively.

Example:

\documentclass{article}
\usepackage{graphicx}
\begin{document}

\begin{figure}
  \makebox[\textwidth][c]{\includegraphics[width=1.2\textwidth]{image}}%
  \caption{Caption}
  \label{fig:key}
\end{figure}
\end{document}

Starting from 2011/08/13 you can also use adjustbox package for such alignments. For centering use the center=<length> which centers the content in the given length. The length is optional and defaults to \linewidth which is normally identical to \textwidth. There is also left and right as well as inner and outer.

\documentclass{article}
\usepackage[export]{adjustbox}[2011/08/13]
\begin{document}

\begin{figure}
  \includegraphics[width=1.2\textwidth,center]{image}%
  \caption{Caption}
  \label{fig:key}
\end{figure}
\end{document}
Martin Scharrer
  • 262,582
  • 9
    At http://tex.stackexchange.com/a/57709/2552 there's the suggestion of using \centerline{\includegraphics}. Could you comment on whether there are any drawbacks with that approach compared to your suggestions (in the interest of a "canonical answer")? – Jake May 29 '12 at 06:49
  • 2
    @Jake: \centerline is actually a plainTeX macro and does not leave-vmode, i.e. you need to add \leavevmode before it to make sure it is not accidentally stacked in vertical mode with some following \hbox. Beside this the center key of adjustbox works the exact same way as \centerline, just with on optional width argument. – Martin Scharrer May 29 '12 at 06:52
  • 1
    Applying the makebox approach, the figure caption is still centered within the actual textblock, not within the area spanned by the overlarge makebox/figure. Is there a fix to that? – bioslime Dec 07 '12 at 22:51
  • @bioslime: The captions are build be special code which might fix the width to \textwidth. Have a look at caption package, maybe it allows to change this. – Martin Scharrer Dec 10 '12 at 21:03
  • Good hint. And indeed, simply replace \caption and \label to include \captionsetup[figure]{margin={10pt,\omargin}} \captionof{figure}{\label{img}Description}.Now margin definies the inner/outer margin in respect to linewidth(or textwidth?). Therefore, define the margin that breaks out e.g. like \newlength\omargin \setlength\omargin\linewidth \addtolength\omargin{-1.2\linewidth} % in case that is the total image width \addtolength\omargin{+10pt} % add the margin you want. Also when the captionsetup is placed within the environment it will only affect this caption. – bioslime Dec 12 '12 at 11:16
  • 1
    I found \makebox[0pt]{...} worked well. (It is from another answer somewhere but I mention it here as this is the one duplicates are getting linked to.) – cfr Jan 18 '15 at 00:59
  • As it turns out, this also removes the warning about to full hbox. – magu_ May 20 '15 at 22:03
  • In LyX all you need is adding \usepackage[export]{adjustbox} to the preamble and then add center to Graphics' LaTeX options. It works like a charm! – Atcold Aug 04 '19 at 00:44
  • 1
    In case your figure is not inside a figure environment, make sure to remove the indentation or you will find your figure strangely deported on the right: \noindent\makebox[\textwidth][c]{your figure}. – tobiasBora Jan 11 '22 at 11:38
  • So unfortunate that it works for includegraphics but not for includesvg. \centerline{} works for the later, though. – MacroController Feb 04 '22 at 01:26
  • @MartinScharrer @cfr I also mention that \makebox[1.1\textwidth][c]{...} does not give the wanted behaviour. – Karlo Aug 04 '23 at 23:25
  • 1
    @Karlo It isn't surprising that wouldn't work if the behaviour is centring a too-wide graphic, though, since the box will be too wide, too. You have to lie ;). – cfr Aug 05 '23 at 00:32
70

If you're using the memoir class, the \centerfloat command does that:

\begin{figure}
\centerfloat
% your figure, caption and label
\end{figure}

If you're using another class, you can define the same command by grabbing its code from memoir:

\makeatletter
\newcommand*{\centerfloat}{%
  \parindent \z@
  \leftskip \z@ \@plus 1fil \@minus \textwidth
  \rightskip\leftskip
  \parfillskip \z@skip}
\makeatother
Marc Baudoin
  • 2,676
  • 2
    This is extremely useful! I use this all the time! Thank you for posting this. – Jean-Paul Nov 15 '15 at 22:01
  • 1
    I have looked for this for hours now... this is the only thing that worked for me since I've a listing in my table that is too wide. Thanks a lot! – Hiura Jan 07 '16 at 22:55
  • Wow, I never even knew that memoir had this genius piece of code. I am editing exam questions written in latex and the author insisted on having an image width of exactly 18cm, but the margins were completely off and the image was sticking out past the page border. This definetely saved me some trouble! – Bjarne Thorsted Jun 07 '17 at 08:41
  • Extremely useful! Thanks especially for the code snippet so I don't necessarily have to use package memoir – Samleo Sep 18 '20 at 16:04
20

You could also use the changepage package, which defines the adjustwidth environment that allows an environment that spills out across the margins.

screenshot

% arara: pdflatex
% !arara: indent: {overwrite: yes}
\documentclass{article}

\usepackage[showframe=true,margin=3cm]{geometry}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{changepage}                 % adjust margins for selected portions
\usepackage{lipsum}

% wide page for side by side figures, tables, etc
\newlength{\offsetpage}
\setlength{\offsetpage}{1.0cm}
\newenvironment{widepage}{\begin{adjustwidth}{-\offsetpage}{-\offsetpage}%
    \addtolength{\textwidth}{2\offsetpage}}%
{\end{adjustwidth}}

\begin{document}

\lipsum[1]
\begin{table}[!htb]
    \begin{widepage}
    \begin{tabularx}{\linewidth}{X*{7}rr}
        \toprule
            & 2008/09 & 2009/10 & 2010/11 & 2011/12 & 2012/13 & --2011/12 & --2012/13  & --2012/13 \\
        \midrule
        SY  & 6764    & 8155    & 8847    & 9682    & 8840    & 43.14\%   & $-8.70\%$  & 30.70\%   \\
        CA  & 4159    & 5745    & 5963    & 6585    & 5887    & 58.33\%   & $-10.60\%$ & 41.55\%   \\
        RC  & 6625    & 8033    & 8192    & 8669    & 8454    & $30.85\%$ & $-2.48\%$  & 27.60\%   \\
        ELC & 2785    & 3883    & 4404    & 4709    & 4860    & $69.08\%$ & 3.21\%     & 74.50\%   \\
        \bottomrule
    \end{tabularx}
    \end{widepage}
\end{table}
\lipsum[1]
\end{document}
cmhughes
  • 100,947
  • 1
    My solution

    \begin{figure}[htb!] \begin{widepage} \centering \subfigure[...]{\label{...}\includegraphics[width=0.325\linewidth]{...}} \subfigure[...]{\label{...}\includegraphics[width=0.325\linewidth]{...}} \subfigure[...]{\label{...}\includegraphics[width=0.325\linewidth]{...}} \end{widepage} \caption{...} \label{...} \end{figure}

    – jfabrizio Sep 05 '20 at 13:22
  • Finally, this works! Thank you! – Suuuehgi Jul 26 '22 at 14:23
7

There is a far more simpler answer from this post.

The solution is :

\centerline{\includegraphics{...}}
Welgriv
  • 325