Cmdstanr code chunk in Quarto not displaying correct color coding

Using {cmdstanr} v0.7.1 and cmdstan v2.34.1 and RStudio 2023.12.1 Build 402.

A code chunk in Quarto like the below will fail to properly color code the \ and **\ comments (it only recognizes the old style “#” comments. It also seems to get confused about the transpose tick to convert a row vector to vector. Finally, the shortcut for commenting multiple lines uses the old “#” comments. a {stan} code chunk seems to work properly but doesn’t support the commenting shortcut.

data {
int x; // comment

tranformed data {
vector y[2] = [1, 2]';

in your _quarto.yml file, do you have the syntax-definition attribute set?
in the Stan docs repo, this file works for me: docs/src/stan.xml at master · stan-dev/docs · GitHub

I’m seeing this problem in the RStudio editor. I added the .xml file you mentioned to my _quarto.yml file, but that didn’t make a difference. I think this xml file only matters for the output of Quarto, right?

I’m sorry, I don’t understand the problem.

  • where do you want the syntax highlighting to show up?
  • are you using the RStudio file editor to write a Stan program?
  • are you using the RStudio file editor to write documentation in quarto markdown format?

Sorry I might not have been very clear. I’m editing a Stan code chunk in a .qmd file (not what I typically do, but I wanted to put something concise as an example for something). Here’s a screenshot of what I’m talking about. Notice the correct syntax coloring in RStudio and the difference between the stan and cmdstan chunks?

My guess is “cmdstan” is being recognized as something like “cmd”, the Windows batch scripting language?

Is there a reason you want to use “cmdstan” instead of “stan” as the block’s language?

Because I want to use cmdstanr and not rstan. My understanding is that’s how I’m supposed to do it.

It seems like you can use either if you don’t need both rstan and cmdstanr in the same document (based on the docs, anyway, I haven’t personally tried it).

I’m not sure if cmdstanr has any way to control how rstudio highlights inside of markdown documents, but I’ll ping @jonah just in case

the code snippet is the Stan program, therefore the syntax coloring is stan.
what interface you use to run the Stan program is another question, but RStudio
is only looking at file types - the qmd file you’re editing, and syntax tags -
the src code language, which is stan.

I’m just trying to follow the instructions in

I don’t want to use option 1, when I use option 2 the call to register_knitr_engine doesn’t do anything and I still end up with an rstan model, and finally option 3 is what I’m actually doing (using a cmdstan code chunk) and I am able to build a cmdstanr model but the syntax coloring is all wrong.

I feel that both the cmdstan and stan code chunks should have the same syntax coloring. I don’t know how things are happening in the backend, but as a user I would expect that a supported Stan code chunk (i.e., a cmdstan code chunk) would have correct syntax coloring.

I also just tried to use knitr::knit_engines$set(stan = cmdstanr::eng_cmdstan) but it doesn’t work because I’m still building an rstan model.

Does it highlight any differently in a .Rmd file verses a .qmd?

We don’t really have that much control over how RStudio is rendering things. The register_knitr_engine is already requesting the text be highlighted as Stan

Same thing in Rmd files. Ok, so this sounds like an RStudio/Quarto bug.

have you tried changing the {cmdstan} tag to {stan}.

CmdStan, CmdStanR, and all the other interfaces are interfaces to Stan - any model written using the Stan language is a Stan model. In CmdStanR, a CmdStanModel object manages the underlying Stan program. therefore, when showing the source code for the contents of a .stan file, please use the tag stan.

in that article, the example is correct for both options -

```{stan, output.var="model"}
// Stan model code

(not sure what output.var="model" is doing - the relevant part is that the code block is type stan)

The stan tag should cause RStudio to use rstan, the cmdstan tag should cause it to use cmdstanr, in conjunction with cmdstanr::register_knitr_engine(). This definitely used to work properly in Rmd files (although I haven’t tested qmd files), so if it’s not working properly in Rmd anymore perhaps there’s an RStudio bug, or perhaps they changed something that we’re unaware of and we haven’t made the necessary updates. Maybe @bearloga has an idea (they contributed the very nice functionality to allow switching between rstan and cmdstanr, or using both in the same document)?

here’s what I’m referring to:

please change to 3 backticks followed by {stan}.
does that work?

1 Like

Come on now, I wrote that above and I obviously know that removing the “cmd” part would make the highlighting work :-D

My problem is I want to use cmdstan so I would end up using cmdstanr and not rstan! I was trying to follow the instructions on the cmdstanr website (above) to do exactly that.

Listen, if the highlighting can’t work with cmdstan that’s ok. I never write a real model in a chunk like this. I just wanted to include a simple pretty example in an RStudio notebook, something embedded in there and not in a separate Stan file.

I think we’re talking past each other here and I’m terribly sorry for any misunderstanding.

  • to use CmdStanR (and therefore some version of CmdStan), you load package cmdstanr into your R session or notebook.

  • to ensure that code chunks in Rmd or qmd files have the proper syntax coloring, you add the language tag to the code block opening.

are you doing both of these things?

what is being displayed in which part of RStudio that is not properly highlighted? please share a screenshot.

No need to apologize. I hope I’m not missing something obvious here.

If you look here R Markdown CmdStan Engine • cmdstanr you will see that the tag cmdstan is a legitimate one to generate a cmdstanr model in the notebook. Such a chunk of code in the file will not display correct syntax highlighting in RStudio (I’m not talking about the html output). There are supposed to be ways (see other options in same document) that allow us to force the tag “stan” to use cmdstanr. However, this is broken; rstan is always used no matter what I do to override it.

1 Like

thanks, now I see.

maybe the R devs can help - @jonah, @rok_cesnovar, @andrjohns ?

I have reported this bug - I think that this “feature” should be removed altogether.