4

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 ?

  • 1
    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
19
\documentclass[a4paper]{article} %     / after this
\usepackage{graphicx}            % <--{
\begin{document}                 %     \ before this
\begin{figure} 
  \includegraphics[width=\textwidth]{Gaussfit.png}
\end{figure}
\end{document}
4

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.

  • 1
    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

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.