【问题标题】:sympy pretty print in Rmarkdown同情 Rmarkdown 中的漂亮打印
【发布时间】:2021-03-06 07:02:06
【问题描述】:

是否可以使用 reticulate 在 r-markdown 文档中的 python 块中包含来自 sympy 的漂亮格式的公式。如果我使用 init_printing 它将公式打印为 ascii 艺术,设置 use_latex="mathjax"use_latex=True 无效。

init_printing 或 python 块的块选项或 reticulate 选项是否有任何参数以获得漂亮的公式?

这是一个最小的例子:

---
title: "rmd_sympy_reprex"
author: "snaut"
date: "11/23/2020"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(reticulate)
use_python("/usr/bin/python3", required = TRUE)
```

```{python, include=FALSE}
from sympy import *
init_printing()
```

```{python}
omega = Symbol("omega")
x = Symbol("n", real=True)
Integral(x, (x,0,1))
```

【问题讨论】:

    标签: python r r-markdown sympy reticulate


    【解决方案1】:

    是的!您可以在这里找到解决方案:

    https://cerebralmastication.com/2020/07/27/using-python-sympy-in-a-python-code-chunk-in-rmarkdown/

    以下是可重现的示例:

    ```{r}
    
    library(reticulate)
    ```
    ```{python, results='asis'}
    
    import sympy
    
    x = sympy.Symbol('x')
    polynomial = (2*x + 3)**4
    output = polynomial.expand()
    print('$' + sympy.printing.latex(output) + '$')
    ```
    

    这个想法是使用sympy.printing.latex。然后在字符串的开头和结尾连接'$'。最后在 python 块中使用选项results='asis'

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-22
      • 1970-01-01
      • 2016-07-07
      • 2014-05-19
      • 1970-01-01
      • 2017-11-03
      • 2012-05-08
      相关资源
      最近更新 更多