Skip to content

make latex-div lua filter apply only on latex output and no need for data-latex="" #1779

@cderv

Description

@cderv

Design about this lua filters by @RLesur has been discussed in yihui/bookdown-crc#1 and added in rmarkdown in cecd812

Currently, the lua filter is only activated and useful for latex output if data-latex= has been added. If not useful, it must be set to ""

I think this is not obvious from what you expect from pandoc and it does not serve well "multi-format" output, because you need to add data-latex="" if you want it to work in pdf and in html. Currently, it seems that example in ongoing bookdown-cookbook is not working : rstudio/rmarkdown-cookbook#188

How about improving the filter my thought:

  • we could make the Div function only apply for latex output format in the lua filter. No return nil required, and no risk to conflict with fenced_divs for other format ?
  • That way if no data-latex= is provided, we could generate a verbatim environment, and otherwise data-latex should be provided to set options.

I don't think this will break anything, just change the way the lua filter is activated, to make this type of example works
https://bookdown.org/yihui/rmarkdown-cookbook/multi-column-layout.html

I have something like this in mind

Div = function (div)
  local options = div.attributes['data-latex']

  -- if the output format is not latex, the object is left unchanged
  if FORMAT ~= 'latex' and FORMAT ~= 'beamer' then
    -- if options has been set for latex, unset for other output
    if options then
      div.attributes['data-latex'] = nil
    end
    return div
  end

  local env = div.classes[1]
  -- if the div has no class, the object is left unchanged
  if not env then return nil end

  -- insert raw latex before content
  table.insert(
    div.content, 1,
    pandoc.RawBlock('tex', '\\begin' .. '{' .. env .. '}' .. (options or ""))
  )
  -- insert raw latex after content
  table.insert(
    div.content,
    pandoc.RawBlock('tex', '\\end{' .. env .. '}')
  )
  return div
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions