2

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.
3

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.

  • 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
1

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:

    {\scriptsize\input{mydrawio.pdf_tex}}
    
1

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:

    #!/bin/bash
    
    /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.
    \begin{figure}[h]
      \caption{Caption for my figure}
      \centering
      \includegraphics[width=0.75\textwidth]{images/drawio/myfigure.drawio.pdf}
    \end{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.

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.