Importing a .png file into .tex file

I've been trying to input a graph into my .tex file, but it won't work. I've put the .png file in the same folder as the .tex file and a part of my code looks like:

\documentclass[16pt, a4paper]{article}
\begin{document}
.
.
.

\usepackage{graphicx}
\begin{figure}

\includegraphics{Gaussfit.png}
\end{figure}
\end{document}


The error message I'm getting is:

! LaTeX Error: Can be used only in preamble.


How can I fix this problem ?

• article class has not got a 16pt option, and \usepackage as it says in the errror message has to be in the preamble ie before \begin{document} so just move that line. – David Carlisle Feb 3 '14 at 11:19
• Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. As David said, put the \usepackage{graphicx} in the preamble, which is between the \documentclass and \begin{document}. – hpesoj626 Feb 3 '14 at 11:21
• Thanks, it woks. But the graph is too large. How can I define the size of the graph? – Nikoleta Feb 3 '14 at 11:28

\documentclass[a4paper]{article} %     / after this
\usepackage{graphicx}            % <--{
\begin{document}                 %     \ before this
\begin{figure}
\includegraphics[width=\textwidth]{Gaussfit.png}
\end{figure}
\end{document}


You can also pass an option to set the scale of the image, like so:

\includegraphics[scale=0.5]{Gaussfit.png}


In this case, the image will be scaled to half size. Also, including the file extension, i.e. .png is not mandatory.

You can check for additional options here.

• scale might not be a good idea. Better to scale using say width=0.8\textwidth, or use this interesting answer tex.stackexchange.com/q/157337/3929 – daleif Feb 3 '14 at 15:39
• Can you explain why wouldn't scale be a good idea? – Oxfist Feb 3 '14 at 15:44
• Do you know the original size? So the value for scale will be trail and error, plus what if the margins change? Then you have to go over all of them to make sure they are no longer too wide. – daleif Feb 3 '14 at 15:48