部分更改文本框中文本的颜色

问题描述 投票:3回答:2

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

该表是使用表格和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);
matlab colors textbox latex matlab-figure
2个回答
5
投票

您可以欺骗并使用支持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以及HTML here中阅读有关jLabel组件的更多信息。归功于Yair Altman的博客。


4
投票

您遇到的问题是仅当Interpreter属性设置为'tex'时才支持文本着色,但仅当解释器设置为tabular environment时才支持'latex'。你最好的解决方法可能是使用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);

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.