5

I have a number of documents (jupyter notebooks) that are created and accessed essentially using a browser interface, and because of the browser interface the cleanest way to include external vector pictures in a document is using the SVG format.

Those documents can be converted to LaTeX documents, and while it's easy to add further material to the produced file, in terms of loading packages, redefining commands etc it is not so easy to modify the rest of the produced text, that in particular comprises lines like

\includegraphics{my_picture.svg}

that pdflatex does not appreciate at all.

My question, is it possible to redefine \includegraphics so that pdflatex tries to load my_picture.pdf when it is instructed to load my_picture.svg?

I know that I can have an automatic conversion from .svg to pdf but due to other reasons I already have the .pdf files (that are, in effect, produced using LateX and later converted to .svg)


P.S. It is possible to post-process the LaTeX file, as produced by the conversion procedure, before running pdflatex (or should I say pre-process?) but I'd like to avoid this intermediate step.

gboffi
  • 911

2 Answers2

7

You can add a graphics rule (I didn't test if it works for all sort of pathes and curious file names):

\documentclass[a4paper,12pt]{book}


\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{etoolbox}
\makeatletter
\preto\Gin@extensions{svg,}
\DeclareGraphicsRule{.svg}{pdf}{.pdf}{\noexpand\Gin@base.pdf}
\makeatother

\begin{document}

\includegraphics{example-image-A.svg}

\end{document}
Ulrike Fischer
  • 327,261
  • Thank you Ulrike, not only your solution works but works at the right level, defining a new file extension and declaring the intended behaviour right inside of the graphics machinery... Accepted & upvoted. – gboffi Jun 24 '16 at 14:48
  • Does this only work with pdflatex? With xelatex it raises an error: Unable to load picture or PDF file 'example-image-A.svg'. – Clément Nov 06 '20 at 05:03
  • @Clément no it doesn't work with xelatex. – Ulrike Fischer Nov 06 '20 at 10:21
3

It is always possible to redefine a command, the question is : will it break something if I do it? If you are certain that \includegraphics is used only to import svg files, than this should work:

\documentclass{article}
\usepackage{graphicx}

\let\oig\includegraphics
\def\topdf#1.svg{#1.pdf}
\renewcommand{\includegraphics}[2][]{\oig[#1]{\topdf#2}}

\begin{document}
\includegraphics[width=.25\linewidth]{test.svg}
\end{document}

Macro \topdf strips off the svgextension and replaces it with pdf: \topdf lalala.svg -> lalala.pdf (no braces around the argument).