How to print indexes / superscripts in CLI?

I am writing a piece of code that focuses on mathematical variables and indexes, and I will need to print indexes and superscripts in the CLI, is there a (possibly cross-platform) way to do this? I work in vanilla C ++.

Note. . I would like it to be cross-platform, but since this is not possible from the first answers, I work under MacOS and Ubuntu Linux (so bash).

thanks

+2
source share
2 answers

Since most CLIs are really just terminals (rather dumb, but sometimes with color), the only cross-platform way I've ever done is to allocate multi-level physical lines for each virtual line, for example:

2 f(x) = x + log x 2 

This is not ideal, but it is probably the best you get without a GUI.

After receiving more information on which platforms you are mainly interested in:

With Ubuntu, at least gnome-terminal starts in UTF-8 mode by default, so the following code shows how to create superscript indexes and indexes:

 #include <stdio.h> static char *super[] = {"\xe2\x81\xb0", "\xc2\xb9", "\xc2\xb2", "\xc2\xb3", "\xe2\x81\xb4", "\xe2\x81\xb5", "\xe2\x81\xb6", "\xe2\x81\xb7", "\xe2\x81\xb8", "\xe2\x81\xb9"}; static char *sub[] = {"\xe2\x82\x80", "\xe2\x82\x81", "\xe2\x82\x82", "\xe2\x82\x83", "\xe2\x82\x84", "\xe2\x82\x85", "\xe2\x82\x86", "\xe2\x82\x87", "\xe2\x82\x88", "\xe2\x82\x89"}; int main(void) { int i; printf ("f(x) = x%s + log%sx\n",super[2],sub[2]); for (i = 0; i < 10; i++) { printf ("x%sx%s ", super[i], sub[i]); } printf ("y%s%s%sz%s%s\n", super[9], super[9], super[9], sub[7], sub[5]); return 0; } 

The super and sub char * arrays are UTF-8 encodings for Unicode code points for numeric superscripts and indexes (see here ). This program will display my formula on top (on one line instead of three), then on another test line for all options and y-super-999 and z-sub-75, so you can see how they look.

MacOS doesn't seem to use gnome-terminal as a terminal program, but the link here and here seems to indicate that the standard terminal understands UTF-8 (or you can download and install gnome-terminal as a last resort).

+8
source

I need to print indexes and superscripts in the CLI, is there a cross-platform way to do this?

Only if you have a terminal with Unicode support, which is far from guaranteed. Unicode defines a limited number of compatibility characters for sub- and superscript characters, of course you cannot use it on any old letter:

 β‚€β‚β‚‚β‚ƒβ‚„β‚…β‚†β‚‡β‚ˆβ‚‰β‚Šβ‚‹β‚Œβ‚β‚Žβ‚β‚‘β‚’β‚“ ⁰¹²³⁴⁡⁢⁷⁸⁹⁺⁻⁼⁽⁾ⁿⁱ 

Even then, you rely on the presence of a character in the console font, which is also far from guaranteed. Superscript 2 and 3 are likely to exist as they are present in ISO-8859-1; others may not work.

+4
source

Source: https://habr.com/ru/post/904576/


All Articles