Skip to content

Commit

Permalink
Add support for arguments in command
Browse files Browse the repository at this point in the history
fixes #15 - Arguments in commands?
  • Loading branch information
cderv committed Oct 31, 2023
1 parent ab5a579 commit 4e394e7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
4 changes: 2 additions & 2 deletions _extensions/latex-environment/_extension.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
title: LaTeX Environment
author: Posit Software, PBC
version: 1.1.2
quarto-required: ">=1.2.198"
version: 1.2.1
quarto-required: ">=1.3"
contributes:
filters:
- latex-environment.lua
Expand Down
27 changes: 22 additions & 5 deletions _extensions/latex-environment/latex-environment.lua
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,17 @@ local function writeEnvironments(divEl)
end
end

local function buildCommandArgs(opts, format)
local function wrap(o)
return string.format(format, o)
end
local t = pandoc.List()
for str in string.gmatch(opts, "([^"..",".."]+)") do
t:insert(str)
end
return table.concat(t:map(wrap), "")
end

-- use the environments from metadata to
-- emit a custom environment for latex
local function writeCommands(spanEl)
Expand All @@ -105,19 +116,25 @@ local function writeCommands(spanEl)
if spanEl.attr.classes:includes(k) then

-- resolve the begin command
local beginCommand = pandoc.RawInline('latex', '\\' .. pandoc.utils.stringify(v) .. '{')
local beginCommand = '\\' .. pandoc.utils.stringify(v)
local opts = spanEl.attr.attributes['options']
local args = spanEl.attr.attributes['arguments']
if opts then
beginCommand = pandoc.RawInline('latex', '\\' .. pandoc.utils.stringify(v) .. '[' .. opts .. ']{')
beginCommand = beginCommand .. buildCommandArgs(opts, "[%s]")
end
if args then
beginCommand = beginCommand .. buildCommandArgs(args, "{%s}")
end

local beginCommandRaw = pandoc.RawInline('latex', beginCommand .. '{')

-- the end command
local endCommand = pandoc.RawInline('latex', '}')
local endCommandRaw = pandoc.RawInline('latex', '}')

-- attach the raw inlines to the span contents
local result = spanEl.content
table.insert(result, 1, beginCommand)
table.insert(result, endCommand)
table.insert(result, 1, beginCommandRaw)
table.insert(result, endCommandRaw)

return result
end
Expand Down
14 changes: 11 additions & 3 deletions example.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ format:
filters:
- latex-environment
environments: [center, enumerate]
commands: [ce, item]
commands: [ce, item, colorbox, fcolorbox]
---

## LaTeX Environments
Expand All @@ -27,11 +27,19 @@ Will replace spans of class `ce` with the `\ce{}` command for LaTeX output, but

## Advanced Environments and Commands

You can also combine environments and commands (with options) to address more complex scenarios.
You can also combine environments and commands (with options) to address more complex scenarios. Usually with LaTeX:
* `[opts]` is used for optional arguments
* `{args}` for mandatory arguments

Both environments and commands can accept an `options` attribute which will be properly applied in LaTeX and will be available as an attribute in HTML output.
Both environments and commands can accept an `options` and `arguments` attributes which will be properly applied in LaTeX and will be available as an attribute in HTML output.

:::{.enumerate}
[]{.item options="--"} Question 1
[]{.item options="--"} Question 2
:::

[This text is in a orange background]{.colorbox arguments="BurntOrange"}

If you have multiple arguments or options to pass to a command, use comma separated string

[This one will have a black border]{.fcolorbox arguments="black,red"}

0 comments on commit 4e394e7

Please sign in to comment.