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

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

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:

  1. ---
  2. output: xaringan::moon_reader
  3. ---
  4. ---
  5. ```{cat, engine.opts = list(file = 'code/stan/ex1.stan'), name = "ex1.stan", class.source = "stan"}
  6. data {
  7. int<lower=0> N; //number of data
  8. vector[N] x; //covariates
  9. vector[N] y; //variates
  10. }
  11. parameters {
  12. real alpha; //intercept
  13. real beta; //slope
  14. real<lower=0> sigma; //scatter
  15. }
  16. model {
  17. //priors
  18. alpha ~ normal(0, 10);
  19. beta ~ normal(0, 10);
  20. sigma ~ normal(0, 1);
  21. y ~ normal(alpha + beta * x, sigma); //likelihood
  22. }

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

So the desired result would be:

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

  1. [1]: https://i.stack.imgur.com/o6wNv.png
  2. [2]: https://i.stack.imgur.com/LiV8z.png
  3. <details>
  4. <summary>英文:</summary>
  5. 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:
  6. ```r
  7. ---
  8. output: xaringan::moon_reader
  9. ---
  10. ---
  11. ```{cat, engine.opts = list(file = &#39;code/stan/ex1.stan&#39;), name = &quot;ex1.stan&quot;, class.source = &quot;stan&quot;}
  12. data {
  13. int&lt;lower=0&gt; N; //number of data
  14. vector[N] x; //covariates
  15. vector[N] y; //variates
  16. }
  17. parameters {
  18. real alpha; //intercept
  19. real beta; //slope
  20. real&lt;lower=0&gt; sigma; //scatter
  21. }
  22. model {
  23. //priors
  24. alpha ~ normal(0, 10);
  25. beta ~ normal(0, 10);
  26. sigma ~ normal(0, 1);
  27. y ~normal(alpha + beta * x, sigma); //likelihood
  28. }

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

So the desired result would be:

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

答案1

得分: 2

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

  1. ---
  2. output: xaringan::moon_reader
  3. ---
  4. ---
  5. ```{cat, engine.opts = list(file = "test.stan", lang = "stan")}
  6. data {
  7. int<lower=0> N; //数据数量
  8. vector[N] x; //协变量
  9. vector[N] y; //变量
  10. }
  11. parameters {
  12. real alpha; //截距
  13. real beta; //斜率
  14. real<lower=0> sigma; //散射
  15. }
  16. model {
  17. //先验分布
  18. alpha ~ normal(0, 10);
  19. beta ~ normal(0, 10);
  20. sigma ~ normal(0, 1);
  21. y ~ normal(alpha + beta * x, sigma); //似然
  22. }
  1. <details>
  2. <summary>英文:</summary>
  3. As I understand you in comments above, you simply need this:
  4. ---
  5. output: xaringan::moon_reader
  6. ---
  7. ---
  8. ```{cat, engine.opts = list(file = &quot;test.stan&quot;, lang = &quot;stan&quot;)}
  9. data {
  10. int&lt;lower=0&gt; N; //number of data
  11. vector[N] x; //covariates
  12. vector[N] y; //variates
  13. }
  14. parameters {
  15. real alpha; //intercept
  16. real beta; //slope
  17. real&lt;lower=0&gt; sigma; //scatter
  18. }
  19. model {
  20. //priors
  21. alpha ~ normal(0, 10);
  22. beta ~ normal(0, 10);
  23. sigma ~ normal(0, 1);
  24. y ~normal(alpha + beta * x, sigma); //likelihood
  25. }
  26. ```
  27. </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:

确定