2

For example, if I want to add this image from url

http://www.qqxxzx.com/images/profile-pics/profile-pics-16.jpg

in my pdf, what do I have to do?

I have tried several solutions with href. For example this one

\begin{figure}%[2]
    \centering
    \href{http://www.qqxxzx.com/images/profile-pics/profile-pics-16.jpg}{\includegraphics{profile-pics-16.jpg}}

\end{figure}

but nothing is working. I always get this error

Package pdftex.def Error: File `profile-pics-16.jpg' not found. See the pdftex.def package documentation for explanation. Type H <return> for immediate help. ... l.57 ...pg}{\includegraphics{profile-pics-16.jpg}}
  • 4
    Just download it manually and put it somewhere where latex can find it. – wilx Sep 24 '16 at 8:16
  • 1
    Your code seems to work fine with pdflatex as long as you load the following two packages:\usepackage{graphicx} and \usepackage{hyperref}. If that does not resolve your issue, please compose a fully compilable MWE including \documentclass and the appropriate packages that reproduces the problem. – Peter Grill Sep 24 '16 at 8:57
  • if latex did not find it then presumably you saved the file in the wrong place (same folder as your document is the easiest place) – David Carlisle Sep 24 '16 at 9:27
  • I used the same code but this is not working and giving error, so its not duplicate – user1478137 Sep 24 '16 at 13:25
  • @user1478137: You are probably not running with the --shell-escape flag on. – Werner Sep 24 '16 at 15:40
6

The shell escape feature (\write18) can be used to call external programs during the compilation. The following example first checks, if the file is present. If it is missing, then it calls wget to download the file if pdflatex was called with enabled shell escape feature, e.g. via option -shell-escape or -enable-write18 (MiKTeX).

\documentclass{article}
\usepackage{graphicx}
\begin{document}
\begin{figure}
  \centering
  \IfFileExists{profile-pics-16.jpg}{%
  }{%
    \immediate\write18{%
      wget http://www.qqxxzx.com/images/profile-pics/profile-pics-16.jpg%
    }%
  }
  \includegraphics{profile-pics-16.jpg}
\end{figure}
\end{document}

In case of LuaTeX, shell escape is not necessary, because the downloading can be done by library luasocket.

The following example defines macro \Download, which takes the file name and the URL. If LuaTeX is detected, the downloading is handled in Lua. Otherwise shell escape is needed. For convenience, the file name is stored in macro \DownloadFile for the later use in \includegraphics.

\documentclass{article}
\usepackage{graphicx}
\usepackage{ifluatex}

\ifluatex
  \directlua{
    tex.enableprimitives('', {'luaescapestring'})
  }
  \newcommand*{\Download}[2]{%
    \IfFileExists{#1}{%
    }{%
      \directlua{
        local io = require('io')
        local http = require('socket.http')
        local ltn12 = require('ltn12')
        local file_name = '\luaescapestring{#1}'
        local url = '\luaescapestring{#2}'
        texio.write_nl('Downloading: ' .. file_name)
        texio.write_nl('')
        http.request{
          url=url,
          sink=ltn12.sink.file(io.open(file_name, 'w'))
        }
      }%
    }%
    \edef\DownloadFile{#1}%
  }
\else
  \newcommand*{\Download}[2]{%
    \IfFileExists{#1}{%
    }{%
      \immediate\write18{%
        wget -O "#2" "#1"%
      }%
    }%
    \edef\DownloadFile{#1}%
  }%
\fi

\begin{document}
  \begin{figure}
    \centering
    \Download{profile-pics-16.jpg}{%
      http://www.qqxxzx.com/images/profile-pics/profile-pics-16.jpg%
    }
    \includegraphics{\DownloadFile}
  \end{figure}
\end{document}

Caution: The code does not check for any download errors.

| improve this answer | |
  • For a general solution it would be better to use shellesc's \ShellEscape macro. Newer versions of luatex no longer support \write18. – Josef Sep 24 '16 at 10:41
  • @Josef In LuaTeX the download can be directly managed in Lua, no need for shell escape here, see the updated answer. – Heiko Oberdiek Sep 24 '16 at 11:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.