Importing diagram from draw.io into LaTeX
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.
Then tick the Crop
checkbox and now include the PDF in your LaTeX code.
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.
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.
- Install "draw.io Desktop" from https://about.draw.io/integrations/
I used draw.io-amd64-10.7.7.deb as I use Ubuntu. - Start draw.io Desktop and use it to create figures.
Store them under ./document/images/drawio/ with .drawio as the extension, eg.myfigure.drawio
Convert the .drawio files to PDF files. I use a short script to automate that process.
Addconvert_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 tomyfigure.drawio.pdf
. Removing the existing PDF file first was necessary as otherwise draw.io outputs "Error writing to file".- 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}
- 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.
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}}