You are currently only viewing entries from July 2013.

To go back to view all items, click here. Also, see August 2013 and June 2013
Monday, 1 July 2013

Simple diagrams for LaTeX with Inkscape

Using Inkscape effectively # Permalink C Comment

Yesterday I discovered how to make nice, simple, elegant diagrams for fairly painless inclusion into \LaTeX. It turns out this is remarkably straightforward to do, but useful to have explained to you once beforehand!

Make It Work

  1. First of all, make sure you have the prerequisite software installed. I'm going to go ahead and assume you have \LaTeX (for instance, the MiKTeX distribution on Windows, or a suitable Unix version). Then you need to get Inkscape itself. This is a very nice piece of vector graphics software which we will find easy enough to make work. Have a play with it.
  2. Next, check you have the \LaTeX packages you need. You will need only graphicx, transparent, color (and only the last two if you want the features they refer to); you almost certainly already have these.
  3. Now make a diagram in Inkscape; draw some filled circles and arrows and so on. Then, use the text editor - but ignore the font - and just type in, for instance, $f(x)$. The one thing you might like to do is make the font small enough to make it easy to position the text. Save it.
  4. A good idea: trim/crop the image by selecting the whole document (usually Ctrl+A) and going to File > Document Properties and using the Resize page... options to resize the document to the size of the selection.
  5. Then export the image:
    1. Go to File > Save a copy (for me that's Ctrl+Alt+Shift+S).
    2. Select the type Portable Document Format (*.pdf). Click Save.
    3. Make sure that PDF+LaTeX: Omit text in PDF, and create LaTeX file is selected (and obviously Convert texts to paths is unselected!). You can play with the rest.
    4. Press OK.
  6. You have now created a pair of files, widget.pdf_tex and widget.pdf. The first is a \LaTeX file containing the text, and the later contains all the drawing. Make sure these are in a directory accessible to the document. (In particular, the first file will just \includegraphics the second one, so make sure that you \graphicspath{{diagram-directory//}} if the files are not immediately accessible.)
  7. To include the diagram, there are three main points to consider. We'll look at these, then give a sample document.
    • You need to include all the relevant packages. You can probably just use \usepackage{graphicx, transparent, color} and forget about it.
    • You need to define a width for the whole image. This is done via commands like \def\svgwidth{0.4\linewidth} or \def\svgwidth{250pt}.
    • You need to actually include everything, which is done with a command like \input{widget.pdf_tex}. This imports the TeX file and renders the text around the image as you directed in the Inkscape version.
    • Here is a sample document:
      \documentclass{article}
      \usepackage{graphicx, transparent, color}
      \usepackage{wrapfig}
      \begin{document}

      \begin{wrapfigure}{r}{0.4\linewidth}
      \def\svgwidth{\linewidth}
      \input{widget.pdf_tex}
      \caption{Ooh, look! Wrapping!}
      \end{wrapfigure}

      \noindent This is a demonstration of a wrapped figure. This is merely some more filler text. There is a LaTeX package for that, but I'm too lazy to use it.
      \clearpage

      \begin{figure}[h]
      \centering
      \def\svgwidth{200pt}
      \input{widget.pdf_tex}
      \caption{Cool, a centered figure!}
      \end{figure}

      \end{document}

Things to Watch Out For

  • If you get "Cannot determine size of graphic" errors referring to PDF files, you most likely compiled with latex instead of pdflatex.
  • As stated above, if you have the diagrams stored in a different directory to the .tex file itself, then even if you \input the correct path, the .pdf_tex file generated by Inkscape doesn't know about this, and just tries including the .pdf file from the current directory - this could lead to the file not being found. You can fix the search path with a command like \graphicspath{{diagram-directory//}}.
  • Using \linewidth for the SVG width is often useful if the diagram is to be included within a small environment of fixed size, since it usually fills the available space in a satisfactory way. However, if your image is still wildly the wrong size, then play with other expressions like \textwidth, \columnwidth.

Inkscape Tips

  • To get nice smooth lines easily, use the pen tool (Shift+F6 for me) which creates nodes and the Spiro mode (second across in the toolbar at the top for me). Inkscape distinguishes between 'cusp' nodes and 'smooth' nodes, and only smooths out the latter. You can manually set nodes to be smooth after drawing them using the node editing tool (F2, thenCtrl-A with some of the path selected and find Make selected nodes smooth on the toolbar), but to make them smooth as you go you should click and drag as you place each node. The clicking places the node, whilst the dragging places a guide which (in the normal drawing mode) would usually specify the tangent to the Bezier curve drawn - in Spiro mode I think this is ignored. When you're done, and press Enter to finalize the path, a curve of 'minimal curvature' in some sense should be drawn linking your points. Note that if you place two points you will get a straight line in this mode!
  • If the diagram has too much whitespace around it, make sure you've trimmed/cropped the Inkscape image as suggested above!
  • You might like to try out inkscape2tikz, which does what it says on the tin - converts Inkscape images into TikZ ones. TikZ is another very nice way of producing quick and dirty diagrams for TeX documents with many excellent features; it's what I used for the Algorithms notes.

Posted by carl at 01:46

Filed under: Computing

Journal from July 2013

All entries from month July 2013

Simple diagrams for LaTeX with Inkscape • 1.7.2013 Yesterday I discovered how to make nice, simple, elegant diagrams for fairly painless inclusion… [read more - comment]No Data Connection (Android) • 4.6.2013 Just spent an age dealing with a phone (Samsung Galaxy S2, I9100, on the UK network 3) running a… [read more - comment]Android/BusyBox Segmentation Faults • 30.9.2012 Just had a terrifying moment when, after attempting to install BusyBox on an Android device,… [read more - comment]Temporarily Redefining In-built Mathematica Functions • 16.7.2012 Suppose a package you're using is, say, zealously Simplifying lots of Mathematica expressions… [read more - comment]Nuclear Power • 24.7.2011 I've never known the answers to the big questions about energy. I do know that with … [read more - comment]
top / xhtml / css
© Carl Turner 2008-2017
design & engine by suchideas / hosted by xenSmart