如何使用LaTeX编写带有双重(希腊字母)索引

huangapple go评论86阅读模式
英文:

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环境中打印如下:

如何使用LaTeX编写带有双重(希腊字母)索引

然而,我希望t也成为一个下标,因此Lambda符号将具有两个下标而不是一个。

当我尝试使用theta和phi符号作为下标时,会出现另一个相关的问题:

display(Latex(smp.latex(fr'$\Lambda^{phi}_{theta}{r}$')))

这会打印(同样,在Jupyter Notebook环境中):

如何使用LaTeX编写带有双重(希腊字母)索引

为了解决这个问题,我不得不在符号之间加一个空格:

display(Latex(smp.latex(fr'$\Lambda^{phi}_{theta} {r}$')))

然而,不幸的是,这也会打印:

如何使用LaTeX编写带有双重(希腊字母)索引

编辑:

我尝试了@geofisue的修复方法,它打印出以下内容:

如何使用LaTeX编写带有双重(希腊字母)索引

英文:

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):

如何使用LaTeX编写带有双重(希腊字母)索引

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):

如何使用LaTeX编写带有双重(希腊字母)索引

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:

如何使用LaTeX编写带有双重(希腊字母)索引


Edit:

I've attempted @geofisue's fix, and it prints this:

如何使用LaTeX编写带有双重(希腊字母)索引

答案1

得分: 0

找到了解决方法!

我必须创建一个自定义类,继承自sympy库的IndexedIndexedBase类,并编辑它以满足我的需求。我的示例代码现在如下所示:

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])

这将显示正确的符号:

Indexed Symbol (Correct)

英文:

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:

Indexed Symbol (Correct)

答案2

得分: 0

你的方法存在问题的地方在于花括号。你有两个选项。

  1. 花括号包裹所有下标项。双花括号对于f-string来说是必要的,以便知道哪些花括号是用于替换的,哪些是最终字符串中保留为花括号的。你需要保留花括号,以便LaTeX知道这两个术语都在下标中。
  2. 使用双下标。这在技术上不是有效的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)

如何使用LaTeX编写带有双重(希腊字母)索引

英文:

The issue with your methods is the curly brackets. You have two options.

  1. 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.
  2. 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)

如何使用LaTeX编写带有双重(希腊字母)索引

huangapple
  • 本文由 发表于 2023年6月22日 15:41:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76529598.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定