- Undocumented Matlab - https://undocumentedmatlab.com -

Bold color text in the Command Window

Posted By Yair Altman On August 8, 2012 | 2 Comments

A reader comment [1] last week sent me on a short wild goose chase, that had an interesting and useful conclusion. The comment requested a way to display bold text in the Command Window, similarly to the way that we can display colored and underlined text using my cprintf utility [2].
Since Matlab release 7.13 (R2011b), the Matlab Command Window (CW) has the ability to display bold text. This is used, for example, when displaying the function name in the output of the help function (i.e., help(‘max’) will display the term ‘max’ in bold when displaying the help section). It therefore seemed a reasonable request to add this functionality to cprintf.

Attempt #1: use the underlying Java

I stated out investigating the relevant attributes used by the CW’s underlying Java component to display the text. It turns out that in addition to the three custom attributes used by Matlab in past releases, a fourth attribute called BoldStartTokens was added, which stored the relative position index of the bold text within each text element.
Unfortunately, this attribute contains an array of integer values, that get converted to a simple numeric array in Matlab. This means that unlike the SyntaxTokens attribute, which contains an array of Java String objects that can be modified one at a time (this is used by cprintf), the BoldStartTokens attribute value cannot be modified in a similar manner. Attempting to remove and add the attribute afresh (with modified values) failed due to a javax.swing.text.StateInvariantError: Illegal cast to MutableAttributeSet Java exception.
So unless I find a way to update the BoldStartTokens attribute values from Matlab, this looked like a dead end.

Attempt #2: follow the Matlab trail

My second attempt was to follow the Matlab trail, to see exactly what the help function does internally to format parts of the text as bold. Starting with help‘s m-file (%matlabroot%/toolbox/matlab/helptools/help.m), and step-by-step debugging, I quickly found out that the internal function prepareHelpForDisplay parses the help text and wraps the relevant terms with an HTML <strong> tag to make these terms appear bold.
We can easily use this information ourselves:

>> fprintf('not bold,  bold \n')
not bold, bold     % the 'bold' term appears bolded

Matlab does have an internal limitation, of not allowing hyperlinks and bold formatting together. If both <a> and <strong> tags are present, then only the inner tag set is parsed.

Updating cprintf

Using <strong> is great, but apparently if we use the <strong> tag in cprintf, the output gets messed up – the HTML tag confuses cprintf. So I took the opportunity to update cprintf such that (1) it will not get confused by a <strong> tag, and (2) so that we could use ‘*’ in the style part (input argument #1) to specify bold format:

>> cprintf('*blue', 'this is bold\n')
this is bold     % this text appears bolded-blue

cprintf demo
cprintf demo


The benefit of using cprintf over directly using the <strong> tag is that it works seamlessly on Matlab release 2011a and earlier that do not support the tag (the text is simply not bolded, whereas if we had used <strong> this tag would be displayed onscreen, which is ugly). Moreover, cprintf catches a situation of trying to use both underline and bold formats, and issues a warning. Finally, of course, ‘*’ is more compact and easier to use and maintain than ‘<strong>…</strong>’ …
Please feel free to download [3] cprintf from the File Exchange and read its contents to see how a not-too-large utility can achieve something previously believed to be impossible. And if you find ways to make cprintf better, please do let me know (I don’t mean suggestions for *me* to work on: I mean actual improvements that *you* made to the code).

Categories: Desktop, High risk of breaking in future versions, Java, Undocumented feature


2 Comments (Open | Close)

2 Comments To "Bold color text in the Command Window"

#1 Comment By Aurélien Queffurust On August 9, 2012 @ 00:50

Hi Yair,

Nice article as usual. I had also noticed in Sept. 2011 the new bold syntax for the help
FYI you can read the answer of Scott Hirsch on my blog :
[10]

It is important to say that using the s t r o n g> HTML tag as in your example with fprintf will only work in MATLAB environment.
In a standalone mode , the DOS command window will display the HTML tags … so my advise would be to use this feature only if the MATLAB code is not expected to be compiled .

Have a nice day,

Aurélien

#2 Pingback By Another Command Window text color hack | Undocumented Matlab On January 1, 2014 @ 02:42

[…] using my cprintf utility. I followed this up last year with the relatively new ability to display bold text formatting, using either fprintf or cprintf.A short while ago, Carsten Allefeld (a regular on this blog) […]

#3 Comment By Khaled On October 30, 2019 @ 22:04

Hello,
I found out how Matlab displays bold hyperlinks. It does so by adding an inline style (style=”font-weight:bold”) inside the <a> tag. For example:

fprintf('\n\t [11]\n\n');

Article printed from Undocumented Matlab: https://undocumentedmatlab.com

URL to article: https://undocumentedmatlab.com/articles/bold-color-text-in-the-command-window

URLs in this post:

[1] comment: http://undocumentedmatlab.com/blog/cprintf-display-formatted-color-text-in-command-window/#comment-101312

[2] cprintf utility: http://undocumentedmatlab.com/blog/cprintf-display-formatted-color-text-in-command-window/

[3] download: http://www.mathworks.com/matlabcentral/fileexchange/24093-cprintf

[4] cprintf – display formatted color text in the Command Window : https://undocumentedmatlab.com/articles/cprintf-display-formatted-color-text-in-command-window

[5] Another Command Window text color hack : https://undocumentedmatlab.com/articles/another-command-window-text-color-hack

[6] Command Window text manipulation : https://undocumentedmatlab.com/articles/command-window-text-manipulation

[7] Changing Matlab's Command Window colors : https://undocumentedmatlab.com/articles/changing-matlab-command-window-colors

[8] EditorMacro v2 – setting Command Window key-bindings : https://undocumentedmatlab.com/articles/editormacro-v2-setting-command-window-key-bindings

[9] Changing Matlab's Command Window colors – part 2 : https://undocumentedmatlab.com/articles/changing-matlab-command-window-colors-part2

[10] : http://blog.developpez.com/matlab4geek/p10298/r2011b/startup-accelerator-et-help-en-gras/?page=2

[11] : http://undocumentedmatlab.commatlab:doc sin

Copyright © Yair Altman - Undocumented Matlab. All rights reserved.