英文:
How can I write double (Greek) indices using LaTeX
问题
我正在尝试编写一组函数,使用SymPy可以打印带有上标和两个(或更多)下标的符号;但是,我一直在努力解决一个问题。
这是一个最小示例:
import sympy as smp
from IPython.display import display, Latex
smp.init_printing()
t, r, theta, phi = smp.symbols(r't, r, \theta, \phi')
display(Latex(smp.latex(fr'$\Lambda^{r}_{t}{t}$')))
在Jupyter Notebook环境中打印如下:
然而,我希望t
也成为一个下标,因此Lambda
符号将具有两个下标而不是一个。
当我尝试使用theta和phi符号作为下标时,会出现另一个相关的问题:
display(Latex(smp.latex(fr'$\Lambda^{phi}_{theta}{r}$')))
这会打印(同样,在Jupyter Notebook环境中):
为了解决这个问题,我不得不在符号之间加一个空格:
display(Latex(smp.latex(fr'$\Lambda^{phi}_{theta} {r}$')))
然而,不幸的是,这也会打印:
编辑:
我尝试了@geofisue的修复方法,它打印出以下内容:
英文:
I am attempting to write a set of functions that could print out symbols with an upper index and two (or more) lower indices using SymPy; however, I've been attempting to get past a problem.
Here's a minimal example:
import sympy as smp
from IPython.display import display, Latex
smp.init_printing()
t, r, theta, phi = smp.symbols(r't, r, \theta, \phi')
display(Latex(smp.latex(fr'$\Lambda^{r}_{t}{t}$')))
Prints this (In a Jupyter Notebook Environment):
However, I want the t
to be an index too, so the Lambda
symbol would have two lower indices instead of one.
Another related problem arises when I try using the theta and phi symbols as indices:
display(Latex(smp.latex(fr'$\Lambda^{phi}_{theta}{r}$')))
Which prints (Again, in a Jupyter Notebook Environment):
In order to fix this, I had to put a space between the symbols:
display(Latex(smp.latex(fr'$\Lambda^{phi}_{theta} {r}$')))
Which, unfortunately, also prints:
Edit:
I've attempted @geofisue's fix, and it prints this:
答案1
得分: 0
找到了解决方法!
我必须创建一个自定义类,继承自sympy
库的Indexed
和IndexedBase
类,并编辑它以满足我的需求。我的示例代码现在如下所示:
import sympy as smp
from IPython.display import display
smp.init_printing()
class CustomIndexed(smp.Indexed):
def _latex(self, printer):
return '%s^{%s}_{%s%s}' % (
self.base, *self.indices
)
class CustomIndexedBase(smp.IndexedBase):
def __getitem__(self, indices, **kwargs):
return CustomIndexed(self.name,
*indices)
t, r, theta, phi = smp.symbols('t, r, \\theta, \\phi')
Lambda = CustomIndexedBase('Λ')
display(Lambda[r, phi, theta])
这将显示正确的符号:
英文:
Found a fix!
I had to create a custom class inheriting from the Indexed
and IndexedBase
sympy classes, and edit it to fit my needs. My example code now looks like so:
import sympy as smp
from IPython.display import display
smp.init_printing()
class CustomIndexed(smp.Indexed):
def _latex(self, printer):
return '%s^{%s}_{{%s}{%s}}' % (
self.base, *self.indices
)
class CustomIndexedBase(smp.IndexedBase):
def __getitem__(self, indices, **kwargs):
return CustomIndexed(self.name,
*indices)
t, r, theta, phi = smp.symbols(r't, r, \theta, \phi')
Lambda = CustomIndexedBase(r'\Lambda')
display(Lambda[r, phi, theta])
Which displays the correct symbol:
答案2
得分: 0
你的方法存在问题的地方在于花括号。你有两个选项。
- 用双花括号包裹所有下标项。双花括号对于f-string来说是必要的,以便知道哪些花括号是用于替换的,哪些是最终字符串中保留为花括号的。你需要保留花括号,以便LaTeX知道这两个术语都在下标中。
- 使用双下标。这在技术上不是有效的LaTeX(如果你在Overleaf上尝试会出错),但似乎对sympy有效。
import sympy as sp
from IPython.display import display
t, r, theta, phi, Lambda = sp.symbols("t r theta phi Lambda")
symbol = sp.symbols(f"{Lambda}^{{r}}_{{{{phi}{theta}}}}") # 方法1
#symbol = sp.symbols(f"{Lambda}^{{r}}_{{phi}_{theta}}") # 方法2
display(symbol)
英文:
The issue with your methods is the curly brackets. You have two options.
- Wrap all the subscript terms in double curly brackets. The double curly brackets are necessary for the f-string to know which curly brackets are for replacement and which are to stay as curly brackets in the final string. You need the curly brackets to stay so that LaTeX knowns that both those terms are in the subscript.
- Use a double subscript. This isn't technically valid LaTeX (if you tried it on Overleaf you'd get an error), but it seems to work for sympy.
import sympy as sp
from IPython.display import display
t, r, theta, phi, Lambda = sp.symbols("t r \\theta \\phi \\Lambda")
symbol = sp.symbols(f"{Lambda}^{r}_{{{phi}{theta}}}") # method 1
#symbol = sp.symbols(f"{Lambda}^{r}_{phi}_{theta}") # method 2
display(symbol)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论