Skip to content

Commit

Permalink
Initial Extension
Browse files Browse the repository at this point in the history
  • Loading branch information
dragonstyle committed Jun 29, 2022
1 parent 1f60acf commit 5709818
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022 quarto-ext
Copyright (c) 2022 RStudio, PBC.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
45 changes: 44 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,44 @@
# latex-environment
# latex-environments

## Overview

This is Quarto extension that enables `divs` to be output as a custom environment in LaTeX. This is useful when you'd like to share content between LaTeX and other formats, but need the content to be placed in an environment when emitting LaTeX.

## Installation

To install this extension in your current directory (or into the Quarto project that you're currently working in), use the following command:

```
quarto install extension dragonstyle/latex-environments
```

## Usage

Divs with a class name listed in the in the `environments` key will be emitted in LaTeX as an environment with the provided name (or the class name itself if no name is provided). All of the following are valid:

```yaml
environments: program

environments:
- program

environments: [program]

environments:
program: program-env
```
## Example
```markdown
---
title: Test document
environments: [program]
----

:::{.program}
The contents of this div will be output in a `program`
latex environment, but will appear in HTML (and any other output
format as a simple div with the class `program`)
:::
```
6 changes: 6 additions & 0 deletions _extensions/quarto-ext/latex-environments/_extension.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
title: LaTeX Environments
author: RStudio, PBC
version: 0.1.0
contributes:
filters:
- extension.lua
67 changes: 67 additions & 0 deletions _extensions/quarto-ext/latex-environments/extension.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
-- environment.lua
-- Copyright (C) 2020 by RStudio, PBC

local classEnvironments = pandoc.MetaMap({})

-- helper that identifies arrays
local function tisarray(t)
local i = 0
for _ in pairs(t) do
i = i + 1
if t[i] == nil then return false end
end
return true
end

-- reads the environments
local function readEnvironments(meta)
local env = meta['environments']
if env ~= nil then
if tisarray(env) then
-- read an array of strings
for i,v in ipairs(env) do
local value = pandoc.utils.stringify(v)
classEnvironments[value] = value
end
else
-- read key value pairs
for k,v in pairs(env) do
local key = pandoc.utils.stringify(k)
local value = pandoc.utils.stringify(v)
classEnvironments[key] = value
end
end
end
end

-- use the environments from metadata to
-- emit a custom environment for latex
local function writeEnvironments(divEl)
if quarto.doc.isFormat("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 .. '}'

-- if the first and last div blocks are paragraphs then we can
-- bring the environment begin/end closer to the content
if 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

-- Run in two passes so we process metadata
-- and then process the divs
return {
{Meta = readEnvironments},
{Div = writeEnvironments}
}
35 changes: 35 additions & 0 deletions example.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
title: Environment Example
environments:
- program
format:
latex: default
html: default
filters:
- latex-environments
---

## Overview

Divs with a class name listed in the in the `environments` key will be emitted in LaTeX as an environment with the provided name (or the class name itself if no name is provided). All of the following are valid:

```yaml
environments: program
```
```yaml
environments:
- program
```
```yaml
environments:
program: program-env
```
## Example
:::{.program}
The contents of this div will be output in a `program`
latex environment, but will appear in HTML (and any other output format as a simple div with the class `program`)
:::

0 comments on commit 5709818

Please sign in to comment.