R Markdown with \newif fails to compile to pdf after update

After the latest update to RStudio "Ghost Orchid" (Build 372) the RMarkdown below fails to compile to pdf on macOS Monterey, as well as on RStudio cloud.

An identical file compiles fine on a different machine running previous versions of both RStudio and macOS ("Juliet Rose" (Version 1.4.1717), macOS Big Sur).

Any help to fix the problem is highly appreciated, thanks!

---
title: "minimalTest"
output: pdf_document
---

``` {r, include = F}
solution <- TRUE 
``` `

\newif\ifsol
\sol`r ifelse(solution, 'true', 'false')`

# Simple example of non-compiling code

\ifsol

Text to conditionally skip

\fi

The error produce is the following

! Use of ifMT@inlist@ doesn't match its definition.
MT@setupfont ...g MT@check@font ifMT@inlist@ else 
                                                  MT@vinfo {Setting up font ...
l.77 code}
        \label{simple-example-of-non-compiling-code}}

The same error is generated under Ubuntu with an earlier version of RStudio. It's a LaTeX error message. Try this

---
title: "minimalTest"
output: pdf_document
---

``` {r, include = TRUE}
solution <- TRUE 

\def\sol{r solution}

\sol

Thanks a lot for your response. Yes, that's absolutely right, it's LaTeX to throw the error. However, from comparing the tex files between the machine where it compiles and that where it doesn't I find the following lines differ in the generated .tex

the file which works has

\newif\ifsol
\soltrue

the file which does not work has instead

\newif\ifsol
\def\iffalse\iftrue

Is this a difference due to pandoc?

Also, I am not sure I understand how to use \def instead. Basically I want to use the variable solution to conditionally include text in a file, and I have that code in a large number of files, so I would really like to find a solution that allows me to compile those files without modifications on the new machine. Since the identical file compiles on different machines, something must have changed in the way one of the softwares involved handles the source.

Too many moving pieces. Let me think about the problem in the terms you've now described it—conditionally including text based on the evaluation of some arbitrary R command and get back to you.

Just ran a few more tests confirming the suspicion that it may be a pandoc issue. In fact doing the following works

  • installing a previous version of pandoc (2.13 is the latest I know to work on my laptop)

  • telling rmarkdown to use the desired version of pandoc: rmarkdown::find_pandoc(version="2.13")

  • rendering the document with a command instead of pressing the knit button
    rmarkdown::render("minimalTest.Rmd", pdf_document())

For the last point it would be great if there were an option in RStudio to specify the location of the pandoc to use, rather than having to go via render.

More generally to avoid this sort of problem it would be even better to be able to choose the pandoc version to use in RStudio

I can't reproduce the problem due to lack of a minimal working example‐what is working with the portions of tex posted is missing something for it to work.

Since the goal is to salvage existing code, there's nothing I can offer beyond using \def. I can post more detailed examples of that with error trapping being done in the online r snippet if of interest.

Thanks. Actually, I do now have a workaround: configure RStudio to use panic 2.13. Then all my files compile correctly without problems (including the minimal example originally provided).
Some more details are here Can pandoc deal with ifdefined or skip certain parts with special LaTeX macros. · Issue #6096 · jgm/pandoc · GitHub

1 Like

@annlia I think this is indeed related to Pandoc and I still thing this is a bug on their side but I am not quite understand the change.

I have reported a change to their issue board too Possible regression with detecting and escaping raw latex in markdown when using `\iffalse` · Issue #7460 · jgm/pandoc · GitHub

Can you use R and conditional chunk to do what you want ?
Are those recipies helping ?

?

@cderv Thanks a lot for coming back to this, much appreciated. In fact I had also briefly mentioned the issue there Can pandoc deal with ifdefined or skip certain parts with special LaTeX macros. · Issue #6096 · jgm/pandoc · GitHub

As for your suggestion to use a conditional chunk with asis, I just tested it on the RStudio cloud (on my machine I set things up to use Pandoc 2.13) with a simple example and it seems to work. The only down side of that is that the parts I wish to include conditionally are in LaTeX, and using code chunks the editor syntax highlighting won't work. In addition I have several files using the code which doesn't work and it would not be much fun having to change all of them.

For clarification my intention is not to compile different things for different formats (LaTeX or HTML) but just really to includes sections of text conditionally (solution to exercise in the specifics).

---
title: "minimalTest"
output: pdf_document
---

``` {r, include = F}
solution <- TRUE ```


```{asis, echo = solution == TRUE}
\section*{Test}
$f(x) = \frac{1}{\sqrt{2\pi\sigma^2}}e^{\frac{(\mu-x)^2}{2\sigma^2}}$```


\section*{Test}
$f(x) = \frac{1}{\sqrt{2\pi\sigma^2}}e^{\frac{(\mu-x)^2}{2\sigma^2}}$

Yes I know. You could try visual editor in RStudio IDE, maybe it can detect better this.

As this is now how Pandoc is rendering things until it is possibly fixed, you need to stick with pandoc 2.13.
Or deactivated the Pandoc extensions that cause the trouble as it was adviced on the issue thread in GIthub

output: 
  pdf_document:
    md_extensions: -latex_macros

If you only outputing LaTeX, I don't think you'll loose anything.

Thanks! Sticking to pandoc 2.13 is working well for me now, so I may just do that until I have any good reasons to change :slight_smile:

John MacFarlane has closed the related issue with

LaTeX reader: improve handling of newif.

Adding a pair of braces around the second argument of `\def`
prevents LaTeX from an emergency stop with:  Closes #6096.

pandoc -f markdown -o test.pdf
\newif\ifepub

\epubtrue

\ifepub

hi

\fi
^D

Thanks @technocrat !

This should indeed be fixed in next Pandoc release. Testing your example @annlia works now with pandoc nightly build.

Thanks a lot for the report and pushing Pandoc to fix this.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.