297

I use

\inclugraphics[scale=TRYING TO GUESS THE NUMBER FOR PAGE WIDTH]{...}

but I find this errorsome, is there some ready flag to set the image to page width? I usually use PNG and JPG photos.

4
  • 81
    \includegraphics[width=\textwidth]{...}
    – egreg
    Commented Dec 22, 2011 at 21:56
  • 11
    Simply use width=\textwidth to scale it to the width of the text area, as egreg already noted. (The whole page would be \paperwidth and would cause an overfull error.) You should have a look at the Graphic Guide for all the possible options for \includegraphics. Also check the adjustbox package which adds several more options to it. Commented Dec 22, 2011 at 22:03
  • Similar question in SO here, contacted mod to look for this. Perhaps merging?
    – hhh
    Commented Dec 26, 2011 at 21:46
  • 2
    When the idea is to fit the images to the margins, it is better to get used to use \linewidth ("only the actual width of the lines") that \textwidth ("the whole width of the text area"). In one column text both lengths will be same thing, but inside a nested list, or in two column text, or a tabular with a p{3cm} column, for instance, \linewidth still is the correct length, whereas \textwidth will produce here only junk results.
    – Fran
    Commented Nov 4, 2018 at 9:49

7 Answers 7

282

Use \textwidth for the width of the text block, and \paperwidth if you want to fit it into the paper width. You could also use \linewidth if you want to fit the image within the line width, which may vary depending on the environment you're in (for example, within a list like enumerate).

Note that if you use \includegraphics outside a figure or table environment, you might want to prepend it with \noindent to avoid the image being pushed over to the right by \parindent. Also, centering the image within the page width (when using \paperwidth) is best obtained using

\begin{center}
  \makebox[\textwidth]{\includegraphics[width=\paperwidth]{...}}
\end{center}

or

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

In both instances it typesets a box of width \textwidth, while the contents may stretch outside this width (given by width=\paperwidth). Fixing it to \textwidth avoids Overfull \hbox warnings.

This works as expected within the article and report document class, while some horizontal re-adjustment is required in book. For completeness, and without resorting to page numbering issues that may occur at shipout if the image is placed near a page break, the following \centerimg[<options>]{<image>} command works for all standard document classes, including book:

\documentclass{article}
\usepackage{graphicx,changepage}

\newcommand{\adjustimg}{% Horizontal adjustment of image
  \checkoddpage%
  \ifoddpage\hspace*{\dimexpr\evensidemargin-\oddsidemargin}\else\hspace*{-\dimexpr\evensidemargin-\oddsidemargin}\fi%
}
\newcommand{\centerimg}[2][width=\textwidth]{% Center an image
  \makebox[\textwidth]{\adjustimg\includegraphics[#1]{#2}}%
}

\begin{document}
\mbox{} \par
\noindent\centerimg[width=\paperwidth,height=200pt]{tiger}

\newpage

\mbox{} \par
\noindent\centerimg[width=\paperwidth,height=200pt]{tiger}
\end{document}

The horizontal adjustment for book (obtained via \adjustimg) depends on whether the page number is odd or even. The above MWE, with the tiger image, compiles to the output:

enter image description here

10
  • 4
    I don't think the \paperwidth works other than in the article class, provided no change is made to the pagination parameters.
    – egreg
    Commented Dec 22, 2011 at 22:03
  • 2
    Better would be \makebox[\textwidth]{\includegraphics[width=\paperwidth]{...}} (see Center figure that is wider than \textwidth) or \includegraphics[width=\paperwidth,center]{...} with \usepackage[export]{adjustbox}. Commented Dec 22, 2011 at 22:06
  • 1
    @egreg: I'm using it with success in tex.stackexchange.com/questions/23860/… and it works with memoir as well. Commented Dec 22, 2011 at 22:14
  • @egreg: Do you know the horizontal length causing the difference in book?
    – Werner
    Commented Dec 22, 2011 at 22:18
  • It depends on whether the page is odd or even: \oddsidemargin and \evensidemargin. Look at package layout for getting a nice drawing. However the problem is to know what's the page number!
    – egreg
    Commented Dec 22, 2011 at 22:23
50

This worked for me

\begin{figure}[ht]
\centering
     \includegraphics[width=1.0\textwidth]{Normal_Case_1_req_1_response}
      \caption{Normal Case: 1 Request \& 1 Response.}
       \label{normal_case}
\end{figure}
1
  • 6
    figure is a float and is therefore not directly typeset inside the current environment, but rather floats to the next best position. Therefore putting it into a center environment doesn't make sense and doesn't result in a centered image. For this the \centering macro is used inside figure as you also did. Commented Jan 18, 2013 at 8:11
27

I used pdfpages package to include a png. Simple and effective. Fills the entire page with the graphic.

\usepackage{pdfpages}
...
\includepdf{image.png}

Happy TeXing!

2
  • Seeing as pdfpages supports .png and .jpg as well as .pdf-files this is the easiest way to achieve what OP was trying to do. There's also the possibility to specify optional scale arguments as used with includegraphics.
    – Tarok
    Commented Apr 2, 2017 at 13:19
  • Thanks! By far the easiest way possible... Didn't know that it also supports images :) Commented Jan 8, 2022 at 20:43
18

width=\columnwidth worked for me in a two-column document:

\begin{figure}
    \includegraphics[width=\columnwidth]{myfigure.pdf}
    \caption{My amazing figure}
\end{figure}
2
  • 1
    First one of the answers that worked for me in a minipage / figure, thanks!
    – Vinz
    Commented Jan 23, 2023 at 11:32
  • Works pretty well with pdfLaTeX, too, with a .png figure. Commented Feb 18, 2023 at 6:56
3

Maybe you should work with the easyfig package from Martin Scharrer : CTAN.

Or, maybe, look at this post post where I've ask a similar question (I think at least).

Regards,

2

This worked for me.

\begin{figure}
    \centering
    \resizebox{\textwidth}{!}{\includegraphics{example.png}}
    \caption{CAPTION}
    \label{LABEL}
\end{figure}
1

If you want an image to be automatically scaled to fit both vertically and horizontally while maintaining proportions, this works.

\usepackage{adjustbox}

\adjustbox{max width=\textwidth}{
    \includegraphics[height=\textheight]{image.jpg}
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .