Tuesday, May 4, 2010

Getting xfig, LaTeX, and LyX to work properly with each other

To get these three guys to cooperate is a hassle if you do not know the secrets.

In xfig, there are few things to remember:

  • Use the 'special' flag when creating text objects.
  • Use LaTeX fonts; 'default' works for me.
  • Use LaTeX code as you normally would. E.g. to enter the antiderivative of the sine function, use $\int \sin (x)$. LyX sometimes balks if slashes (\) are not preceded by empty space, which I do not understand. To place your LaTeX text objects in their correct location with respect to the graphical objects is not straightforward and may take several iterates (see below; I may post something about this intuitive though tedious process).
  • Export to 'Combined PDF/LaTeX (both parts)', though this post should apply equally to the eps format if you make the appropriate changes. If your xfig file is named foo.fig, this will output foo.pdf and foo.pdf_t by default. foo.pdf contains graphical objects without any text that had the 'special' flag on, while foo.pdf_t contains a call to \includegraphics{foo.pdf} and all the LaTeX commands necessary for superimposing the LaTeX onto the graphical objects.

To take care of the first two points, I launch xfig with

xfig -specialtext -latexfonts -startlatexFont default

In LaTeX and LyX you needs to add into your preamble the following:

\usepackage[pdftex]{graphics,color}

\DeclareGraphicsRule{.pdf_t}{pdf}{*}{}

\graphicspath{{path_to_your_graphics/}}

I do not understand why the 'color' package is necessary, but it is. The second line is to tell LaTeX how to handle the graphics extension .pdf_t. The last line is to tell the graphics package where to find graphics files. The trailing / is necessary as per the documentation of the graphics bundle. The alternative to adding the last line is to edit the

\includegraphics{foo.pdf}

line in foo.pdf_t to

\includegraphics{path_to_your_graphics/foo.pdf}

The problem with this is that you need to edit this file after every time you export foo.pdf and foo.pdf_t from xfig and you may need to adjust the LaTeX text objects and export your graphics several times.

At the point in the document where you want the graphic to appear, you can use

\input{path_to_your_graphics/foo.pdf_t}

If you are using LyX, enter this code using LaTeX input (ctrl-l on unix, or Insert -> TeX Code). If LaTeX can find your graphics (i.e. your graphics are in the TEXINPUTS path or kpathsea can find them) then you should be able to get away with

\input{foo.pdf_t}

for the last line, but I have not tried to get this to work.

Friday, May 1, 2009

Interactive Search and Replace in vi

I have found a useful command in vi to perform an interactive search and replace in vi. Suppose that you want to find the string trig and replace it with quart in almost every instance of a file: in such a way that you must interactively confirm which instances to replace and which to leave alone.

The way to do this is, for the above example, i.e. replace trig with quart, is:

:1,$s!trig!quart!cg

You can replace all three ! with some other character. This is useful, for instance, if your search string has a ! in it. The 1 denotes the line that you want to start searching and the dollar sign denotes the line that you want to end at. $ means the last line as per usual.

Saturday, April 4, 2009

A better line style method for Matlab plotting


I will be working with a student to create some seamless way to plot a high resolution data set using line markers to distinguish between data sets. Some reasons for why this is not so straightforward now is that if one wants to use Matlab's legend function to distinguish the data sets current methods make it difficult to get the desired effect.

To the right is an example that shows two aspects that we will try to improve upon: the more efficient use of the space in a figure and the use of plotting markers to distinguish lines without using a marker for each data point.

Notice that there is very little whitespace in the figure to the right. (There was still some unused space between the x axis label and the title of the axes below the label. This should be fixed.) This was achieved in a programmatic way. This is essential if one is generating a series of such plots for inclusion in a laboratory report.

Another aspect to observe in the figure is that though each data set has a high resolution each curve has around 5 or 6 plotting markers. This is to distinguish between the lines when one does not need to see where each data point is.

Matlab's latex interpreter is broken


When trying to use something like

title('$\int_0^1 f(x) \, dx $', 'interpreter', 'latex');

in (some versions of) Matlab on a Unix platform the output is garbled. The image to the left shows a zoom-in of the title of two plots using the above code: Within the image are two parts: the left is the result of executing the above code on a Windows machine while the left was generated by executing the above code on a Unix machine. I wish someone had a fix for this. Such capability would be really useful.

Here is a link to a post from 2008 that shows that this problem has not been taken care of

http://www.mathworks.de/matlabcentral/newsreader/view_thread/161183