In my pre­vi­ous blog post, I ex­plained how I take lec­ture notes using Vim and LaTeX. In this post, I’ll talk about how I draw fig­ures for my notes using Inkscape and about my cus­tom short­cut man­ag­er.

Some examples

First, let me show you some ex­am­ples of fig­ures I’ve made. They were made for com­plex analy­sis, dif­fer­en­tial geom­e­try, elec­tro­dy­nam­ics and my bach­e­lors’s the­sis on el­lip­tic curves. I drew them dur­ing the lec­ture — ex­cept of course those for my the­sis — using Inkscape, so let’s start with that.

What is Inkscape?

Inkscape is an open source vec­tor graph­ics ed­i­tor, avail­able for all major plat­forms. It’s a free — but ar­guably less fea­ture­ful — al­ter­na­tive to Adobe Il­lus­tra­tor. You can use it for de­sign­ing fly­ers and logos like in the pic­ture below, but it’s also a pow­er­ful tool for draw­ing math­e­mat­i­cal fig­ures.

Why Inkscape?

The most com­mon so­lu­tions for adding fig­ures to LaTeX doc­u­ments are TikZ, PSTricks and As­ymp­tote. These op­tions have one thing in com­mon: they are com­mand based, i.e. you just write some code that spec­i­fies how the fig­ure should be drawn. For ex­am­ple, the fol­low­ing TikZ code

\begin { tikzpicture } \coordinate (A) at (1, 3); \draw [ thick, -> ] (0, 0) -- (A);

ode [ above right ] at (A) { $A = (1, 2)$ } ; \end { tikzpicture }

gives this fig­ure:

The ben­e­fit of these pack­ages is that draw­ing a fig­ure is a lot like pro­gram­ming: you can use vari­ables, do cal­cu­la­tions, use for loops, etc. Fur­ther­more fig­ures blend in nice­ly in your doc­u­ment be­cause all text is type­set by LaTeX it­self. This means that you can type­set math ef­fort­less­ly, or that if later on, you de­cide to change the font of your doc­u­ment, all the fig­ures will change au­to­mat­i­cal­ly to match the new font.

How­ev­er, these as­sets cost you vi­su­al feed­back and speed. Draw­ing com­plex fig­ures is in­her­ent­ly a graph­i­cal task and with­out a graph­i­cal in­ter­face, it can be ex­treme­ly time con­sum­ing. It’s im­pos­si­ble to click and drag an ob­ject to move it, free­hand draw a curve or drag the con­trol points of a Bézier curve. This makes TikZ a lot hard­er and slow­er to use than Inkscape. While I use TikZ once in a while for com­plex fig­ures, I found that in most cases, the ben­e­fits of Inkscape far out­weigh those of TikZ, es­pe­cial­ly if you’re under time pres­sure dur­ing a lec­ture.

With this out of the way, let’s get start­ed.

Including Inkscape figures in a LaTeX document

Just like TikZ, Inkscape has the op­tion to ren­der the text of a fig­ure using LaTeX. For this, it ex­ports fig­ures as both a pdf and a LaTeX file. The pdf doc­u­ment con­tains the fig­ure with text stripped, and the LaTeX file con­tains the code need­ed to place the text at the cor­rect po­si­tion. For ex­am­ple, sup­pose you’re work­ing on the fol­low­ing fig­ure in Inkscape:

To in­clude this fig­ure in a LaTeX doc­u­ment, you’d go to File › Save As, se­lect ‘pdf’ as the ex­ten­sion and then press Save which makes the fol­low­ing di­a­log pop up:

Choos­ing ‘Omit text in pdf and cre­ate LaTeX file’, will save the fig­ure as pdf+LaTeX. To in­clude these Inkscape fig­ures in your LaTeX doc­u­ment, you can add the fol­low­ing code to your pre­am­ble:

\usepackage { import } \usepackage { xifthen } \usepackage { pdfpages } \usepackage { transparent }

