Search code examples
matlabfigures

Add extra whitespace to matlab legend (for use with psfrag)


How to pad white-space on a matlab legend to the right of the text? I am using a combination of psfrag (and adobe illustrator for a few other diagram modifications), and will replace the placeholder text in a figure with an equation. The problem is that it tightly bounds the box on the placeholder text, while I want to leave room for my equation

Start with the simple figure;

h_plot = plot([0 1], [0 1]); 
h_legend = legend('A',0);

The spacing I really want would be something like this with

h_plot = plot([0 1], [0 1]); 
h_legend = legend('A!!!!!!!!',0);

where the !!!!!!!! is actually whitespace, and it really is stored as one character 'A'.

A few things which did not seem to work:

  1. One obvious solution is: just add in text such as "A!!!!!!!!!!!!" and replace the whole text with my equation in psfrag. However, if I touch the file with Adobe Illustrator, then it converts the text to individual characters, which breaks psfrag (see http://engineeringrevision.com/314/getting-illustrator-to-play-nicely-with-psfrag/ for example). So I really need to just have the 'A' character as the string.

  2. Another is to try to stretch the box, but chanking the position or the aspect ratio streches the text and line accordingly.

For example, the following just stretches the width

h_plot = plot([0 1], [0 1]); 
h_legend = legend('A',0);
leg_pos = get(h_legend,'position'); leg_pos(3) = leg_pos(3) * 2;
set(h_legend, 'position', leg_pos);
  1. The legendflex file looks very interesting, but I think the control over the whitespace buffering was only for the position of the legend itself.

Solution

  • You can add any of the first 32 ascii codes (non printable characters) to create a space. Not sure it will work with psfrag though.

    Here, the piece of code creates 30 spaces using ASCII code 3.

    h_plot = plot([0 1], [0 1]); 
    h_legend = legend([ 'A' repmat(char(3),1,30) ],0);
    

    EDIT

    Another possibility. You can use the handles from legend. Here, changing the legend's text from 10 to 1 characters do not modify the size of the legend box.

    [~,OBJH,~,~] = legend('0123456789');  % display a legend of 10 characters
    set(OBJH(1), 'String', 'A');          % change its String to 1 character
    

    -- see comments: saving as .eps brings back the old string in the image file created.

    enter image description here