Skip to content

Automatically convert SVG for PDF output #196

Closed

Description

@kuon

I use SVG images in my quarto document, like so:

![Electronic overview](overview.svg){layout-align=center}

it works fine for HTML, but for PDF it says:

LaTeX Error: Unknown graphics extension: .svg.

I know that latex do not support svg, and that the usual workflow is to convert svg to PDF using inkscape or similar.

Is there a way to automate this with quarto?

Activity

  1. jjallaire commented on Nov 21, 2021

    @jjallaire
    Collaborator

    The easiest thing to do would be to write a Lua filter (https://quarto.org/docs/authoring/shortcodes-and-filters.html#filters) that automatically uses a (pre-generated) PDF or PNG in place of SVG when rendering to PDF. It would look something like this:

    function Image(el)
      if isLatexOutput() then
        --- check el.src for an svg extension
        --- re-assign el.src to the pdf or png version
        --- return the modified element
       return el
     end
    end
    
    local function isLatexOutput()
      return FORMAT == "latex" or FORMAT == "beamer" or FORMAT == "pdf"
    end

    This should indeed be a built in feature (so leaving this open for resolution later on)

  2. kuon commented on Nov 21, 2021

    @kuon
    Author

    I got the filter working, but is it possible to have the absolute path to the image file within the filter? I was unable to find how.

    For example, if the processed document is chapter/section/hello.qmd with intro.svg in it, I only get intro.svg and no way to get the chapter/section/intro.svg full path to access the image.

    This would let me to automatic "lazy" pdf conversion within the filter.

  3. tarleb commented on Nov 22, 2021

    @tarleb
    Collaborator

    The pandoc.path module (and maybe pandoc.system) can be helpful here. Maybe something like

    local path = require 'pandoc.path'
    local input_dir = path.directory(PANDOC_STATE.input_files[1])
    local relative = path.join{input_dir, 'INPUT-RELATIVE-FILE-PATH-GOES-HERE'}
    
  4. jjallaire commented on Nov 22, 2021

    @jjallaire
    Collaborator

    If you configure your filter to run after the quarto filters like this:

    filters:
      - quarto
      - filter.lua

    Then you'll get the right path (it will be chapter/section/intro.svg and the current working directory will be the project root, so that relative file reference will work).

    I have been debating in my head whether user filters should run before or after Quarto filters --- this makes me think that I may have the default wrong (currently user filters run first).

  5. jjallaire commented on Nov 22, 2021

    @jjallaire
    Collaborator

    I've changed the default to have user filters run after quarto filters here: 606f92f

    So your filter should work w/o any special configuration.

    We are ultimately going to need to allow fairly fine grained control (i.e. per filter) over where user filters run. We will will also need to make some of Quarto's higher level constructs like tabsets, callouts, etc. directly available to filters.

  6. sijow commented on Dec 3, 2021

    @sijow
    Contributor

    I think a nice way to solve this particular problem is to write the image filename without extension in the markdown source, e.g. write simply ![Electronic overview](overview), and call quarto with the option --default-image-extension=svg to generate the web version, and with --default-image-extension=pdf to generate the PDF.

  7. jjallaire commented on Jan 8, 2022

    @jjallaire
    Collaborator

    FYI we now provide default-image-extension automatically (it defaults to png and then uses pdf for pdf formats): https://quarto.org/docs/authoring/figures-and-layout.html#multiformat-figures

  8. benabel commented on Mar 14, 2022

    @benabel

    @kuon I use this filter that uses inkscape to convert svg to pdf for latex output.

    function Image(el)
        if FORMAT == "latex" then
            -- convert svg to pdf
            if string.sub(el.src, -4) == '.svg' then
                local pdfName = string.gsub(el.src, "svg", "pdf")
                pandoc.pipe('/usr/bin/inkscape', { el.src, '--export-filename', pdfName}, '')
                el.src = pdfName
                return el
            end
        end
    end
  9. fuhrmanator commented on Jun 18, 2022

    @fuhrmanator
    Contributor

    I'm very late to this party, but just wanted to share that I am using latexmk for the pdf and self-contained: true for html in a book project, and I'm able to see my .svg files just fine (no filter necessary for SVG files, but I do use a filter to convert PlantUML/Graphviz diagrams into SVG).

    format:
    
      html:
        # needed for inline graphviz figures
        self-contained: true
    
      pdf:
        # has to be latexmk for graphviz/svg figures
        pdf-engine: latexmk

    The full repo is at https://github.com/fuhrmanator/quarto-test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions