You are currently only viewing items in the 'Computing' category:

View All Entries | View Other Categories

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

Tuesday, 4 June 2013

No Data Connection (Android)

No idea if this actually helps, but... # Permalink C Comment

Just spent an age dealing with a phone (Samsung Galaxy S2, I9100, on the UK network 3) running a custom 4.2 android build which, despite having perfect signal and nothing having been changed, steadfastly refused to even attempt to establish a data connection.

Things I tried included

  • Repeatedly toggling airplane mode and all the various 'data enabled'-type settings knocking around in the menus.
  • Pulling the battery and SIM out for a bit.
  • Changing the APN settings, including resetting them to default, trying my own ones, removing the "APN type", changing the "APN type" to "default", ...
  • Dialing *#*#4636#*#* (that's "info" in the middle there) and tweaking the preferred network type, toggling the radio, using menu -> "Enable data connection"...
  • Updating the ROM.

Literally none of these did anything. Then, later, when I ran "logcat" in a terminal emulator in desperation to stare at the output as I toggled data and airplane mode, it immediately worked. Coincidence?

Yes, it turns out my carrier network was down. Siiiigh.

PS: the *#*#4636#*#* thing was quite interesting. The relevant bits were "GRPS service: Disconnected", "GSM Disconnects: ========DATA========", "PPP sent: 0 pkts, 0 bytes / PPP received: 0 pkts, 0 bytes". I note that even though it's all working fine now, the "Data attempts: 0 / Data successes: 0" still look a bit odd.

Posted by carl at 09:22

Filed under: Computing

Sunday, 30 September 2012

Android/BusyBox Segmentation Faults

Oops. # Permalink C Comment

Just had a terrifying moment when, after attempting to install BusyBox on an Android device, almost all console commands seg faulted. This is possibly because I ran out of space on the /system partition, though I'm not certain. (To fix that simply requires deleting some things you don't want, like .apk files for apps the manufacturer/carrier dumped in /system/apps.)

To fix it, I found out that the /system/xbin/busybox executable which was installed was the wrong version/corrupt, so I went to the BusyBox site to download a (more?) correct version; I used the armv6l app for a Samsung Galaxy S2. Then one needs to replace the existing version with a working version. (The following is based on this.)

On your computer, go to the directory containing the file 'busybox' which you just downloaded, and run

adb push busybox /data/local/tmp/
adb shell

Now there is a console open on screen which is actually on the phone. Type

su
chmod 755 /data/local/tmp/busybox
/data/local/tmp/busybox mount -o rw,remount /system
/data/local/tmp/busybox cp /data/local/tmp/busybox /system/xbin/

If one hasn't already installed BusyBox, you can also issue

/system/xbin/busybox –install -s [INSTALLATION DIRECTORY, PERHAPS /system/xbin/]

which sets everything up - BusyBox installers work by creating a folder on the device filled with executables (or better, links to executables) which run /system/xbin/busybox itself. (For me, this was actually at /sbin.)

Posted by carl at 17:37

Filed under: Computing

Monday, 16 July 2012

Temporarily Redefining In-built Mathematica Functions

A neat trick # Permalink C Comment

Suppose a package you're using is, say, zealously Simplifying lots of Mathematica expressions inefficiently, and you'd rather do that later yourself. Can you prevent this from happening?

Yes, very easily, by using the Block environment, so long as you don't need access to the Simplify function in your new, temporary definition:

NiceFunction[arg_] := Block[{Simplify = Function[x, x]}, NastyFunction[arg]]

This code redeclares Simplify as being the 'pure function' x \mapsto x and then evaluates NastyFunction. A simple example to test this:

NastyFunction[arg_] := Simplify[arg + Sin[h]^2]
NastyFunction[Cos[h]^2] (* outputs 1 *)
NiceFunction[Cos[h]^2] (* outputs Cos[h]^2+Sin[h]^2 *)
Simplify[%] (* outputs 1 *)

What about if you need to be able to use the original function?

More Sophisticated Needs

If you still want access to the old definition of the method you're modifying in your new version of the function, you could do something like this:

ClearAll[DoItNicely];
SetAttributes[DoItNicely, HoldFirst];
DoItNicely[code_] := Module[{$inNicely},
Internal`InheritedBlock[{Simplify},
Unprotect[Simplify];
Simplify[arg_] :=
Block[{$inNicely = True},
Print["I can still Simplify: " <> ToString[Simplify[arg]]];
arg
] /; ! TrueQ[$inNicely];
Protect[Simplify];
code
]
];

We use that (originating from a useful answer I found after reading an excellent Stack Overflow thread on Mathematica tricks) like the following:

NastyFunction[arg_] := Simplify[arg + Sin[h]^2]
NastyFunction[Cos[h]^2]
DoItNicely[NastyFunction[Cos[h]^2]]
Simplify[%]

which produces the output

1
I can still Simplify: 1
Cos[h]^2 + Sin[h]^2
1

This uses a variable $inNicely (which must not be used elsewhere!) to determine whether to use the original definition or the new one, and the undocumented Internal`InheritedBlock to completely copy the original function into the new context so that it can be locally modified. To change this for your own purposes, just replace Simplify with the name of the function you're interested in, and modify the two lines of code Print[...]; arg to what you want the new function to do. (You might need to change the number of arguments to the function too, which is fine. Remember that to match arbitrary numbers of arguments use a double underscore after the variable name, args__.).

Posted by carl at 11:38

Filed under: Computing

Wednesday, 15 September 2010

signgam Error Compiling plotutils-2.6

How to compile plotutils-2.6 (on MinGW) # Permalink C Comment

I was just compiling the GNU plotutils library (version 2.6) using MSYS for MinGW, and had the following problematic error:

undefined reference to `signgam'

It turns out there are problems with the 'gauss' methods provided by various platforms: in the file specfun.c in the ode directory of the plotutils source, the following comment occurs:

/* The following gamma-related nonsense is necessary because (1) some
   vendors have lgamma(), some have gamma(), and some have neither [see
   include/sys-defines.h for further comments], (2) some vendors do not
   declare whichever function they have [e.g. Irix 5.3 requires an
   auxiliary preprocessing symbol to be defined for the declaration in
   math.h to be visible], and (3) some vendors supply broken versions which
   we can't use [e.g. AIX's libm.a gamma support is conspicuously broken],
   so we need to link in a replacement, but we can't use the same name for
   the external symbol `signgam'.  What a mess! -- rsm */

There are some threads discussing ways of solving this in old versions, and apparently only with Cygwin; however, I found that the simplest way to solve the problem is to make use of the C macro NO_SYSTEM_GAMMA referenced in the plotutils source - the package will then declare its own gamma functions for us. (So in fact, this isn't an error with plotutils, it's plotutils that provides the solution!)

The fix: make clean the directory and then run configure with an additional flag for the compiler defining the variable NO_SYSTEM_GAMMA. For example:

./configure --prefix=/where/to/install CFLAGS="-DNO_SYSTEM_GAMMA" --enable-libplotter

Then running make, make check and make install as usual, you should be good to go!

Remark: I actually had problems with the make check phase, which reported errors (8 of 11 failures, in fact!) when the relevant files (the .out and .xout files in the test directory) seemed to match. I recommend not worrying/bothering with these unless you have problems, or spot a potential future problem.

Other remark: The --enable-libplotter switch for the configure command is only needed if you want to use the C++ wrapper!

Posted by carl at 13:51

Filed under: Computing

'Computing' Category

Any observations, commentaries, helpful hints or voyages of discovery relating to any OS or software.

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