I noticed that if I try to center an image that goes off the page, TeX/LaTeX seems to give up and just left-align the image. Why is this?

  • 1
    You can trick TeX using a box: \noindent\makebox[\linewidth]{\includegraphics[scale = 1.5]{example-image-a} } – Gonzalo Medina Feb 17 '15 at 0:54

The basic mechanism by which Tex works is a mechansm known as boxes and glue. Centering is not a primitive operation.

Glue is white space that has a natural length and separate lengths by which it can stretch or shrink.

in LaTeX most centering (say of the letter X) is done by placing glue either side that is equivalent to \hfil that is 0pt plus 1fil minus 0pt

So in

\hbox to \textwidth{%
\hspace{0pt plus 1fil}X\hspace{0pt plus 1fil}%

The requested box is \textwidth wide, X is smaller than that, so the two glue elements expand equally to fill the gap.

But note the minimum glue size on either side is 0pt so if you replace X by something wider than \textwidth the box is overfull (and sticks out on the right).

Other choices would be possible, for example the plain Tex macro \centerline is similar to the above but uses \hss which has infinite stretch and shrink values. that allows the text to be wider than the textwidth and the glue automatically assumes negative amounts to compensate. This always centres but you get no warning the text boundaries (or even the page boundaries) have been exceeded.

| improve this answer | |
  • Ah, I misread the question. I thought the OP wanted to know how to center an image wider than \textwidth. I'll delete my answer and gladly give you my +1 :) – Gonzalo Medina Feb 17 '15 at 1:00
  • @GonzaloMedina no I'd leave yours. although phrased as a "why" the Op may want to know how to avoid the problem:-) – David Carlisle Feb 17 '15 at 1:01
  • OK. I undeleted it and you still have my +1 :). – Gonzalo Medina Feb 17 '15 at 1:04
  • @GonzaloMedina voted:-) – David Carlisle Feb 17 '15 at 1:05
  • 1
    "sticks out on the left" ... huh? i'm used to things like this sticking out on the right. – barbara beeton Feb 17 '15 at 1:13

David's answer nicely explains the centering mechanism. If you want to know one option to center an image wider than \textwidth, one possibility is to trick TeX using a box:



\noindent\makebox[\linewidth]{\includegraphics[scale = 1.3]{example-image-a} }


enter image description here

Of course, the best solution is to control the width for the image so it doesn't exceed \textwidth:

| improve this answer | |

Your Answer

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