2

I am using quarto to create a lesson with lots of mixed in tex. I want two version of this lesson -- one with answers and one without the answers -- in the same document to avoid trying to keep the two files consistent. What is the easiest way to do this?

I tried

::: {.content-visible XXX}

Will only appear in HTML.

:::

but that only seems to be working if I want to change the document output format. I want it to toggle with just a TRUE or FALSE value. I also tried

```{r, eval = showText, echo = TRUE, output = "asis"}
The probability that $n^2$ items happy is `dpois(n, 1)`.
``` 

However, this gives me the error

Error: unexpected symbol in "The probability"

Furthermore, it doesn't render the latex $n^2$

UPDATE: I tried the Lua filter approach without success.

---
title: "Conditional Content"
format: pdf
editor: visual
hide-answer: false
filters: 
  - hide-answer.lua
---

## Answer Test

-   This is a list

    -   This $n^2$ works

-   This is another element

-   **Question:** What is my name?

::: answer
    -   Why is this $n^2$ failing?
:::

-   Continuation

Lua-Problem

blahblah4252
  • 105
  • 4
  • 1
    Have a look at the [params](https://quarto.org/docs/computations/parameters.html) in quarto. Maybe this answer points you in the correct direction: https://stackoverflow.com/questions/73571919/how-to-pass-logical-parameters-with-the-quarto-r-package-to-the-knitr-chunk-opti – Julian Feb 14 '23 at 09:19

2 Answers2

1

You can use Lua filter to create an option which if true, will remove all the contents within answer divs.

---
title: Conditional content
format: pdf
hide-answer: true
filters: 
  - hide-answer.lua
---

## Part 01

**Question 01: What is the probability that .... ?**

::: answer

The probability that $n^2$ items happy is `dpois(n, 1)`.

:::

hide-answer.lua

function answer()
  return {
   Div = function(el)
     if el.classes:includes('answer') then
       return pandoc.Null()
     else 
       return el
     end
   end
  }
end


function Pandoc(doc)
  local meta = doc.meta
  local hide = meta['hide-answer']
  if hide  then
    return doc:walk(answer())
  end
end

output when hide-answer: true,

output without answer


output when hide-answer: false,

output with answer

shafee
  • 15,566
  • 3
  • 19
  • 47
  • I'm new to Lua filters... any idea why when I try to add things like bulleting inside the answers (e.g., - The Probability), it renders it as code instead of converting the Latex and such properly? – blahblah4252 Feb 14 '23 at 21:51
  • To clarify, I meant bullet continuation. "- The Probability" works but not " - The Probability") – blahblah4252 Feb 14 '23 at 21:58
  • @blahblah4252, can you update your question with an small example of qmd file that showcases the problem. I am confused by the word "bullet continuation" : ) – shafee Feb 15 '23 at 10:53
  • Firstly, I think you are trying to create nested bullet points but a single bullet point inside the pandoc divs (`:::`) will not have a nested structure. It will be a simple bullet point. – shafee Feb 17 '23 at 04:52
  • And you are getting a code formatted line because you have 4 spaces in front of the `- Why is this $n^2$ failing?` and Quarto considers it a code block. But why the former one is working? It is because the former one had a list item before it and so ` - This $n^2$ works` is considered as a nested list item. But the list item inside the `answer` divs is considered as a different list environment and since it has 4 spaces in front of it, it is considered as code block. – shafee Feb 17 '23 at 05:02
  • To solve it, you can pull bullet points inside the `answer` div without any indent before it and try [this answer](https://tex.stackexchange.com/a/78339/261065) or similar to get indentation before the list item. – shafee Feb 17 '23 at 05:04
0

I’m trying to do the same at the moment and am exploring the use of profiles. https://quarto.org/docs/projects/profiles.html

I’d love to give a more complete answer, but just wanted to share that. I’m creating a profile for student (without answers) and TA (with answers).

If any experts have further advice, please feel free!

Best wishes!