luapandoc

Add text to start of Lua pandoc Span and Div when I show/hide solutions


I am trying to write a Lua filter for pandoc to show solutions based on a metadata variable for both span and div elements. I was able to get the answers to render based on the metadata filter using this this question/answer.

Now, I would like to do two things to make the answer stand out:

  1. I would like to prepend the text "ANSWER: " as the first element in both the Span and Div
  2. I would like to render the text inside each Span and Div using strong.

This Lua filter works to show/hide the solutions:

-- A lua filter to render answers based on YAML block entry `show-answers`
-- 
-- The class `.answers` will render when `show-answers` is True and not if 
-- `show-answers` option is False (note captialization)
--
-- Barely adapted from: https://stackoverflow.com/a/75837627/12586249

local show_answers = nil

function Pandoc (doc)
  show_answers = doc.meta['show-answers']
  return doc
end

function Span (span)
  if span.classes:includes 'answer' then
    return show_answers and span.content or {}
  end
end

function Div (div)
  if div.classes:includes 'answer' then
    return show_answers and div.content or {}
  end
end


return {
  {Pandoc = Pandoc},
  {Div = Div},
  {Span = Span}
}

I have tried to add text (like this solution suggests), but that removes inline math and code blocks also need to be included in the answers.

I tried prepending the string "ANSWER: " to the content of the span:

function Span (span)
  if span.classes:includes 'answer' then
    new_content = {{"ANSWER: "} .. span.content}
    return show_answers and new_content or {}
  end
end

but I received the following error: Inline, list of Inlines, or string expected, got table.

Here is a minimal markdown example that would use the filter:

---
title: A Test of Answer Reveal
show-answers: True
---

## Question 1

What is the equatio of a line? [A line follows the formula $y = mx + b$]{.answer}

## Question 2 

Randomly generate a normally distributed population of weights with a mean weight among women of 150 pounds and 190 pounds among men with standard deviation of 25 pounds.

::: { .answer }
We need to set our $\beta_0$ to equal 150 and $\beta_1$ to equal 40, then generate the population and assume approximately equal shares of men and women:

```{r}
N <- 1e6L
beta0 <- 140
beta1 <- 40
sigma <- 25

pop <- rnorm(N, beta0 + sample(c(0,1), N, replace = TRUE) * beta1, sigma)

```
:::

Solution

  • Let's start with Span elements, because everything is easier there. To get bold text, it's enough to wrap the content with pandoc.Strong – pandoc calls bold text "strongly emphasized".

    function Span (span)
      if span.classes:includes 'answer' then
        return show_answers and pandoc.Strong(span.content) or {}
      end
    end
    

    The code to add ANSWER: was almost correct, it just wraps the result in too many curly braces. Removing the extra braces should work:

    function Span (span)
      if span.classes:includes 'answer' then
        return show_answers
          and pandoc.Strong({"ANSWER: "} .. span.content)
          or {}
      end
    end
    

    Now on to divs. It's slightly more complicated there, because we need to traverse all paragraphs in the div to make them bold. It could also be that the div doesn't start with a paragraph but with a list, so prepending the ANSWER string becomes more difficult.

    We solve the first issue by using a "local" filter on all answer elements and then apply it with :walk:

    local make_bold = function (element)
      element.content = {pandoc.Strong(element.content)}
      return element
    end
    
    function Div (div)
      if div.classes:includes 'answer' then
        return show_answers
          and div.content:walk{Plain = make_bold, Para = make_bold}
          or {}
      end
    end
    

    And now finally, some code to prepend a string to a list of blocks. It can be used in the Div filter function:

    local function prepend_string (str, blks)
      -- Check if we can add the string to the contents of the first element
      if blks[1] and pandoc.utils.type(blks[1].content) == "Inlines" then
        blks[1].content:insert(1, str)
        return blks
      else
        -- otherwise we add a new `Plain` element with the string
        return {pandoc.Plain{str}} .. blks
      end
    end