2

I'm reading the tutorial on LaTeX by overleaf, and in their section Adding Images they have some example code:

\documentclass{article}
\usepackage{graphicx}
\graphicspath{ {images/} }

\begin{document} The universe is immense and it seems to be homogeneous, in a large scale, everywhere we look at.

\includegraphics{universe}

There's a picture of a galaxy above \end{document}

When explaining the \includegraphics{universe} part of that they say:

The \includegraphics{universe} command is the one that actually included the image in the document. Here universe is the name of the file containing the image without the extension, then universe.PNG becomes universe. The file name of the image should not contain white spaces nor multiple dots.

My question really comes down to this: If I had in that folder both universe.png and universe.jpg what would be chosen? Why?

  • If using pdfLaTeX, the order is \def\Gin@extensions{.pdf,.png,.jpg,.mps,.jpeg,.jbig2,.jb2,.PDF,.PNG,.JPG,.JPEG,.JBIG2,.JB2} – Phelype Oleinik Oct 29 '20 at 17:24
  • 1
    This is engine/backend dependent. For pdfLaTeX, what @PhelypeOleinik said. Take a look at the definitions inside <path to texlive>/texmf-dist/tex/latex/graphics/, in texmf-dist/tex/latex/graphics-def, and in texmf-dist/tex/latex-dev/graphics. There are some .def files in which the different backends are managed. – Skillmon Oct 29 '20 at 17:27

1 Answers1

3

In case the file has no extension (no . in the last part of the name after all path separators /), or in case the extension is not a known graphics extension (for example, if the file is called big.universe, universe is not a valid graphics extension), then graphics does something like:

    \@for\Gin@temp:=\Gin@extensions\do{%
      \ifx\Gin@ext\relax
        \Gin@getbase\Gin@temp
      \fi}%

The loop above will iterate in the \Gin@extensions list, and try to lookup a file named <base>.\Gin@temp. If the file exists, the extension \Gin@ext is set equal to \Gin@temp and it doesn't try other extensions, so the earlier items in \Gin@extensions have higher priority.

Each backend (output format) allows different graphics file formats. The usual ones:

  • dvips (latex):
    \def\Gin@extensions{.eps,.ps,.eps.gz,.ps.gz,.eps.Z,.mps}
    
  • pdftex and luatex:
    \def\Gin@extensions{%
      .pdf,.png,.jpg,.mps,.jpeg,.jbig2,.jb2,%
      .PDF,.PNG,.JPG,.JPEG,.JBIG2,.JB2%
      % .eps is usually in this list, if conversion to pdf is enabled
    }
    
  • xetex (dvipdfmx):
    \def\Gin@extensions{% order here is like pdftex.def, except for PS
      .pdf,.PDF,.ai,.AI,%
      .png,.PNG,.jpg,.JPG,.jpeg,.JPEG,.jp2,.JP2,.jpf,.JPF,.bmp,.BMP,%
      .ps,.PS,.eps,.EPS,.mps,.MPS,%
      .pz,.eps.Z,.ps.Z,.ps.gz,.eps.gz}
    
  • 1
    Noteworthy difference from xetex to the others: extensions in all-caps have a higher priority. – Skillmon Oct 29 '20 at 17:55
  • Is there any particular reason why they chose these arbitrary orders? – Lazerbeak12345 Oct 29 '20 at 18:19
  • @Lazerbeak12345 No idea. It looks as if they are from most to least common (well, I've never seen an adobe illustrator file, but...), but I'm not sure. I can ask around – Phelype Oleinik Oct 29 '20 at 18:34
  • 1
    @Lazerbeak12345 Apparently kind of arbitrary: https://chat.stackexchange.com/transcript/message/55989663#55989663 Really the rules are not intended for you to select files based on extension hierarchy. Of course they allow that, but I'd still favour adding the extension explicitly. In my experience guessing extensions is usually worse rather than better... – Phelype Oleinik Oct 29 '20 at 19:17
  • 1
    @Lazerbeak12345 the model was that authors of dvi drivers supplied the lists for the extensions they support, so different people wrote different lists at different times, it's not surprising the orders are different. – David Carlisle Oct 29 '20 at 19:24