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 . 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 (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 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 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

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

suchmod 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' 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}, InternalInheritedBlock[{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 1I can still Simplify: 1Cos[h]^2 + Sin[h]^21 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 InternalInheritedBlock 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

Sunday, 24 July 2011

## Nuclear Power

An uncertain future # Permalink C Comment

I do know that with clever uses of grids there might be some hope for a sustainable future. Yet many of these plans are troubled by lack of (economically viable) technology - renewables are, if not exactly young, still very much in development - and 'long-term' and 'future' are phrases which are used heavily in discussing plans for renewables. (Another interesting question is why renewables have much lower support amongst governments and administrators than the public, and whether this is justified.)

Anyway, for me, one natural alternative to continuing to use fossil fuels whilst we oscillate between plans, and technologies, and try and decide between waiting for the next generation or committing now, is to use an already well-established, efficient, considerably less harmful source of power - nuclear fission.

Now, this isn't a manifesto for nuclear power. It's just a few thoughts. The thing that interests - and frustrates - me is public opinion. The thing which bites at my soul is how misguided most of the common objections really are. One thing I'd like to hammer home into people's minds: fossil fuels are far deadlier than nuclear power. The public image of nuclear power is tarnished by a lot of complicated cultural baggage (I blame the hippies), but precisely how governments can justify turning away from nuclear power based speficially on the perceived dangers of the technology is beyond me. Data from the WHO shows exactly the same thing - that article has a fascinating review of the subject. Read it

Indecision and u-turns plague recent history with nuclear power. China's decision to suspend its nuclear programme in the wake of the Fukushima disaster is deeply frustrating and bodes ill for a country using 47% of the world's coal - and a quantity which has increased by nearly 3 times since 2000 - is one of many worries troubling those concerned with our fuel habits.

Whilst the news is mixed, there is some light at the end of tunnel: the IAEA provides a handy summary of the current status of nuclear power around the world. There are 440 nuclear reactors in operation, and 65 in construction - even the UK has confirmed plans in the wake of the Fukushima event. That's not the rate of growth I'd ideally like to see - though we should bear in mind the improved efficiency of more modern plans - but growth is interestingly regional, with the UAE, Saudi Arabia, and Turkey all with subsantial nuclear plans. The political situation in other areas - notably Egypt - has been frustrating some other efforts in this area. It is, of course, important that we make sure radioactive materials are carefully protected, or more than the case for nuclear power will be irreversibly damaged. I look forward with interest to see what happens in one particular country.

Anyway - and not at all because I've always wanted to say this - Ahmadinejad certainly has the moral high ground here.

Posted by carl at 21:10

Filed under: Culture

# Blog

We regularly update these pages in order to give you the best possible coverage of what we're doing.

Looking through our Journal items for less recent news? You may find it useful to view entries by category.

# Blog

We regularly update these pages in order to give you the best possible coverage of what we're doing.