如何在 CLI 上打印下标/上标?
我正在编写一段处理数学变量和索引的代码,我需要在 CLI 上打印下标和上标,有没有一种(可能是跨平台的)方法来做到这一点? 我正在使用普通 C++ 工作。
注意:我希望这是跨平台的,但从第一个答案来看,这似乎不可能,我在 MacOS 和 Ubuntu Linux(所以 bash)下工作。
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于大多数 CLI 实际上只是终端(大多数都是相当愚蠢的终端,但有时带有颜色),因此我完成此操作的唯一跨平台方法是为每条虚拟线路分配多个物理线路,例如:
这并不理想,但可能是没有 GUI 的情况下你能得到的最好结果。
下面是关于您主要感兴趣的平台的额外信息:
至少对于 Ubuntu,gnome-terminal 默认以 UTF-8 模式运行,因此以下代码显示如何生成上标和下标:
super< /code> 和
sub
char* 数组是数字上标和下标的 Unicode 代码点的 UTF-8 编码(请参阅 此处)。 给定的程序将从上面输出我的公式(一行而不是三行),然后是所有选择的另一测试行以及 y-super-999 和 z-sub-75,这样您就可以看到它们的样子。MacOS 似乎没有使用 gnome-terminal 作为终端程序,但引用了此处 和 < a href="http://www.macosxhints.com/article.php?story=20050208053951714" rel="noreferrer">这里似乎表明标准终端理解UTF-8(或者你可以下载并安装gnome-terminal 作为最后的手段)。
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:
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:
The
super
andsub
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).
前提是您有一个支持 Unicode 的终端,但这远不能保证。 Unicode 定义了有限数量的下标和上标“兼容字符”,您当然不能在任何旧字母上使用它:
即使这样,您也依赖于控制台字体中存在它的字形,这也远非如此。保证。 上标 2 和 3 很可能存在,因为它们存在于 ISO-8859-1 中; 其他的可能行不通。
Only if you have a Unicode-capable terminal, which is far from guaranteed. Unicode defines a limited number of sub- and superscript ‘compatibility characters’, you certainly can't use it on any old letter:
Even then you're reliant on there being a glyph for it in the console font, which is also far from guaranteed. Superscript 2 and 3 are likely to exist as they're present in ISO-8859-1; the others may well not work.