Yesterday I discovered how to make nice, simple, elegant diagrams for fairly painless inclusion into . It turns out this is remarkably straightforward to do, but useful to have explained to you once beforehand!
Make It Work
- First of all, make sure you have the prerequisite software installed. I'm going to go ahead and assume you have (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.
- Next, check you have the 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. - 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. - A good idea: trim/crop the image by selecting the whole document (usually
Ctrl+A
) and going toFile > Document Properties
and using theResize page...
options to resize the document to the size of the selection. - Then export the image:
- Go to
File > Save a copy
(for me that'sCtrl+Alt+Shift+S
). - Select the type
Portable Document Format (*.pdf)
. ClickSave
. - Make sure that
PDF+LaTeX: Omit text in PDF, and create LaTeX file
is selected (and obviouslyConvert texts to paths
is unselected!). You can play with the rest. - Press
OK
.
- Go to
- You have now created a pair of files,
widget.pdf_tex
andwidget.pdf
. The first is a 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.) - 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}
- You need to include all the relevant packages. You can probably just use
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 ofpdflatex
. - 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 findMake 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 pressEnter
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.
0 Comments