TeXblog

 Typography with TeX and LaTeX

Centering wide tables or figures

17 September 2008 by Stefan Kottwitz

When you want to include an image or a table that’s wider than the text width, you will notice that even when \centering or the center-environment is used this wide object will not be centered in relation to the surrounding text. It will be placed at the left margin but go into the right margin. Its frequently requested that wide figures or tables should overlap at both sides in equal measure.

It can easily achieved by putting the table or picture inside a box, giving the box the width of the text, by the \makebox command. Here is a compilable example, where I’m centering a table having 1.5 times the width of the text:

\documentclass[a4paper,10pt]{article}
\usepackage[english]{babel}
\usepackage{blindtext}
\usepackage{tabularx}
\begin{document}
 
\blindtext
\bigskip
 
\noindent\makebox[\textwidth]{%
\begin{tabularx}{1.5\textwidth}{XX}
  \blindtext & \blindtext
\end{tabularx}}
 
\bigskip
\blindtext
 
\end{document}

I’ve used \noindent to suppress the paragraph indentation, otherwise I would get an overfull \hbox. As you may notice there’s no \centering necessary because the width of the box equals \textwidth.

This topic was discussed in the LaTeX Community Forum, on CQF.info and on mrunix.de.
.

This entry was posted on 17 September 2008 at 8:22 PM and is filed under Figures and Tables, Layout. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

16 responses about “Centering wide tables or figures”

  1. Kjell Magne Fauske said:

    Thanks for sharing this. I recently stumbled upon the same problem and did not find a good solution. Next time I’ll use this trick.

  2. Marcin said:

    That’s pretty interesting. Being a long-time plain TeX user, I would just do an \hbox to\textwidth{\hss … \hss}, but this solution is much more LaTeX-like.

  3. Stefan Kottwitz said:

    Hi Kjell, hi Marcin,

    thank you for your comments, they’re encouraging me to post some more small tricks and workarounds.

  4. Joan said:

    Thanks. You have make a good job. :)

  5. Some said:

    Thanks. I like that. However, it would have been better if you could display what it would look like when you compile it. I have a problem centering a picture even though I used the \centering code; which did not work. How do you put a picture on the left of the frame?
    Again thanks

  6. Stefan Kottwitz said:

    Hi Some,

    here’s a screenshot using the code above:

    centered wide table

    Concerning your problem please show some code that’s needing correction. Btw. to position a picture freely you could use the textpos package.

  7. Mike said:

    I’m still a beginner with Latex, but this definitely helped. Thanks!

  8. Aaron said:

    Thanks for the great post topic.

    The example code works fine for me, but not when I try to adapt it for a figure.

    Specifically,

    \noindent\makebox[\textwidth]{%
    \begin{figure}
    \includegraphics{mypic}
    \end{figure}}

    returns “LaTeX Error: Not in outer par mode.” for me.

    When I place the figure environment outside of the \makebox command,

    \begin{figure}
    \noindent\makebox[\textwidth]{%
    \includegraphics{mypic}}
    \end{figure}

    the figure appears, but I am unable to change the width of the image,

    \begin{figure}
    \noindent\makebox[\textwidth]{%
    \includegraphics[width=\textwidth]{mypic}}
    \end{figure}

    returns the “Not in outer par mode” again. Any ideas what I’m doing incorrectly?

  9. Leon said:

    Dear Sir

    I have just seem this site. I have a question about LaTex. Please, I would like to know if the e-mail of :Aaron from 20 October 2009 at 6:27 PM, was answeered, because I have the same problem. Subject:Centering wide tables or figures.

    Regards

    Leon

  10. Stefan Kottwitz said:

    Hi Leon,

    you cannot put a figure into a box, because figures are intended to float, but you boxes inside figures are possible. So the second try of Aaron should work.

    This example works for me:

    \documentclass[a4paper,10pt]{article}
    \usepackage{graphicx}
    \begin{document}
    \begin{figure}
    \noindent\makebox[\textwidth]{%
    \includegraphics[width=1.4\textwidth]{mypic}}
    \end{figure}
    \end{document}

    Perhaps don’t use a figure environment if you wouldn’t need floating. Captions can be positioned using the caption package, even without a figure environment.

    Stefan

  11. Arne Seemann said:

    Dear Stefan, thank you so much. Your last reply really helped my in formatting my Master-Thesis :)

  12. Bjørnar said:

    Sweet article. But, I don’t understand why would you create the box for the figure instead of simply doing \includegraphics[width=\textwidth]{mypic}, what would the difference be?

  13. Stefan Kottwitz said:

    Hi Bjørnar,

    the difference is: that article should help to center a figure that’s wider than the text, not to resize it.
    Sometimes there’s a picture or a table that’s too wide and resizing it would make it unreadable or would reduce quality. That’s why I’ve noted this workaround.
    If possible, I would prefer to scale the figure to the text width too, then of course we don’t need \makebox.

    Stefan

  14. Mark said:

    I have been searching for this type of solution for a while. Your tip about figures was perfect! I am sure lots of people want this functionality, perhaps we should add it to the latex wiki on figures and/or layout?

  15. Mark said:

    Oh and this method also seems to play well with subfigures!

    \documentclass[a4paper,10pt]{article}
    \usepackage{graphicx}
    \begin{document}
    \begin{figure}[htp]
    \noindent\makebox[\textwidth]{%
    \subfigure[caption1]\includegraphics[width=0.8\textwidth]{pic1.jpg}}
    \subfigure[caption2]\includegraphics[width=0.8\textwidth]{pic2.jpg}}}
    \end{figure}
    \end{document}

  16. Stefan Kottwitz said:

    Hi Mark,

    feel free to copy it if you would like to. Of course it would be very good to add content to such knowledge bases like the LaTeX wiki.

    Stefan

Leave a Reply