17

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.

6 Answers 6

19

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.

1
  • Is it possible to make a latex package just like the already existing package https://ctan.org/pkg/svg. The svg package just use the shell escape feature of the latex, and automatically convert the svg file to pdf file through the Inkscape command line. If drawio also has such a command line interface, I think it can also be automatically converted.
    – ollydbg23
    Commented Apr 7, 2021 at 2:27
18

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.

2
  • 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
    Commented Jul 14, 2018 at 12:31
  • 2
    @Cold_Class I'm not sure if this was the case in 2018, but now you can select a region, export to pdf, and select both Selection Only and Crop and it will give you a cropped selection. Unfortunately, it doesn't seem to work perfectly because mine overflowed slightly onto a second page (even though the diagram was very small)
    – JolonB
    Commented Feb 17, 2021 at 20:48
4

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

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.

1

Based off the answer by mplattner I created a makefile to do this. The biggest difference except the makefile itself is that I also added drawio to PATH

FILES := $(wildcard *.drawio)
PDFS  := $(addsuffix .pdf, $(FILES))

.PHONY : all
all : $(PDFS)

%.drawio.pdf : %.drawio
    drawio $< --crop -x pdf -o $@

clean :
    rm *.drawio.pdf
0

Using the Answer from mplattner i hacked together an "enhanced" version of the script, that only converts the figures that have changed since the last git commit. It also works both on Windows and on Linux (executable is named "draw.io" on windows and "drawio" on Linux). It produces a formatted output in the shell while supressing output from f.e. GTK on Linux.

#!/bin/bash

echo ------------------------- Converting drawio to PDF ------------------------------

COUNTERC=0
COUNTERU=0
COUNTERtot=0

for filename in ./figures/drawio/*.drawio; do

    if [ -n "$(git status ${filename} --porcelain)" ]; then
        echo " 🔁 ${filename} changed, converting..."
        DUMMY=$(rm "${filename}.pdf" 2>&1)
        if [[ "$OSTYPE" == "linux-gnu"* ]]; then
            DUMMY=$(drawio ${filename} --crop -x -o ${filename}.pdf 2>&1)
        elif [[ "$OSTYPE" == "msys"* ]]; then
            DUMMY=$(draw.io ${filename} --crop -x -o ${filename}.pdf 2>&1) 
        fi
        COUNTERC=$((COUNTERC+1))
    else 
        echo " ⤵️  ${filename} hasnt changed, skipping..."
        COUNTERU=$((COUNTERU+1))
    fi
    
done
printf "\n✔ DONE converting"
COUNTERtot=$((COUNTERC+COUNTERU))  
echo -e "\n$COUNTERtot total \t $COUNTERC changed \t $COUNTERU skipped" 

I then used a VSCode Task with keybinding to quickly execute the script with a button press. Hope it also works for you.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .