如何在CLI上打印脚注/上标? [英] How to print subscripts/superscripts on a CLI?

查看:169
本文介绍了如何在CLI上打印脚注/上标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一段处理数学变量和索引的代码,我需要在CLI上打印下标和上标,是否有一个(可能是跨平台的)方法?我在使用香草C ++。



注意:我希望这是跨平台,但从第一个答案似乎不可能我在MacOS和Ubuntu Linux(所以bash)下工作。



谢谢

  2 
f(x)= x + log x
2

这不太理想,但它可能是没有GUI的情况下最好的。



关于您主要感兴趣的平台的额外信息:



至少使用Ubuntu,gnome-默认情况下为UTF-8模式,所以下面的代码显示如何生成上标和下标:

  #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 \\\
,super [2],sub [2]
for(i = 0; i <10; i ++){
printf(x%s x%s,super [i],sub [i]);
}
printf(y%s%s%sz%s%s\\\
,super [9],super [9],super [9],sub [7] 5]);
return 0;
}

super sub char *数组是数字上标和下标的Unicode代码点的UTF-8编码(参见这里)。给定的程序将从上面(一行而不是三行)输出我的公式,然后输出所有选择的另一个测试行,y-super-999和z-sub-75,这样你可以看到它们的样子。 p>

MacOS似乎不使用gnome-terminal作为终端程序,但引用此处此处似乎表明标准终端了解UTF-8(或者您可以下载并安装gnome-terminal作为最后的手段)。


I'm writing a piece of code which deals with math variables and indices, and I'd need to print subscripts and superscripts on a CLI, is there a (possibly cross-platform) way to do that? I'm working in vanilla C++.

Note: I'd like this to be cross-platform, but since from the first answers this doesn't seem to be possible I'm working under MacOS and Ubuntu Linux (so bash).

Thank you

解决方案

Since most CLIs are really only terminals (pretty dumb ones mostly but sometimes with color), the only cross-platform way I've ever done this is by allocating muliple physical lines per virtual line, such as:

        2
f(x) = x  + log x
               2

It's not ideal but it's probably the best you're going to get without a GUI.

Following you extra information as to what platforms you're mainly interested in:

With Ubuntu at least, gnome-terminal runs in UTF-8 mode by default so the following code shows how to generate the superscripts and subscripts:

#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%s x%s ", super[i], sub[i]);
    }
    printf ("y%s%s%s z%s%s\n", super[9], super[9], super[9], sub[7], sub[5]);
    return 0;
}

The super and sub char* arrays are the UTF-8 encodings for the Unicode code points for numeric superscripts and subscripts (see here). The given program will output my formula from above (on one line instead of three), then another test line for all the choices and a y-super-999 and z-sub-75 so you can see what they look like.

MacOS doesn't appear to use gnome-terminal as a terminal program but references here and here seem to indicate the standard terminal understands UTF-8 (or you could download and install gnome-terminal as a last resort).

这篇关于如何在CLI上打印脚注/上标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