Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing Multiple Arguments/Options to an Environment #17

Open
advieser opened this issue Aug 28, 2024 · 0 comments
Open

Passing Multiple Arguments/Options to an Environment #17

advieser opened this issue Aug 28, 2024 · 0 comments

Comments

@advieser
Copy link

advieser commented Aug 28, 2024

Hey,

it seems that currently it is only possible to pass multiple arguments/options to commands, not environments. However, this would be a useful addition., i.e. it should be possible to do something like this

::: {.env arguments="1,2"}
body
:::

which should result in

\begin{env}{1}{2}
  body
\end{env}

The same should also be possible for options.

I developed something that at least works for my purposes by editing _extensions/latex-environment/latex-environment.lua and basically copying what is already implemented for commands. However, as I'm not really familiar with Lua nor particualrly profficient with LaTeX, I did not want to open a PR directly. Here's what I did:

Redefine buildCommandArgs (now simply called buildArgs since it is also for environments):

-- helper function to parse multiple options/arguments --
local function buildArgs(opts, format)
  local function wrap(o)
    return string.format(format, o)
  end
  local t = pandoc.List()
  local last_pos = 1
  opts = opts .. ","  -- Add a trailing comma to handle the last argument
  
  for str in string.gmatch(opts, "([^,]*),") do
    if str == "" then
      t:insert("")
    else
      t:insert(str)
    end
  end
  
  return table.concat(t:map(wrap), "")
end

This is basically the same as the buildCommandArgs, however, I tried to add the possibility of leaving arguments empty, i.e by using ::: {.env arguments=",2,3"}. This should probably be considered a separate issue.

Additionaly, I updated writeEnvironment:

-- Use the environments from metadata to emit a custom environment for LaTeX
local function writeEnvironments(divEl)
  if quarto.doc.is_format("latex") then
    for k, v in pairs(classEnvironments) do
      if divEl.attr.classes:includes(k) then
        -- Process this into a LaTeX environment
        local beginEnv = '\\begin' .. '{' .. v .. '}'
        local endEnv = '\n\\end{' .. v .. '}'
        
        -- Check if custom options or arguments are present
        -- and add them to the environment accordingly
        local opts = divEl.attr.attributes['options']
        if opts then
          beginEnv = beginEnv .. buildArgs(opts, "[%s]")
        end

        local args = divEl.attr.attributes['arguments']
        if args then
          beginEnv = beginEnv .. buildArgs(args, "{%s}")
        end
        
        -- If the first and last div blocks are paragraphs, 
        -- bring the environment begin/end closer to the content
        if #divEl.content > 0 and divEl.content[1].t == "Para" and divEl.content[#divEl.content].t == "Para" then
          table.insert(divEl.content[1].content, 1, pandoc.RawInline('tex', beginEnv .. "\n"))
          table.insert(divEl.content[#divEl.content].content, pandoc.RawInline('tex', "\n" .. endEnv))
        else
          table.insert(divEl.content, 1, pandoc.RawBlock('tex', beginEnv))
          table.insert(divEl.content, pandoc.RawBlock('tex', endEnv))
        end
        return divEl
      end
    end
  end
end

Obviously, also change all other mentions of buildCommandArgs.

I hope this can be implemented sometime. As I said, I'm not too comfortable with Lua and LaTeX, so this should probably be checked more thoroughly then I was capable of.

In the meantime, there is also the (ugly) workaround of using

::: {.env arguments="1}{2}{3"}
body
:::
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant