我喜欢在输入中使用 Unicode 字符。它们可以让文本看起来更像打印版本,并且占用更少的窗口空间。不过有一个小问题,我无法正确输入上标(或下标)。总是存在双重上标问题。
我寻求的是一个宏,例如\SUP
,这样 $X\SUP i \SUP + \SUP 1$ 就可以重写为 $X^{i+1}$
这是一个 MWE:“”
\documentclass{article}
\usepackage[utf8]{inputenc}
\def\SUP{^}
\DeclareUnicodeCharacter{00B2}{\SUP2}\DeclareUnicodeCharacter{00B3}{\SUP3}
\begin{document}
I can ask for $a$ squared plus $b$ squared to be equal to $c$ squared,
\[a²+b²=c²,\]
and I can also ask for $a$ cubed plus $b$ cubed to be equal to $c$ cubed,
\[a³+b³= c³.\]
But, I cannot ask for $a$ raised to the twenty-third degree plus $b$ raised
raised to the twenty-third degree to be equal to $c$ raised to the twenty-third
degree, \[ a²³ + b²³ = b²³. \]
\end{document}
然而,错误信息很烦人
<argument> ^
3
l.11 degree, \[ a²³
+ b²³ = b²³. \]
I treat `x^1^2' essentially like `x^1{}^2'.
答案1
宏\SUP
应该定义:
\def\SUP#1{\def\SUPt{#1}\futurelet\next\SUPa}
\def\SUPa{\ifx\next\SUP^\bgroup\SUPt\let\SUP=\SUPb\else^{\SUPt}\fi}
\def\SUPb#1{#1\futurelet\next\SUPc}
\def\SUPc{\ifx\next\SUP\else\egroup\fi}
%test:
$a\SUP2+b\SUP2=c\SUP2$
$a\SUP1\SUP2\SUP3$
\bye
答案2
这适用于 XeLaTeX 或 LuaLaTeX:
\documentclass{article}
\usepackage{newunicodechar}
\ExplSyntaxOn
\cs_new_protected:Nn \yossi_sup:
{
\peek_regex_replace_once:nn
{ ([¹²³⁴⁵⁶⁷⁸⁹⁰]*) }
{ \c{sp}\cB\{\c{yossi_replace:n}\cB\{\1\cE\}\cE\} }
}
\cs_new_protected:Nn \yossi_replace:n
{
\tl_map_function:nN { #1 } \__yossi_replace:n
}
\cs_new_protected:Nn \__yossi_replace:n
{
\str_case:nn { #1 } { {¹}{1}{²}{2}{³}{3}{⁴}{4}{⁵}{5}{⁶}{6}{⁷}{7}{⁸}{8}{⁹}{9}{⁰}{0} }
}
\newunicodechar{¹}{\yossi_sup: ¹}
\newunicodechar{²}{\yossi_sup: ²}
\newunicodechar{³}{\yossi_sup: ³}
\newunicodechar{⁴}{\yossi_sup: ⁴}
\newunicodechar{⁵}{\yossi_sup: ⁵}
\newunicodechar{⁶}{\yossi_sup: ⁶}
\newunicodechar{⁷}{\yossi_sup: ⁷}
\newunicodechar{⁸}{\yossi_sup: ⁸}
\newunicodechar{⁹}{\yossi_sup: ⁹}
\newunicodechar{⁰}{\yossi_sup: ⁰}
\ExplSyntaxOff
\begin{document}
$a²³ + b²³ = b²³$
$a¹²³⁴⁵⁶⁷⁸⁹⁰$
\end{document}
每个上标数字都有一个以 开头的定义。这将开始工作:收集\yossi_sup:
列表中的每个后续字符(包括起始字符),直到列表中没有其他字符。之后,将所有内容放入其中,并用标准对应字符逐一替换字符。¹²³⁴⁵⁶⁷⁸⁹⁰
^{...}
它可能对于 也是可行的pdflatex
,但主要问题是每个上标数字都是两个或三个字节长,最终的替换会复杂得多。
答案3
与 wipet 类似,但增加了额外的扩展,因此\SUP
可以隐藏在 unicode 字符的扩展后面。
\documentclass{article}
%\usepackage[utf8]{inputenc}% only needed in old latex releases
\def\SUP#1{^\bgroup
#1\expandafter\futurelet\expandafter\next\expandafter\SUPb
\romannumeral`\^^@}
\def\SUPb{\ifx\next^\expandafter\SUPc\else\egroup\fi}
\def\SUPc#1#2{}
\DeclareUnicodeCharacter{00B2}{\SUP2}\DeclareUnicodeCharacter{00B3}{\SUP3}
\begin{document}
I can ask for $a$ squared plus $b$ squared to be equal to $c$ squared,
\[a²+b²=c²,\]
and I can also ask for $a$ cubed plus $b$ cubed to be equal to $c$ cubed,
\[a³+b³= c³.\]
But, I cannot ask for $a$ raised to the twenty-third degree plus $b$ raised
raised to the twenty-third degree to be equal to $c$ raised to the twenty-third
degree, \[ a²³ + b²³ = b²³. \]
\end{document}
答案4
好吧,这基本上是复制粘贴的大卫·卡莱尔的回答--- 我刚刚浏览过https://unicode-table.com/en/blocks/superscripts-and-subscripts/,修改了(我希望是正确的)\romannumeral
黑魔法并将其扩展为下标。
我不敢编辑大卫的答案......;-)
这并不能保证它不会吃掉你的小猫——我真的不知道黑魔法的部分格罗克。
\documentclass{article}
%
% Shamelessly copied from @David Carlisle
% See https://tex.stackexchange.com/a/586136/38080
%
%\usepackage[utf8]{inputenc}% only needed in old latex releases
\def\SUP#1{^\bgroup
#1\expandafter\futurelet\expandafter\next\expandafter\SUPb
\romannumeral`\^^@}
\def\SUPb{\ifx\next^\expandafter\SUPc\else\egroup\fi}
\def\SUPc#1#2{}
\def\SUB#1{_\bgroup
#1\expandafter\futurelet\expandafter\next\expandafter\SUBb
\romannumeral`\^^@}
\def\SUBb{\ifx\next_\expandafter\SUBc\else\egroup\fi}
\def\SUBc#1#2{}
\DeclareUnicodeCharacter{2070}{\SUP0}
\DeclareUnicodeCharacter{00B9}{\SUP1}
\DeclareUnicodeCharacter{00B2}{\SUP2}
\DeclareUnicodeCharacter{00B3}{\SUP3}
\DeclareUnicodeCharacter{2074}{\SUP4}
\DeclareUnicodeCharacter{2075}{\SUP5}
\DeclareUnicodeCharacter{2076}{\SUP6}
\DeclareUnicodeCharacter{2077}{\SUP7}
\DeclareUnicodeCharacter{2078}{\SUP8}
\DeclareUnicodeCharacter{2079}{\SUP9}
\DeclareUnicodeCharacter{2071}{\SUP{i}}
\DeclareUnicodeCharacter{207A}{\SUP{{+}}}
\DeclareUnicodeCharacter{207B}{\SUP{{-}}}
\DeclareUnicodeCharacter{207C}{\SUP{{=}}}
\DeclareUnicodeCharacter{207D}{\SUP{(}}
\DeclareUnicodeCharacter{207E}{\SUP{)}}
\DeclareUnicodeCharacter{207F}{\SUP{n}}
\DeclareUnicodeCharacter{2080}{\SUB0}
\DeclareUnicodeCharacter{2081}{\SUB1}
\DeclareUnicodeCharacter{2082}{\SUB2}
\DeclareUnicodeCharacter{2083}{\SUB3}
\DeclareUnicodeCharacter{2084}{\SUB4}
\DeclareUnicodeCharacter{2085}{\SUB5}
\DeclareUnicodeCharacter{2086}{\SUB6}
\DeclareUnicodeCharacter{2087}{\SUB7}
\DeclareUnicodeCharacter{2088}{\SUB8}
\DeclareUnicodeCharacter{2089}{\SUB9}
\DeclareUnicodeCharacter{208A}{\SUB{{+}}}
\DeclareUnicodeCharacter{208B}{\SUB{{-}}}
\DeclareUnicodeCharacter{208C}{\SUB{{=}}}
\DeclareUnicodeCharacter{208D}{\SUB{(}}
\DeclareUnicodeCharacter{208E}{\SUB{)}}
\DeclareUnicodeCharacter{2090}{\SUB{a}}
\DeclareUnicodeCharacter{2091}{\SUB{e}}
\DeclareUnicodeCharacter{2092}{\SUB{o}}
\DeclareUnicodeCharacter{2093}{\SUB{x}}
\begin{document}
I can ask for $a$ squared plus $b$ squared to be equal to $c$ squared,
\[a²+b²=c²,\]
and I can also ask for $a$ cubed plus $b$ cubed to be equal to $c$ cubed,
\[a³+b³= c³.\]
But, I cannot ask for $a$ raised to the twenty-third degree plus $b$ raised
raised to the twenty-third degree to be equal to $c$ raised to the twenty-third
degree, \[ a²³ + b²³ = b²³. \]
\[ x¹²³⁴⁵⁶⁷⁸⁹⁰pⁱⁿv⁺⁻z⁼k⁽¹⁾ \]
\[a¹₀₁₍₃₊₎\]
\end{document}
(再次对糟糕的抗锯齿效果表示抱歉)