Thread Subject: Subscripts in legend

Subject: Subscripts in legend

From: Matthew Rankin

Date: 25 Jul, 2001 09:23:38

Message: 1 of 5

Taking advantage of the TeX interpreter in Matlab, I created a legend
containing some subscripts; however, this caused the text to shift upward,
so that the top of the text was touching the top of the box for the legend.
To fix this, I grabbed the handle of the text object in the legend and
shifted the position of the text, which works great when viewing the figure.
Then when I export the figure to a type 2 color eps file or jpeg file, the
text moves back to its original position, so that when I open the eps or
jpeg file the text is not where I want it to be. Any suggestions (other
than to stop using subscript text in a legend) on how to fix this problem?
I have included a sample of the code below. I am using Matlab 6.0 (R12) on
AIX 4.3.3.

Many thanks,
mdr

%% Initialize
clear all;
in2mil = 1000; % convert inches to mils
pdouter = 140;
pdinner = 180;
trise = linspace(0,350,200);

%% Calculate
linner = in2mil * trise / (pdinner * 6);
louter = in2mil * trise / (pdouter * 6);

%% Plot
figure(1)
plot(trise,louter,'-',trise,linner,'--')
grid on
[legend_handle,object_handles] = ...
    legend('Outer FR4 (140ps/in \rightarrow \epsilon_{eff} = 2.8)',...
    'Inner FR4 (180 ps/in \rightarrow \epsilon_{eff} = 4.5)',2);
% The vertical alignment gets messed up, when using a subscript; therefore
% I must manually adjust the text in the legend.
curpos = get(object_handles(1),'pos');
set(object_handles(1),'pos',curpos + [0 -0.17 0]);

--
M. Rankin
mrankin22@WASHINGTONyahoo.com
Remove the president from email address above to reply.

Subject: Subscripts in legend

From: shay@wise1.tau.ac.il (Shay Zucker)

Date: 1 Aug, 2001 09:05:38

Message: 2 of 5

Hi,

I don't really have an answer, but a few insights of my own,
since I'm battling now exactly the same problem: I understand
every text string you put in a plot has its own bounding box, and
this bounding box is the object that is aligned and positioned.
The position pertains, by default, to the bottom-left of this
box, but you can change it using the "verticalalignment" and
"horizontalalignment" properties. At first I thought that using
a value of "baseline" for the verticallignment would do the job,
because then the alignment is done using the font's baseline and
not the bottom border of the box, but it didn't...

That's what I could come up with so far. If I find another solution,
I'll post it here.

Shay Zucker
Wise Observatory
Tel-Aviv University
Israel

Subject: Subscripts in legend... STYLED TEXT toolbox or BSTEX??

From: Michael Robbins<michael.robbins@us.cibc.com>

Date: 1 Aug, 2001 22:28:28

Message: 3 of 5

Doug Schwarz (schwarz@kodak.com) has generously created the well-done and FREE
Styled Text Toolbox, available at
<http://www.servtech.com/~schwarz/stextfun/index.html>



joesababa@yahoo.com (Joe Sababa) created a toolkit called BSTEX. It is a
pay-for package that I haven't used it but it may work for you.
<http://www.geocities.com/bstex2001>

Michael Robbins, CFA
Director, Debt Capital Markets
CIBC World Markets Corp., Canadian Imperial Bank of Commerce
New York, NY USA
michael.robbins@us.cibc.com , robbins@bloomberg.net

Subject: Subscripts in legend... STYLED TEXT toolbox or BSTEX??

From: craq

Date: 11 Nov, 2011 17:27:11

Message: 4 of 5

this message is really old, but I had the same problem and couldn't find any reasonable solution using version 2010a. Doug's slegend routine has moved to here
http://www.frontiernet.net/~dmschwarz/stextfun/index.html
but I couldn't make that work.

This is my solution:

function shift_underscore_legend_entries(lh)
% shifts legend entries with an underscore down so they are properly aligned
%
% INPUTS:
% lh = legend handle (optional, otherwise the routine tries to find one)
%
% Chris Rapson 2011.11.11

if ~exist('lh','var') || isempty(lh)
    lh=findobj(gcf,'tag','legend');
end

lhc=get(lh,'children');

counter=1;
for i=1:length(lhc)
    if ~isempty(get(lhc(i),'Tag')) && strcmp(get(lhc(i),'Type'),'line')
        myMarker(counter)=lhc(i);
    elseif strcmp(get(lhc(i),'Type'),'text')
        myText(counter)=lhc(i);
        counter=counter+1;
    end
end

for i=1:length(myText)
    myString=get(myText(i),'String');
    if strfind(myString,'_')
        textPos=get(myText(i),'position');
        textExtent=get(myText(i),'extent');
        if any(ismember(myString,'A':'Z'))
            myShift=0.3;
        else
            myShift=0.25;
        end
        set(myText(i),'position',[textPos(1),textPos(2)-myShift*textExtent(4) textPos(3)]);
    end
end
%%
return

%% test routine
figure
plot(1:10)
hold all
plot(1:2:20)
plot(sin(1:10))
legend('C_s','c_s','Cc',2)
% lh=legend('blah_1','blah^2','blah');
shift_underscore_legend_entries%(lh)

Subject: Subscripts in legend... STYLED TEXT toolbox or BSTEX??

From: Philip

Date: 1 Jan, 2012 22:11:08

Message: 5 of 5

You are the man, craq! Thanks a lot. Works like a charm.

Philip

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
legend craq 11 Nov, 2011 12:29:12
subscript craq 11 Nov, 2011 12:29:12
alignment craq 11 Nov, 2011 12:29:12
rssFeed for this Thread

Contact us at files@mathworks.com