ewcommand { \incfig } [ 1 ] { \def \svgwidth { \columnwidth } \import { ./figures/ } { #1.pdf_tex } }

As­sum­ing the fig­ure is lo­cat­ed at figures/riemmans-theorem.svg , it can sim­ply be in­clud­ed with fol­low­ing code:

\begin { figure } [ ht ] \centering \incfig { riemmans-theorem } \caption { Riemmans theorem } \label { fig:riemmans-theorem } \end { figure }

Com­pil­ing your doc­u­ment, you’d get the fol­low­ing.

As you can see, the text is ren­dered by LaTeX which makes the fig­ure blend in beau­ti­ful­ly. When you later de­cide to change the font, it gets up­dat­ed ac­cord­ing­ly:

This setup al­lows you to draw fig­ures in Inkscape, while still hav­ing the power of LaTeX for type­set­ting.

Creating and including figures quickly

When I’m tak­ing notes dur­ing a lec­ture, I need to be able to add a fig­ure with­out dis­rupt­ing my flow, and not spend time open­ing Inkscape, search­ing for the cor­rect di­rec­to­ry, typ­ing the LaTeX code for in­clud­ing the fig­ure man­u­al­ly and so on. To avoid this, I added some short­cuts to Vim for man­ag­ing my fig­ures. For ex­am­ple, when I type the title of the fig­ure I want to cre­ate and press Ctrl+F , the fol­low­ing hap­pens:

The script finds the fig­ures di­rec­to­ry de­pend­ing on the lo­ca­tion of the LaTeX root file. Then it checks if a fig­ure with the same name ex­ists. If so, the script does noth­ing. If not, my fig­ure tem­plate gets copied to the fig­ures di­rec­to­ry. The cur­rent line which con­tains the fig­ure title gets re­placed with the LaTeX code to in­clude the fig­ure. The new fig­ure opens in Inkscape. A file watch­er is set up such that when­ev­er the fig­ure is saved as an svg file by press­ing Ctrl + S , it also gets saved as pdf+LaTeX. This means that the an­noy­ing pdf save di­a­log we’ve dis­cussed be­fore doesn’t pop up any­more.

See it in ac­tion:

When I want to edit a fig­ure, I can press Ctrl+F in nor­mal mode to open a se­lec­tion di­a­log that al­lows me to search for fig­ures in the cur­rent doc­u­ment. Upon se­lect­ing one, it shows me the fig­ure in Inkscape. When I save the fig­ure, the code for in­clud­ing it is copied to the clip­board. This way, I can rein­clude it if I delet­ed the orig­i­nal code to do so.

These short­cuts make adding and open­ing fig­ures a breeze. I don’t have to re­mem­ber sav­ing fig­ures as pdf+LaTeX, pick the right di­rec­to­ry or write the code need­ed for in­clud­ing the fig­ure. The bar­ri­er to add a new fig­ure is much lower than if I’d have to do those things man­u­al­ly. You can find my script for man­ag­ing fig­ures on Github.

Now that I’ve ex­plained how I man­age my fig­ures, I’d like to talk about ac­tu­al­ly draw­ing fig­ures in Inkscape.

Drawing figures

While draw­ing fig­ures in Inkscape is quite a bit faster than using TikZ in most cases, it is still slow­er than draw­ing them by hand. Using the built-​in short­cuts of Inkscape speeds up the process but it still doesn’t hit the mark.

There­fore, I de­cid­ed to pro­gram a cus­tom short­cut man­ag­er in Python which al­lows me to in­ter­cept all key­board events be­fore they reach Inkscape. This way, I have full con­trol over how every key­stroke gets in­ter­pret­ed, giv­ing me a lot of flex­i­bil­i­ty.

Drawing shapes

Let’s start with the built in key­board short­cuts of Inkscape. For ex­am­ple, r ac­ti­vates the rec­tan­gle draw­ing tool, e draws el­lipses, etc. Implementation-​wise, this means that the short­cut man­ag­er will ‘re­play’ these key­board events, i.e. it’ll just pass them through to Inkscape.

How­ev­er, in­stead of the de­fault short­cuts p for pen­cil and b for the Bézier tool, I use w and f , as these are a bit more com­fort­able to reach while using a mouse with my right hand. In the spir­it of mak­ing short­cuts left hand­ed, I also mapped z to undo, Shift+z to delete, and x to tog­gle snap­ping which is nor­mal­ly the hard to reach % key.

Key chords to apply commonly used styles

Styling ob­jects is the sec­ond most com­mon thing I do in Inkscape. The styles I use for draw­ing fig­ures are quite sim­ple:

Shapes e.g. rec­tan­gles or cir­cles are most­ly black, light gray, white or trans­par­ent and op­tion­al­ly have a stroke.

Lines (in­clud­ing strokes) are most­ly solid, dot­ted or dashed. They can be (very) thick or have a nor­mal width and op­tion­al­ly have an arrow on ei­ther side.

These op­tions com­bined give the fol­low­ing table of com­mon­ly used styles:

As ap­ply­ing these styles is some­thing I do very fre­quent­ly, I want it to be fast, but using the de­fault short­cuts in Inkscape takes too long. For ex­am­ple, sup­pose you’d like to change the style of some ob­jects as fol­lows:

You’d press Ctrl+Shift+F to open the styles panel, and up­date the style of each ob­ject by click­ing be­cause you can’t do this using key­board short­cuts in Inkscape. This is tire­some. Ide­al­ly, it should only take a frac­tion of a sec­ond.

This is where key chords come into play. A key chord is a key­board short­cut which con­sists of two or more si­mul­ta­ne­ous key­strokes. For ex­am­ple, when I press s and f si­mul­ta­ne­ous­ly, my short­cut man­ag­er will apply a solid stroke and a grey fill to the cur­rent se­lec­tion. When I want the stroke to be thick, I press s + f + g to­geth­er, where g stands for thick (as the t key is hard to reach).

This way, every styling prop­er­ty cor­re­sponds to a key: s stands for nor­mal stroke, f for gray fill, g for thick, a for arrow, d for dot­ted, e for dashed, etc. Here’s the full table of pos­si­ble op­tions:

Some styles in this table core­spond to only one key, e.g. the style in the top left cor­ner: filled in gray with­out a stroke. This could present a prob­lem, as only press­ing f gets you the Bézier tool. The so­lu­tion is to press Space + f where Space acts like a place­hold­er.

Using these key chords, the pre­vi­ous prob­lem can be solved in a few key­strokes:

f + s makes the rectangle gray and gives it solid border.

+ makes the rectangle gray and gives it solid border. f + h + e stands for 'filled and very thickly dashed'.

+ + stands for 'filled and very thickly dashed'. a + g + d adds an arrow and makes the line thickly dotted.

Adding text

An­oth­er im­por­tant part of mak­ing fig­ures is adding text. As fig­ures often in­clude math­e­mat­i­cal for­mu­las, I want to be able to use my snip­pets in Vim. To en­sure this, I can press t which opens a small Vim win­dow where I can type LaTeX. When I quit, a text node is in­sert­ed in Inkscape:

As I cov­ered be­fore, this text will be ren­dered by LaTeX when I in­sert the fig­ure in a doc­u­ment. How­ev­er, some­times I want to ren­der some LaTeX im­me­di­ate­ly, which I can do with Shift+T :

Both these op­tions have their ad­van­tages and dis­ad­van­tages. I main­ly use the first method be­cause the text is ren­dered by the LaTeX doc­u­ment. This means the font will al­ways match and that you can use macros de­fined in the pre­am­ble of your doc­u­ment. A dis­ad­van­tage how­ev­er is that po­si­tion­ing of text is some­times a bit dif­fi­cult. As you don’t see the final re­sult in Inkscape, it some­times re­quires some hop­ping from Inkscape to my pdf view­er and back to fine-​tune the re­sults.

Saving and using less commonly used styles

While the key chord styles suf­fice 90% of the time, I some­times want to use a cus­tom style. For ex­am­ple, to draw op­tics di­a­grams it’s use­ful to have a glass and a ray style. To achieve this, I first have to cre­ate the style in Inkscape using the de­fault short­cuts:

To save these styles, I se­lect one of these ob­jects and press Shift + s . Then I type the name of the new style, in this case, ‘glass’ and press enter to con­firm. Now this style is saved and I can use it later on.

Now, when I se­lect an ob­ject, press s and type ‘glass’, the cor­re­spond­ing style will be ap­plied to the ob­ject. How­ev­er, it’s not nec­es­sary to type the full name be­cause style gets ap­plied im­me­di­ate­ly when there an unique style match­ing the typed char­ac­ters so far. For ex­am­ple, if I’d have only one style start­ing with ‘g’, typ­ing ‘g’ would be enough to apply the style. If you’d have mul­ti­ple styles, ‘gl’ or maybe even ‘gla’ would be nec­es­sary.

The an­noy­ing thing about this is that when­ev­er you type too many char­ac­ters — i.e. sup­pose typ­ing ‘gl’ would apply the style, but you’ve typed ‘gla’ — the re­main­ing keys, in this case a would be in­ter­pret­ed as an­oth­er com­mand, which would give un­in­tend­ed re­sults. There­fore, the short­cut man­ag­er waits 500 ms be­fore it goes back to the de­fault mode. This is enough time for a human to see that the style has been ap­plied and to stop typ­ing.

Adding and saving objects

The final el­e­ment of my setup is adding and sav­ing ob­jects. I can add them with a and save them with Shift+a . For ex­am­ple, press­ing a and typ­ing ‘ec’ adds an el­lip­tic curve:

As an­oth­er ex­am­ple, press­ing a and typ­ing ‘dg’ adds a ‘key­hole’ (‘dg’ is the mir­ror image of ‘kh’ on the key­board). I can use Ctrl+- to sub­tract it from a given shape:

Some other ex­am­ples are blobs and 2D, 3D axis, which I’ve used quite fre­quent­ly in com­plex analy­sis and dif­fer­en­tial geom­e­try.

Code

If you want to try this out for your­self, you can find both my script for man­ag­ing fig­ures in Vim and my Inkscape short­cut man­ag­er on Github. The first script should work out of the box on both Mac and Linux-​based sys­tems; the sec­ond only works on Linux (it uses Xlib) and is a bit hard­er to set up.

Conclusion

Using Inkscape, draw­ing fig­ures for ar­ti­cles, books and pre­sen­ta­tions is a breeze. They look pro­fes­sion­al and blend in nice­ly in your doc­u­ment. While it’s slow­er than draw­ing them by hand, it’s faster than TikZ in most cases. My cus­tom short­cuts and my script for man­ag­ing fig­ures in Vim make it even faster, al­low­ing me to draw fig­ures dur­ing lec­tures.

Liked this blog post? Con­sid­er buy­ing me a cof­fee!