Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SVG images in Markdown are not rendered in latex export #244

Open
amueller opened this issue Feb 12, 2016 · 11 comments
Open

SVG images in Markdown are not rendered in latex export #244

amueller opened this issue Feb 12, 2016 · 11 comments
Labels
format:PDF pertains to exporting to the PDF format Preprocessor:SVG2PDF

Comments

@amueller
Copy link
Contributor

This is a bit of a tricky one I guess.
By default latex doesn't support svg images, but they are a nice format if you want to show vector graphics in a browser. I could convert to png but that seems not great.
there is a svg package that could be used to convert svg packages to pdf on the fly.

@jakobgager
Copy link
Contributor

Actually svgs should be converted to pdf using inkscape during the nbconversion. How do you use/embed the svg?

@amueller
Copy link
Contributor Author

![some_svg](some.svg) in a markup cell. I'll try a minimal reproduction later.

@amueller
Copy link
Contributor Author

Ok, so I did ![svg_thing](overfitting_underfitting_cartoon.svg) which gave me

! LaTeX Error: Unknown graphics extension: .svg.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.255 ...ics{overfitting_underfitting_cartoon.svg}

? 
! Emergency stop.
 ...                                              

l.255 ...ics{overfitting_underfitting_cartoon.svg}

!  ==> Fatal error occurred, no output PDF file produced!
Transcript written on notebook.log.

when trying to convert to pdf.

the generated latex is

    \begin{figure}[htbp]
\centering
\includegraphics{overfitting_underfitting_cartoon.svg}
\caption{svg\_thing}
\end{figure}

@jakobgager
Copy link
Contributor

You are right. In your particular case only the syntax is converted from markdown to latex but the linked content not.
If the svg data is embedded in the notebook, e.g. created by a matplotlib plot this svg data is converted during the extraction to pdf. Hence, it would be a good idea to write a preprocessor which captures all linked content and converts is as required. Here, it would be possible a well to download images which are linked via a web address.

@mpacer
Copy link
Member

mpacer commented Sep 17, 2016

Is there a reason this is better to have as a jupyter extension rather than as part of core nbconvert preprocessors?

@Carreau, @takluyver, @minrk how do we determine what goes in the extensions repo and what is incorporated into core projects? Is there a system for migrating things? This seems like important svg support and may address other issues that I'm having difficulty finding right now.

@amueller
Copy link
Contributor Author

I agree with @michaelpacer that this would be good to have in the main repo.

@takluyver
Copy link
Member

@michaelpacer sometimes we ask people to make an extension if something is too specific for the main repos, or we're not sure about how to design it. But in this case, it's an extension largely because @juhasch, who maintains the repo with a collection of extensions, got round to doing it.

There's no system to migrate things to or from extensions, it would just involve making a pull request like any other feature.

Note that we already have svg2pdf conversion, but as @jakobgager said, it doesn't currently handle svgs linked into the notebook in markdown cells.

@t-makaro t-makaro changed the title SVG images are not rendered in latex export SVG images in Markdown are not rendered in latex export Jun 10, 2019
@t-makaro t-makaro added format:LaTeX pertains to exporting to the LaTeX format format:PDF pertains to exporting to the PDF format and removed format:LaTeX pertains to exporting to the LaTeX format labels Jun 10, 2019
@romor001
Copy link

Ran into this issue when trying to convert my notebooks contaning SVG images in Markdown into Pdf.
Any workaround recommended other than converting the SVGs to PNG?

@MichaelAdolph
Copy link
Contributor

MichaelAdolph commented Nov 19, 2020

@romor001 I also ran into trouble with converting SVGs. I hunted down the problem, and indeed: there are two problems:

Some specs: I'm using jupyter-nbconvert 6.0.7 on python 3.8.6, windows PC.
Inkscape is installed into the default folder C:\Program Files\Inkscape

And here is the first problem, the whitespace in "Program Files" makes the call fail, because it is not enclosed in double quotes or in another way escaped.

The second problem is that the initial --export-filename will raise an error because auf an overwriting risk, it should be changed to --export-type. But than you have to change the value (from a filename to just "pdf").

All in all I change the site-packages\nbconvert\preprocessors\svg2pdf.py the following way:

    @default('command')
    def _command_default(self):
        self.inkscape_path_string = f'\"{self.inkscape}\"' # added this to escape the whitespace
        major_verison = self.inkscape_version.split('.')[0]
        export_option = ' --export-type' if int(major_verison) > 0 else ' --export-pdf'
        gui_option = '' if int(major_verison) > 0 else ' --without-gui'
        to_filename = 'pdf' if int(major_verison) > 0 else to_filename # Need this for backward-capability

        return '{inkscape}{gui_option}{export_option}='.format(
            inkscape=self.inkscape_path_string, export_option=export_option, gui_option=gui_option
        ) + f'{to_filename}' + " {from_filename}" # needed to update the string, otherwise it did not work for my (but that is probably my fault)

Now converting Jupyter Notebooks works.

I'll look at a way to update the package and create a pull request.

@MichaelAdolph
Copy link
Contributor

I added a pull request for this issue #1469 , this should also work for issue #1325

@aravindh-krishnamoorthy
Copy link

aravindh-krishnamoorthy commented Nov 16, 2024

In PR #2190 a workaround is provided that hooks onto \includegraphics and replaces it with \includesvg for SVG files. But the resulting LaTeX files need -shell-escape to compile as \includesvg uses Inkscape for conversion, see also issue #2192.

I understand that -shell-escape may not be secure and might allow malicious users to include arbitrary commands in a Python notebook, which will activate during LaTeX compilation or export.

On the other hand, I also do not think messing with ![](file.svg) in a markdown cell and converting it to PDF on the fly is a good idea.

So, not sure what's the best way forward here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
format:PDF pertains to exporting to the PDF format Preprocessor:SVG2PDF
Projects
None yet
Development

No branches or pull requests

8 participants