Drafting Scientific Figures with Inkscape

Tags
Productivity
Last edited time
Apr 24, 2024 1:27 PM

🏠 Back Home | 📚 Publications | ✉️ Contact | 📝 Blog

⚠️
The prerequisites of the installation process are a local LaTeX installation (TeX Live is recommended). No particular LaTeX IDE is required. TeXStudio and VSCode with the LaTeX Workshop extension are recommended.

Why Inkscape?

An image is worth a thousand words. It is universally accepted that an illustrative and powerful image enhances the quality of a talk and helps convey an article’s message. However, (electrical) engineers are not graphical designers, and aesthetics leave too much behind in scientific and technical works.

When asked why not invest time in improving the quality of their renders, most (electrical) engineering professionals say “they have no time” to dedicate to TikZ, perhaps the most widespread LaTeX\LaTeX-based library in mathematics and exact sciences for creating artworks. The scope of this blog post is to socialize my personal approach to draft vector-quality graphs using Inkscape, an open-source vector designer tool.

In this blog post, you will learn how to set up Inkscape adding vectorized circuit symbols to draw diagrams like the one shown below.

Workflow in a Nutshell

  • Images are drafted using Inkscape functionality.
  • Mathematical equations are embedded as text objects.
  • ⚠️
    There exist Inkscape extensions to embed an equation object into the Inkscape canvas. Personally, I feel I have more control by adding math objects using the LaTeX\LaTeX syntax as described in this post.
  • LaTeX\LaTeX is used to render the mathematical equation metadata into a *.pdf file, generated by compiling a standalone TeX document.

Extensions for Power Users

It is possible to automate the presented workflow using Vim or VSCode. I leave a couple of references below.

  • Gilles Castel’s (RIP) approach relies on Vim.
  • I found sleepymalc's instructions difficult to follow; besides, I could not replicate them fully on macOS (and the approach did not work on Windows).

Installation Instructions

  1. Download and install Inkscape from the official website (below). The installer is available as an *.msi file for Windows.
  2. ⚠️
    Make sure that the downloaded version is >1.X.

    In macOS, the installation can be done via brew (recommended).

  3. Get the *.svg file for circuitikz symbols from the following GitHub repository.
  4. If the repository above does not work, a (possibly outdated) copy is available below.

    circuitikz.svg99.7KB
  5. Navigate to the Inkscape local roaming directory.
  6. Windows: if the local disk is the C:\ unit, then the path to the inkscape folder should be as follows:

    C:\\Users\\%USERNAME%\\AppData\\Roaming\\inkscape

    Create a new folder called symbols (if it does not exist yet). Paste the circuitikz *.svg file inside the new directory.

    macOS: open a terminal and query the path to the Inkscape installation.

    inkscape --user-data-directory

    For Inkscape >1.X and macOS >12.0, the path should be as follows:

    ls~/Library/Application Support/org.inkscape.Inkscape/config/inkscape/
  7. Restart Inkscape (or launch it if it is not open). Go to ObjectSymbols (Windows: Shift + Ctrl + Y; macOS: Shift + Cmd + Y) in a new document. Look for the CircuitTikZ Electrical Symbols option in the dropdown list.
  8. image

    The CircuiTikZ symbols will be available as drag-and-drop objects that can be placed directly into the drawing canvas.

Description of the Workflow

The approach to drawing figures is as follows.

  • Sketching is done with Inkscape.
  • Text objects (e.g., labels, titles, and legends) are also added to the diagram on the Inkscape canvas.
  • If LaTeX rendering is required, it is suggested to “encapsulate” the LaTeX commands within brackets. For instance, if we want to add the legend Vg=2.0 pu\left\vert V_g \right\vert = 2.0 \ \text{pu}, we would add the following text in Inkscape.

    {$\left\vert V_g \right\vert = 2.0 \ \text{pu}$}

    The text will not be rendered on Inkscape. However, it will be recognized by a LaTeX compiler when using the associated *.pdf_tex file (see below).

  • When the figure is ready, export the figure as a *.pdf file to the destination folder. Make sure the option Omit text in PDF and create LaTeX file is checked under the available options (unchecked by default). A *.pdf and a *.pdf_tex file will be generated on the desired output directory by clicking on Export. The figure needs both files (i.e., the *.pdf and the *.pdf_tex).
  • ⚠️
    As long as the *.pdf_tex and the *.pdf files are in the same directory, the *.pdf_tex file might be treated as an “image” file and used within floating objects such as Figures.
