6

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.
jlperla
  • 201
  • 2
  • 8

3 Answers3

4

To add extra withspace to legend, simply use

{          }  -> { "add how much space you wish between two brackets" }

for example:

legend( 'A{     }','b{    }' ) 
KittMedia
  • 7,368
  • 13
  • 34
  • 38
2

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

marsei
  • 7,691
  • 3
  • 32
  • 41
  • Thank you, this is very close. I played around with most of the non-printing characters. Curiously, (32), the space and (0), the null get trimmed by legend, but most others do not. I realize you can't entirely recreate my toolchain, but the only problem is that when I open these up in illustrator they end up being in weird visible boxes. – jlperla Aug 26 '14 at 16:52
  • Do you think there is any way to add in a blank 'column' in the legend instead? – jlperla Aug 26 '14 at 16:53
  • 1
    See the edit. The final string is one character in the big initial legend box. – marsei Aug 26 '14 at 19:25
  • It displays great. but when I saveas eps, it reverts to the old text! Funny enough, it doesn't revert when saved as a .fig! – jlperla Aug 26 '14 at 19:48
  • Weirdo! Graphics... Maybe you can copyobj the final legend, delete it, and paste it again, so that a trace of the ancient text is removed. – marsei Aug 26 '14 at 20:17
  • Crazy, right? I think that your initial approach works best. It has the manual step of opening the .eps file and hitting delete on the weird non-printable boxes, but that isn't all that cumbersome. Consider editing your edit to point out the weirdness of the .eps save – jlperla Aug 26 '14 at 20:23
1

The function legend has an argument 'Location'. You can pass a vector to it, i.e.

plot(1:10)
legend('Sometext', 'Location', [0.20, 0.1, 0.75, 0.25])

where the latter vector can be interpreted as [Position_Right_in_pct, Position_Top_in_pct, Horizontal_Stretch, Vertical_Stretch]

Ufos
  • 3,083
  • 2
  • 32
  • 36
  • This will position the legend and resize it, *as a whole*. How does this specifically add a white space anywhere? – Adriaan Jun 26 '16 at 13:57
  • 1
    You cannot add whitespace "anywhere", but you can add it almost anywhere. Unfortunately, there is no way to position the lines, the rest is adjustable: `Horizontal_Stretch` and `Vertical_Stretch` define the size of your box, you then can add blank characters to your text, or, if you are using LaTeX interpreter, corresponding tex-commands. P.S. The original question was about the space to the *right* of the line. – Ufos Jun 28 '16 at 09:30