Xaringan会渲染一个具有class.source选项定义的代码块。如何解决它?

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

Xaringan does render a chunk with chunk option class.source defined. How to solve it?

问题

I am using xaringan to create slides with stan code. I want to present the model code and save it using the syntax below. However, when I add the class.source = "stan" (to highlight the stan syntax appropriately) option the code does not render as it should:

---
output: xaringan::moon_reader
---

---

```{cat, engine.opts = list(file = 'code/stan/ex1.stan'), name = "ex1.stan", class.source = "stan"}
data {
    int<lower=0> N; //number of data
    vector[N] x; //covariates
    vector[N] y; //variates
}

parameters {
  real alpha; //intercept
  real beta; //slope
  real<lower=0> sigma; //scatter
}

model {
  //priors
  alpha ~ normal(0, 10);
  beta ~ normal(0, 10);
  sigma ~ normal(0, 1);
  
  y ~ normal(alpha + beta * x, sigma); //likelihood
}

Xaringan会渲染一个具有class.source选项定义的代码块。如何解决它?

So the desired result would be:

Xaringan会渲染一个具有class.source选项定义的代码块。如何解决它?


[1]: https://i.stack.imgur.com/o6wNv.png
[2]: https://i.stack.imgur.com/LiV8z.png

<details>
<summary>英文:</summary>

I am using `xaringan` to create slides with stan code. I want to present the model code and save it using the syntax below. However, when I add the `class.source = &quot;stan&quot;` (to highlight the stan syntax appropriately) option the code does not render as it should:


```r
---
output:   xaringan::moon_reader
---

---

```{cat, engine.opts = list(file = &#39;code/stan/ex1.stan&#39;), name = &quot;ex1.stan&quot;, class.source = &quot;stan&quot;}
data {
    int&lt;lower=0&gt; N; //number of data
    vector[N] x; //covariates
    vector[N] y; //variates
}

parameters {
  real alpha; //intercept
  real beta; //slope
  real&lt;lower=0&gt; sigma; //scatter
}

model {
  //priors
  alpha ~ normal(0, 10);
  beta ~ normal(0, 10);
  sigma ~ normal(0, 1);
  
  y ~normal(alpha + beta * x, sigma); //likelihood
}

Xaringan会渲染一个具有class.source选项定义的代码块。如何解决它?

So the desired result would be:

Xaringan会渲染一个具有class.source选项定义的代码块。如何解决它?

答案1

得分: 2

根据您在上面的评论中的理解,您只需要以下内容的翻译:

---
output:   xaringan::moon_reader
---

---
```{cat, engine.opts = list(file = "test.stan", lang = "stan")}
data {
    int<lower=0> N; //数据数量
    vector[N] x; //协变量
    vector[N] y; //变量
}

parameters {
  real alpha; //截距
  real beta; //斜率
  real<lower=0> sigma; //散射
}

model {
  //先验分布
  alpha ~ normal(0, 10);
  beta ~ normal(0, 10);
  sigma ~ normal(0, 1);
  
  y ~ normal(alpha + beta * x, sigma); //似然
}

<details>
<summary>英文:</summary>

As I understand you in comments above, you simply need this:

    ---
    output:   xaringan::moon_reader
    ---
    
    ---
    ```{cat, engine.opts = list(file = &quot;test.stan&quot;, lang = &quot;stan&quot;)}
    data {
        int&lt;lower=0&gt; N; //number of data
        vector[N] x; //covariates
        vector[N] y; //variates
    }
    
    parameters {
      real alpha; //intercept
      real beta; //slope
      real&lt;lower=0&gt; sigma; //scatter
    }
    
    model {
      //priors
      alpha ~ normal(0, 10);
      beta ~ normal(0, 10);
      sigma ~ normal(0, 1);
      
      y ~normal(alpha + beta * x, sigma); //likelihood
    }
    ```

</details>



huangapple
  • 本文由 发表于 2023年6月12日 08:06:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76452981.html
匿名

发表评论

匿名网友

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

确定