3

I'm currently having a few problems with centering an image on a page (both vertically and horizontally) while enlarging the image so that it is taking the whole width of the page.

I'm managing to center the picture horizontally, but not vertically. The code below shows the version I made that centers the image horizontally. My question is here what can I do to center it also vertically?

I'm having an srcbook class .tex:

\documentclass[11pt,a4paper,BCOR10mm,DIV11,toc=listof,parskip=full]{scrbook}

\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{booktabs}            
\usepackage{multicol}   

I'm currently positioning the image like this:

\centerline\noindent\makebox[\textwidth]{\includegraphics[width=\paperwidth]{MyImage.jpg}
  • before the \centerline (which isn't the most "latexy" way to do this), add \vspace*{\fill} and add \vspace{\fill} after it. the starred version will keep the space from disappearing at the top of a page. as for the \centerline, that's a holdover from plain tex. a "better" way (or at least one with better latex syntax) is to use \begin{center} ... \end{center} to "wrap" it. – barbara beeton Jul 22 '14 at 18:42
3

\centerline takes an argument so in

\centerline\noindent\makebox[\textwidth]{\includegraphics[width=\paperwidth]{MyImage.jpg}

the argument is just \noindent

so it is equivalent to

\centerline{\noindent}%
\makebox[\textwidth]{\includegraphics[width=\paperwidth]{MyImage.jpg}}

so you get one blank white line from the \centerline then an overfull box with the image.

possibly you want

\vspace*{\fill}
\noindent
\hspace*{-\oddsidemargin}%
\makebox[0pt][l]{\includegraphics[width=\paperwidth]{MyImage.jpg}}
\vspace*{\fill}

I am assuming here you did intend your image to be as wide as the paper not just as wide as the text block, so covering both margins.

  • the last part results in an compile error for me (runaway argument file ended while scanning \@imakebox (seemingly at \includegraphics)) – Thomas Jul 22 '14 at 18:47
  • @ThomasE. was missing a } If you had supplied a test file in your question I'd have tested it:-) – David Carlisle Jul 22 '14 at 18:49
  • tnx functions that way. albeit a bit smaller in width than pagewidth – Thomas Jul 22 '14 at 18:51
  • tnx = thank you (sry did take up on that shortcut for thank you thanks to MMORPGs). Found an issue with how the graphics is displyed, but that has nothing to do with the original question and the answer to it. will probably make a second question there if I can't solve the problem. So thank you again for the answer helped a lot there! – Thomas Jul 22 '14 at 19:00
  • ah......... :-) – David Carlisle Jul 22 '14 at 19:01
2
\documentclass[demo]{article}
\usepackage[a6paper,margin=2cm]{geometry}
\usepackage{graphicx}
\begin{document}
\null\vfill
\centering\includegraphics{xyz}
\vfill\null
\end{document}

How to prove?

\documentclass[pstricks,border=12pt]{standalone}

\usepackage{filecontents}
\begin{filecontents*}{foo.tex}
\documentclass[demo]{article}
\usepackage[a6paper,margin=2cm]{geometry}
\usepackage{graphicx}
\begin{document}
\null\vfill
\centering\includegraphics{xyz}
\vfill\null
\end{document}
\end{filecontents*}

\immediate\write18{pdflatex foo}
\immediate\write18{pdftops -f 1 -l 1 -eps foo.pdf}


\usepackage{graphicx}
\newsavebox\IBox
\savebox\IBox{\includegraphics{foo}}

\psset
{
    xunit=0.1\wd\IBox,
    yunit=0.1\ht\IBox,
}

\begin{document}
\begin{pspicture}[showgrid=top](10,10)
    \rput[bl](0,0){\usebox\IBox}
\end{pspicture}
\end{document}

enter image description here

  • \centering will not do anything if the image is wider than \textwidth as in the OP's case. – David Carlisle Jul 22 '14 at 18:58
  • @DavidCarlisle: I see. In this answer I mainly just concentrated on the vertical alignment. – kiss my armpit Jul 22 '14 at 19:01
  • Thanks PSTricks for helping me to prove it geometrically. – kiss my armpit Jul 22 '14 at 19:08
2

The image can easily be centered horizontally and vertically via package pdfpages. It also enlarges the image as much as possible to fit into the paper area without distorting the image.

Example:

\documentclass[11pt,a4paper,BCOR=10mm,DIV=11]{scrbook}

\usepackage{pdfpages}

\begin{document}
  \includepdf{MyImage.jpg}
\end{document}

Option BCOR is not taken into account, when placing the image. Some white space can be added to the left or right of the image via option trim:

\includepdf[trim=-5mm 0 -8mm 0]{MyImage.jpg}

Would add a white margin of 5mm to the left and 8mm to the right of the unscaled image.

1

Try this

\vspace*{\fill}
\begin{center}
  \includegraphics[width=\paperwidth]{MyImage.jpg}
\end{center}
\vspace*{\fill}
  • 1
    that will be over full (and not centred) in the usual case that \paperwidth is greater than \textwidth – David Carlisle Jul 22 '14 at 18:43

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.