I created a diagram in draw.io that I'd like to include in my LaTeX file. Draw.io has the option to export as svg, png or pdf.

When the svg is included LaTeX re-renders the fonts inside the svg with its own style (font size and font family) which results in a bad looking diagram.

The png can be included and looks nice but I read that including a vector graphic usually gives better results in printing.

The pdf export of draw.io gives me a full A4 page around the diagram and lots of whitespace. I can include it in my LaTeX file but it the whitespace makes it look aweful.

Do you have a solution on including a draw.io image?

Let's say:

  1. How to crop a PDF to its content.
  2. How to stop rerendering an SVG so it looks the same in draw.io and LaTeX.

I integrated draw.io into my LaTeX workflow for my thesis as follows:

Tested on Ubuntu 18.04. Directory paths need to be adjusted accordingly.

  1. Install "draw.io Desktop" from https://about.draw.io/integrations/
    I used draw.io-amd64-10.7.7.deb as I use Ubuntu.
  2. Start draw.io Desktop and use it to create figures.
    Store them under ./document/images/drawio/ with .drawio as the extension, eg. myfigure.drawio
  3. Convert the .drawio files to PDF files. I use a short script to automate that process.
    Add convert_drawio_figures.sh to your project:

    /usr/bin/find ./document/images/drawio -name *.drawio -exec rm -f {}.pdf \; -exec /usr/local/bin/draw.io --crop -x -o {}.pdf {} \;

    This script executes draw.io for all .drawio files under ./document/images/drawio/ and exports them as a PDF files to the same directory with the same name, eg. myfigure.drawio is exported to myfigure.drawio.pdf. Removing the existing PDF file first was necessary as otherwise draw.io outputs "Error writing to file".

  4. Include the generated PDF in your tex file. Adjust the LaTeX options to your needs.
      \caption{Caption for my figure}
  5. Run convert_drawio_figures.sh manually before building your LaTeX document, or add it to your build configuration to run it automatically every time you build.

With this approach all your .drawio files are stored locally. I add them to the git repository of my document and exclude the generated PDF file in the .gitignore file.

| improve this answer | |

Answering my own question, the draw.io tool has the possibility to crop PDF output to the size of the actual diagram.

How? Select PDF... from the list of available exports.

PDF export

Then tick the Crop checkbox and now include the PDF in your LaTeX code.

Crop option

Compiling your document with PDF images is nicer but also slower than PNGs so while you are at it, also create a PNG export, put both files in your LaTeX folder and omit the file extension in the includegraphics command. Now you can later choose the preference in image formats.

| improve this answer | |
  • this helped me, thx - I just wish there would be a third option that crops the selection :D - now I have to delete everything I want to omit first and paste it back after exporting. – Cold_Class Jul 14 '18 at 12:31

Some ideas:

  • The easiest way might be to just search "crop PDF" with your favorite search engine and let some online tool do the cropping for you.
  • When including a PDF with \includegraphics, one can specify in the optional argument to do some cropping like:

    \includegraphics[trim=<left> <bottom> <right> <top>, clip]{mydrawio.pdf}
  • When you use the "Export as PDF" feature of SVG with the option to render the text as LaTeX, you usually do something like \input{mydrawio.pdf_tex} and the text is written using the current font settings of LaTeX. I like this a lot because this way the diagrams have the same font as the text around, however you have to adjust the font settings such that the text only takes the same space as in draw.io.

    Without knowing exactly how your image looks in draw.io and in LaTeX, I cannot give you the precise solution. But usually you start by trying to change the font size:

| improve this answer | |

In Windows just open a PowerShell and use:

C:\Program Files\draw.io\draw.io.exe C:\path\to\files\ --crop -x -o {}.pdf {} .

to convert all .drawio-files in C:\path\to\files\ to cropped pdfs. Make sure to remove all PDFs from the directory beforehand.

You need to have draw.io-desktop installed, of course.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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