17

The code below is executing properly:

\begin{figure}
\includegraphics{name1}
\caption{Figure 1}\label{name1}
\end{figure}

However when I add width and height:

\begin{figure}
\includegraphics[width=15cm,height=6cm]{name1}
\caption{Figure 1}\label{name1}
\end{figure}

I get this error:

! Missing number, treated as zero.<to be read again>

How do I solve this problem?

29

The optional argument with key value options are added by package graphicx, note the x at the end. Package graphics has a different syntax and meaning of the optional arguments, from the documentation of packages graphics and graphicx, grfguide:

graphics: \includegraphics[⟨llx,lly⟩][⟨urx,ury⟩]{⟨file⟩}
graphicx: \includegraphics[⟨key val list⟩]{⟨file⟩}

Solution:

\usepackage{graphicx}
...
\includegraphics[width=15cm, height=6cm]{name1}

This might distort the image, if the specifications do not hit the aspect ratio of the image. Option keepaspectratio scales the image down if necessary to fit the available space but without distorting the image:

\includegraphics[
  width=15cm,
  height=6cm,
  keepaspectratio,
]{name1}
3

I read a lot about that and found an answer:

add \shorthandoff{=} AFTER \begin{document}.

Edit: \includegraphics{figure.png} always works great. But whenever I added anything else, such as \includegraphics[height=0.5\textwidth,angle=-90]{figure.png}, it didn't compile. Normally it doesn't do that but that happens whenever I use babel package. That happens because babel package changes = sign and add some other things. That is why I needed to reset = by adding \shorthandoff{=}. At least that was what solved my problem.

  • 4
    You should explain (briefly) what the code does or why the code works. – Héctor Jun 18 '17 at 19:21

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.