gpt4 book ai didi

r - 使用 MathJax 在 Shiny 的应用程序中内联 LaTeX 方程

转载 作者:行者123 更新时间:2023-12-04 11:39:57 25 4
gpt4 key购买 nike

显示 LaTeX 方程的解决方案 在线提供 hereworking live demo

上面提到的在线显示方程的解决方案是(减少几行代码):

用户界面:

library(shiny)

shinyUI(fluidPage(
title = 'MathJax Examples with in-line equations',
withMathJax(),
# section below allows in-line LaTeX via $ in mathjax. Replace less-than-sign with <
# and grater-than-sign with >
tags$div(HTML("less-than-sign script type='text/x-mathjax-config' greater-than-sign
MathJax.Hub.Config({
tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}
});
less-than-sign /script greater-than-sign
")),
helpText('An irrational number $\\sqrt{2}$
and a fraction $1-\\frac{1}{2}$'),
helpText('and a fact about $\\pi$:$\\frac2\\pi = \\frac{\\sqrt2}2 \\cdot
\\frac{\\sqrt{2+\\sqrt2}}2 \\cdot
\\frac{\\sqrt{2+\\sqrt{2+\\sqrt2}}}2 \\cdots$'),
uiOutput('ex1')
))

服务器.R:
shinyServer(function(input, output, session) {
output$ex1 <- renderUI({
withMathJax(helpText('Dynamic output 1: $\\alpha^2$'))
})
})

我在 macOS 上运行 R 版本 3.5.2, Shiny 的 1.2。应用程序输出是一些逐字文本,而不是预期的文本和内联方程的混合:
less-than-sign script type='text/x-mathjax-config' greater-than-sign MathJax.Hub.Config({ tex2jax: {inlineMath: [['$','$'], ['′,′
']]} }); less-than-sign /script greater-than-sign
and a fact about $\pi$:$\frac2\pi = \frac{\sqrt2}2 \cdot \frac{\sqrt{2+\sqrt2}}2 \cdot \frac{\sqrt{2+\sqrt{2+\sqrt2}}}2 \cdots$
Dynamic output 1: $\alpha^2$

可能是因为我的 R 环境?

最佳答案

另一种方法,使用 KaTeX 而不是 MathJax:

library(shiny)

ui <- fluidPage(
tags$head(
tags$link(rel="stylesheet",
href="https://cdn.jsdelivr.net/npm/katex@0.10.1/dist/katex.min.css",
integrity="sha384-dbVIfZGuN1Yq7/1Ocstc1lUEm+AT+/rCkibIcC/OmWo5f0EA48Vf8CytHzGrSwbQ",
crossorigin="anonymous"),
HTML('<script defer src="https://cdn.jsdelivr.net/npm/katex@0.10.1/dist/katex.min.js" integrity="sha384-2BKqo+exmr9su6dir+qCw08N2ZKRucY4PrGQPPWU1A7FtlCGjmEGFqXCv5nyM5Ij" crossorigin="anonymous"></script>'),
HTML('<script defer src="https://cdn.jsdelivr.net/npm/katex@0.10.1/dist/contrib/auto-render.min.js" integrity="sha384-kWPLUVMOks5AQFrykwIup5lo0m3iMkkHrD0uJ4H5cjeGihAutqP0yW0J6dpFiVkI" crossorigin="anonymous"></script>'),
HTML('
<script>
document.addEventListener("DOMContentLoaded", function(){
renderMathInElement(document.body, {
delimiters: [{left: "$", right: "$", display: false}]
});
})
</script>')
),
titlePanel("Hello Shiny!"),
helpText('An irrational number $\\sqrt{2}$
and a fraction $1-\\frac{1}{2}$'),
helpText('and a fact about $\\pi$:$\\frac2\\pi = \\frac{\\sqrt2}2 \\cdot
\\frac{\\sqrt{2+\\sqrt2}}2 \\cdot
\\frac{\\sqrt{2+\\sqrt{2+\\sqrt2}}}2 \\cdots$'),
uiOutput('ex1')
)

server <- function(input, output) {

output$ex1 <- renderUI({
tagList(
helpText('Dynamic output 1: $\\alpha^2$'),
tags$script('renderMathInElement(document.getElementById("ex1"), {delimiters: [{left: "$", right: "$", display: false}]});')
)
})

}

enter image description here

关于r - 使用 MathJax 在 Shiny 的应用程序中内联 LaTeX 方程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54876731/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com