💡
With the default configuration, such changes in the Inkscape options are saved so that they are available afterward.
  • The following template can be used to generate a *.pdf containing the complete figure by compiling a LaTeX file. The use of the standalone class produces a document whose size adjusts to the exact extent of the figure.
  • ⚠️
    This approach allows the use of the figure as a standalone image in other programs and files (e.g., to import it on a slideshow or use it as a figure externally).
    \documentclass{standalone}
    
    \usepackage{newtxtext}
    \usepackage[T1]{fontenc}
    
    \usepackage{import}
    \usepackage{xifthen}
    \usepackage{pdfpages}
    
    \begin{document}
    
    % \def\svgwidth{\columnwidth}
    \import{./Figures/}{fig_LODF-illustration.pdf_tex}
    
    \end{document}

Example

The goal of this example is to draft the figure shown below.

For reference, a copy of the source Inkscape file is provided below.

example_single-line-diagram.svg8.9KB
  1. Create a new Inkscape document.
  2. Right-click the drawing canvas and go do Document Properties. Change the units in the Front page menu to inches (in). Then, set the page width to 3.5 inches (which is the column width in IEEE double-column format). Also, change the Display units to inches.
  3. Drag and drop the following components from the CircuiTikZ Electrical Symbols library.
    • american_inductor
    • ground
    • oscillator
    • resistor
    • polar_capacitor
  4. Connect the elements as per the example image above. Note the following.
    • The snapping option has been enabled in the top right corner. Below it is shown where to enable it in the graphical interface.
    • image
    • All the objects are rescaled, holding Ctrl to preserve their individual aspect ratios.
    • The line width is set to 1.0 pt under Stroke style (Width property).
    • The buses are created using the path tool with a line width of 2.0 pt.
  5. Add the text labels as shown in the image above. Use text objects to do so. Note that:
    • The text alignment shall be set to center before the text content is input.
    • image
    • The LaTeX\LaTeX equations shall be included within brackets and inline delimiters (i.e., $$).
    • The position of the text shall be set according to the object center, not the text extent as shown in Inkscape (it is misleading).
    • The text positions displayed on the Inkscape canvas do not correspond to the actual text position after rendering in
      The text positions displayed on the Inkscape canvas do not correspond to the actual text position after rendering in LaTeX\LaTeX.
    • The text color can be changed from Inkscape. The text fill corresponds to the color rendered in LaTeX\LaTeX. It is recommended to set the text stroke to transparent.
  6. When the text is complete, right-click the canvas and go to Document Properties again. Click the Resize to content button to adjust the height and width of the objects in the canvas. Then, set the figure width to 3.5 inches.
  7. Go to FileExport (Windows: Ctrl + Shift + E; macOS: Shift + Cmd + E). In the new menu,
    • Change the file format to *.pdf.
    • Set the DPI size to 300 (it defaults to 96).
    • Click the wheel icon.
    • image
    • In the Export Format Options window, enable the Omit text in PDF and create LaTeX file option (disabled by default).
  8. Export the figure by clicking on the Export button. This will generate two files. A *.pdf containing the artwork and a *.pdf_tex file with the text objects and their relative coordinates concerning the *.pdf.
  9. ⚠️
    By default, the *.pdf_tex and *.pdf names are the same as the Inkscape document.

    You might change the output directory (i.e., it need not be the same as the path where the *.svg Inkscape file is saved).

  10. Create a new LaTeX\LaTeX document. Note that if placed in the same directory as the *.pdf_tex and *.pdf files from step 8, the name of the LaTeX\LaTeX file must be different than that of the *.svg document. The contents of the *.tex file shall be as follows.
  11. \documentclass{standalone}
    
    \usepackage{newtxtext}
    \usepackage[T1]{fontenc}
    
    \usepackage{import}
    \usepackage{xifthen}
    \usepackage{pdfpages}
    
    \begin{document}
    
    % \def\svgwidth{\columnwidth}
    \import{./}{example_single-line-diagram.pdf_tex}
    
    \end{document}
    ⚠️
    The code above assumed that the *.pdf_tex and the *.pdf files are in the same directory as the *.tex file. Otherwise, the absolute path shall be passed as the first argument to the \import command.
    ⚠️
    I recommend using a modern LaTeX\LaTeX compiler to have more control over the figure. I personally use LuaLaTeX.
  12. Building and compiling the *.tex file in Step 9 generates a *.pdf file containing the artwork (i.e., the graphics and the text). Such a file can be used in LaTeX\LaTeX documents, PowerPoint presentations, and Overleaf projects.
💡
Steps 9 and 10 can be done directly on Overleaf. In fact, as long as the *.pdf_tex and *.pdf files from step 8 are in the same directory, the command \import{} can be used within an Overleaf project (the commented line in the code snippet above controls the width of the rendered image).

2024 © Sergio A. Dorado-Rojas