-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1f60acf
commit 5709818
Showing
6 changed files
with
154 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`) | ||
::: | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`) | ||
::: |