如何在MATLAB中显示米勒指数?

Cro*_*ley 5 matlab text latex labels

我正在使用MATLAB绘制XRD分析,其中米勒指数用于识别晶面方向.这些索引包含3或4个数字,负数值显示为此数字以上的条形.

在LaTeX中,它可以由\([1\bar{1}1]\)\([1\overline{1}1]\)命令编写.

为了标记XRD标准的光谱线,我使用此命令:请注意,不考虑负值.

std_text_hkl(j)=text(theta{i}(j)-r,0.1,['[' hkl{j} ']'],... % position and label
       of j-th line of i-th standard; hkl{j} holds Miller index in string format
    'parent',ax_std(i),... % association with axes of i-th standard
    'rotation',90,...
    'fontsize',12,...
    'fontname',Font); % Font holds global font setup
Run Code Online (Sandbox Code Playgroud)

如何在不使用'Interpreter','latex'属性的情况下自动创建负数而不是使用属性,因为我希望能够更改'FontName'属性.在leat我想避免标签和刻度中的不同字体.

编辑:
感谢Magla的评论我得到了这个想法:

  • 将索引存储为3列矩阵
  • 将标签分成5个文本字段
  • 如果米勒指数是负的画线(文本框架的顶行)

实际代码:

rr=get(ax_std(i),'xlim'); % read x-axis limits of i-th standard
    r=(rr(2)-rr(1))/150; % x-offset of Miller indexes
    for j=1:size(dhkl,1)
      theta{i}(j)=asin(lambda/(2*dhkl(j,1)))*360/pi(); %calculating of lines
                   %positions (Bragg's law)
      line('parent',ax_std(i),...
            'xdata',[theta{i}(j) theta{i}(j)],...
            'ydata',[0 dhkl(j,2)],... % j-th line's reflection intensity
            'color',[colors(1+mod(i-1,size(colors,1)),1:3)],...
            'linewidth',3)

%         Miller indexes

      if theta{i}(j)>rr(1)&&theta{i}(j)<rr(2) % test if line is inside axes
          std_text_lbrace(j)=text(theta{i}(j)-r,0.1,'[',...
            'parent',ax_std(i),...
            'verticalalignment','bottom',...
            'horizontalalignment','left',...
            'rotation',90,...
            'fontsize',12,...
            'fontname',Font);
        pos=get(std_text_lbrace(j),'position');
        ext=get(std_text_lbrace(j),'extent');

        std_text_h(j)=text(pos(1),pos(2)+ext(4)/1.5,int2str(abs(hkl(j,1))),...
            'parent',ax_std(i),...
            'verticalalignment','bottom',...
            'horizontalalignment','left',...
            'rotation',90,...
            'fontsize',12,...
            'fontname',Font); % write 1st Miller index
        pos=get(std_text_h(j),'position');
        ext=get(std_text_h(j),'extent')
        if hkl(j,1)<0 % if negative, draw line over it
            wdth=get(ax0,'xlim');
            wdth=wdth(2)-wdth(1);
            set(std_text_h(j),'color','b','edgecolor','g')
            line('parent',ax_std(i),...
                'xdata',[pos(1)-wdth/280*ext(3),pos(1)-wdth/280*ext(3)],...
                'ydata',[pos(2),pos(2)+ext(4)/wdth*100],...
                'color','r')
        end
      end
Run Code Online (Sandbox Code Playgroud)

我无法适应线长.对于单个数字,它太长了,因为它适合两个数字而且更多(理论上)它太短了.我究竟做错了什么?MATLAB如何测量'extent'旋转文本的属性?

mar*_*sei 3

这是一段在负数顶部显示上划线的代码。该解决方案不使用'interpreter','latex'因此可以选择不同的字体。请注意,该代码使用一组单个文本框,每个文本框都有一个\nchar(10)在顶行显示下划线(char(95)' '表示正数),并在底行显示关联的数字。人们可以选择使用两个不同的文本框来设置下划线与其数字之间的特定距离。不过,这段代码并不适用于所有字体(我想说我的系统字体 90% 都可以正常工作)。

下面的代码

%Miller indices
miller_ind = [1 -1 -2 3 -3];

%font definition
c = listfonts;
ind_perm = randperm(length(c));
font_names = {'Arial','Times','Courier New',c{ind_perm}};
font_size = 16;

figure('Color','w','Position',[10 10 600 1000]);
py = 0.05;
for ind_font = 1:12

    %font name
    text(0.03,py,font_names{ind_font},'FontName',font_names{ind_font},'FontSize',font_size);

    %plot miller textbox
    px = 0.6;
    for ii = 1:length(miller_ind)
        if miller_ind(ii)<0
            text(px,py,[char(95) char(10) num2str(-1*miller_ind(ii)) ],...
                'FontName',font_names{ind_font},'FontSize',font_size,'interpreter','none');
        else
            text(px,py,[' ' char(10) num2str(miller_ind(ii)) ],...
                'FontName',font_names{ind_font},'FontSize',font_size,'interpreter','none');
        end
        px = px + 0.03;
    end
    py = py + 0.09;
end
Run Code Online (Sandbox Code Playgroud)

给出这个结果

在此输入图像描述

编辑 感谢@Oleg Komarov 的评论。图片现在直接另存为 . tiff而不是通过.eps.