【问题标题】:Partially changing the color of text in a text box部分更改文本框中文本的颜色
【发布时间】:2019-02-05 12:48:14
【问题描述】:

我正在尝试包含一个小文本框,其中包含一个在绘图中显示结果的表格。在表格中,我只想更改单个单词或符号的文本颜色。

表格是使用表格和 LaTeX 标记创建的。出于某种原因,来自TextBox Properties 的某些命令(例如\it)可以工作,而例如\color{red} 则不起作用。你知道让它上色的方法吗?

figure
str = '\begin{tabular}{lr} $\it test$ & A \\  $\color{magenta} test$ & A\end{tabular}';  
h = annotation('textbox',[.15 .15 .2 .28],...  
            'Interpreter', 'latex',...
            'FitBoxToText','on',...
            'EdgeColor','black',...
            'BackgroundColor', [1 1 1]);
set(h, 'String', str);

【问题讨论】:

  • IIRC MATLAB 根本没有实现所有的 TeX 命令。有些有效,有些无效。
  • @Adriaan 我认为它不包含包,\color 来自xcolor
  • 这里的问题是什么?为什么它不起作用,或者如何使它着色?
  • 感谢大家的cmets。问题是如何让它着色?

标签: matlab colors textbox latex matlab-figure


【解决方案1】:

您遇到的问题是仅当Interpreter 属性设置为'tex' 时才支持文本着色,但仅当解释器设置为'latex' 时才支持tabular environment。您最好的解决方法可能是使用the jLabel option suggested by Zep

我能看到的唯一方法是使用'tex' 解释器并自己管理水平元素间距。您可以使用cell array of strings 创建多行文本:

str = {'{\it test}   A', '{\color{magenta} test}   A'};
set(h, 'Interpreter', 'tex', 'String', str);

【讨论】:

    【解决方案2】:

    您可以作弊并使用支持 HTML 标记的未记录的 jLabel 对象。

    figure
    str = '<HTML><FONT color="red">Hello</Font></html>';  
    jLabel = javaObjectEDT('javax.swing.JLabel',str);
    [hcomponent,hcontainer] = javacomponent(jLabel,[100,100,40,20],gcf);
    

    您也可以制作 HTML 表格:

    str = ['<HTML><FONT color="red">Here is a table</Font>'...
           '<table><tr><th>1</th><th>2</th><th>3</th></tr>'...
           '<tr><th>4</th><th>5</th><th>6</th></tr></html>'];  
    jLabel = javaObjectEDT('javax.swing.JLabel',str);
    [hcomponent,hcontainer] = javacomponent(jLabel,[100,200,150,250],gcf);
    

    您可以在 Matlab here 中阅读有关 jLabel 组件的更多信息,以及有关 HTML here 的更多信息。归功于 Yair Altman 的博客。

    【讨论】:

      猜你喜欢
      • 2015-01-10
      • 1970-01-01
      • 1970-01-01
      • 2012-10-25
      • 1970-01-01
      • 2012-06-16
      • 2012-09-08
      • 1970-01-01
      • 2010-10-30
      相关资源
      最近更新 更多