Monday, September 14, 2009

Centering a figure on the page in LaTeX

With those wide margins of LaTeX, it sometimes happens that you have a figure or table that is too wide, and just sticks out to the right. Chances are that you want it centered, sticking out equally to the right and to the left, but tough luck: the standard center environment still aligns it with the left margin, and so does the \centering command.

I was about to write a package to provide an environment that centers its content on the page instead of inside the text. This is not too difficult, but preventing the overfull \hbox errors is tricky (they are so bad that even \hbadness=10000 has no effect). But then I stumbled into a standard LaTeX command that does exactly what I want.

So here's how to center your figure whilst ignoring the text margins:
It also works inside float environments such as figure. That was easy!


Codiah said...

This has nothing to do with your current post here, but i had to stop by and praise you. I've been searching for days for a way to fix my sid-by-side error messaging in my visual basic 2008 programs when i execute them on another machine and no one was able to give me a straight answer on how to resolve the issue. However your post titled "Visual C++/Studio: Application configuration incorrect?" on another site made my day... and all is as it should be in the world again... thanks for sharing that information on the web.

lisa.joss said...

Wow, this is great!! thank you so much!

Kit said...

Perfect! Exactly the thing I needed. Thanks.

Anonymous said...

thanks for the help Typethinker. It's great having people like you who have others' interest in their minds.

Anonymous said...


Julia said...

Super cool! Thank you so much!

David said...

The same, thank you man. It took me a long while to find your solution, but this is it.

Anonymous said...

Great, you made my day!

Guanglei Dai said...

You saved me! searching the answer for 2days!!!

Anonymous said...

dude! awesome! This works like a charm. I have two images side by side in an array and it was ticking to the right even with the use of \center. This solved it. Thanks for sharing!

Lukas said...

thx, exactly what i was looking for :)

Anonymous said...

You have made a tired scientist much happier. Thank you!

Ana Barragán Montero said...

Thanks a lot!You solved one of my problems! that works perfectly for one figure but what if I have more than one figure??where should I place the command \centerline{}?
Here is my piece of code:

\subfigure[LYSO total spectrum, 80 ns]{%
\subfigure[LYSO total spectrum, 90 ns]{%
\caption{Total spectra for integration time equal to 80 and 90 ns, it can be noticed how both spectra are the same although the one for 90 ns has a bit more number of counts since the integration time is longer. }%

I've tried to put \centerline{} in each figure but then both figures overlap... any idea of how to do this?

Thomas ten Cate said...

No idea, sorry.

Anonymous said...

Thanks a lot for the centerline!
Exactly what I was lookingfor

Michael said...

This was super helpful. Thank you!

Anonymous said...

Thank you! which package is it?

Thomas ten Cate said...

It's included in LaTeX itself, no need to add any packages.