Search code examples
rlatexsweave

Using graphicspath in latex/sweave


I have an Sweave file, where I need to embed a png and pdf from another folder. This is how I tried it

\usepackage{graphicx}
\graphicspath{{inst/extdata/}}
\DeclareGraphicsExtensions{.pdf,.png}
\begin{document}

\begin{figure}
   \includegraphics[scale=0.7]{image.png}
   \caption{captionA}
   \label{fig:labelA}
\end{figure}

I also tried like this, but this didn't work:

 \graphicspath{{filePath}}

 filePath = "C:/path"

\begin{figure}
   \includegraphics[scale=0.7]{image.png}
   \caption{captionA}
   \label{fig:labelA}
 \end{figure}

I have googled a lot, and look up https://en.wikibooks.org/wiki/LaTeX/Importing_Graphics#Compiling_with_latex, but this is the error I get.

 LaTeX Warning: File `image.png' not found on input
 line 613.

! Package pdftex.def Error: File `image.png' not found.

See the pdftex.def package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.613 ...]{image.png}

Solution

  • I expand my comment with three different ways of achieve your goal; Suppose you want to include a file image named IMG_7254.JPG (note that I do not write the file extension in the following code because I use the \DeclareGraphicsExtensions{.JPG}), You can do it in three ways:

    1. to put the file to compile (the .tex one) and the image to include in the same folder. LaTeX searches (by default) for pictures to include in documents in the same directory where the file to be compiled is. In this case the code is just:

      \includegraphics[width=0.7\textwidth]{IMG_7254}
      
    2. Have pictures and file to compile in very different folders, in this case you must specify the complete path of the file (it can be found in different ways), example with Mac OS read here. In this case the code is:

       \includegraphics[width=0.7\textwidth]{/Users/SabDeM/Pictures/IMG_7254}
      
    3. to put the file to compile in a folder (say File_to_com), then put all pictures to include in a sub folder, say, File_to_com/picturesToInc, and then specify the path. In this latter case the code is:

      \includegraphics[width=0.7\textwidth]{picturesToInc/IMG_7254}
      

    As you can see in all ways (one way or another) LaTeX (which render the PDF) must to know where the pictures are located in your computer.