Skip to content

Commit

Permalink
typst improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
wjschne committed May 2, 2024
1 parent d378726 commit 28e9f13
Show file tree
Hide file tree
Showing 15 changed files with 175 additions and 64 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ docx: $(SOURCE)
typst-man: $(SOURCE)
quarto render $< --to apaquarto-typst \
--output example-$@.pdf

# Don't know yet how to use the documentmode: X trick with Typst
# https://github.com/quarto-dev/quarto-cli/discussions/3733
2 changes: 1 addition & 1 deletion _extensions/apaquarto/_extension.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
title: My Document in APA Style, Seventh Edition
author: W. Joel Schneider
version: 4.0.0
version: 4.0.1
quarto-required: ">=1.4.549"
contributes:
formats:
Expand Down
2 changes: 1 addition & 1 deletion _extensions/apaquarto/apaafternote.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ if FORMAT == "latex" then
return
end

-- The spacing in paragraphs after a figure or table
-- The spacing in paragraphs after a figure or table
-- without a note makes a special style necessary.

-- Set custom style in paragraph by setting it in a custom div
Expand Down
10 changes: 7 additions & 3 deletions _extensions/apaquarto/apafloatstoend.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ end
Pandoc = function(doc)
local tbl = {}
local fig = {}
local movefloatstoend = true
if doc.meta.floatsintext and pandoc.utils.stringify(doc.meta.floatsintext) == "true" then
movefloatstoend = false
end


if movefloatstoend then
for i = #doc.blocks, 1, -1 do
if doc.blocks[i].identifier then
if doc.blocks[i].identifier:find("^tbl%-") then
Expand Down Expand Up @@ -81,9 +87,7 @@ Pandoc = function(doc)
end
end

if doc.meta.floatsintext and pandoc.utils.stringify(doc.meta.floatsintext) == "true" then


if movefloatstoend then

-- Find block where appendices begin
local appendixblock = 0
Expand Down
11 changes: 10 additions & 1 deletion _extensions/apaquarto/crossrefprefix.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
local abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
-- Default prefix
local prefix = ""
-- Default pre-prefex if appendices exceed 26
local preprefix = ""
-- Prefix counter
local intprefix = 0
-- Pre-prefix counter
local intpreprefix = 0
-- Table counter
local tblnum = 0
-- Figure counter
Expand Down Expand Up @@ -50,10 +54,15 @@ end
Block = function(b)
-- Increment prefix for every level-1 header starting with Appendix
if b.tag == "Header" and b.level == 1 and pandoc.text.sub(pandoc.utils.stringify(b.content), 1, 8) == "Appendix" then
if intprefix == 26 then
intprefix = 0
intpreprefix = intpreprefix + 1
preprefix = preprefix .. pandoc.text.sub(abc,intpreprefix,intpreprefix)
end
intprefix = intprefix + 1
tblnum = 0
fignum = 0
prefix = pandoc.text.sub(abc,intprefix,intprefix)
prefix = preprefix .. pandoc.text.sub(abc,intprefix,intprefix)
end

-- Assign prefixes and numbers
Expand Down
13 changes: 11 additions & 2 deletions _extensions/apaquarto/frontmatter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,13 @@ return {
if meta.apatitledisplay then
if meta["blank-lines-above-title"] and #meta["blank-lines-above-title"] > 0 then
local possiblenumber = stringify(meta["blank-lines-above-title"])
intabovetitle = math.floor(tonumber(possiblenumber)) or 2
if type(possiblenumber) == "number" then
local intnumber = tonumber(possiblenumber) * 1
intabovetitle = math.floor(intnumber) or 2
else
intabovetitle = 2
end

end
for i=1,intabovetitle do
body:extend({newline})
Expand Down Expand Up @@ -381,7 +387,7 @@ return {
if a.attributes then
if a.attributes.corresponding and stringify(a.attributes.corresponding) == "true" then
if check_corresponding then
error("There can only be one author marked as the corresponding author. " .. stringify(makeauthorname(a.name)) .. " is the second author you have marked as the corresponding author.")
error("There can only be one author marked as the corresponding author. " .. stringify(a.apaauthordisplay) .. " is the second author you have marked as the corresponding author.")
end
check_corresponding = true
corresponding_paragraph.content:extend(a.apaauthordisplay)
Expand Down Expand Up @@ -454,6 +460,9 @@ return {
local abstractdiv = pandoc.Div({})
local abstractfirstparagraphdiv = pandoc.Div({})
local abstractlinecounter = 1
if FORMAT == "typst" then
abstractlinecounter = 2
end
meta.apaabstract:walk {
LineBlock = function(lb)
lb:walk {
Expand Down
94 changes: 57 additions & 37 deletions _extensions/apaquarto/typst/formattypst.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,65 @@ if FORMAT ~='typst' then
return
end

Div = function(div)
-- Center author and affiliation
if div.classes:includes("Author") then
return {pandoc.RawBlock('typst', "#set align(center)"), div, pandoc.RawBlock('typst', "#set align(left)")}
end

-- Hanging indent on refs
if div.identifier == "refs" then
return {pandoc.RawBlock("typst", "#set par(first-line-indent: 0in, hanging-indent: 0.5in)"), div, pandoc.RawBlock("typst","#set par(first-line-indent: 0.5in, hanging-indent: 0in)") }
end
end



-- typst aggressively wants to make first paragraphs after something not intented.
-- APA style wants almost all paragraphs to be indented.
-- This function inserts a blank paragraph and then negative vertical space
-- before any first paragraph. Hoping that typst will fix this and that this function
-- becomes unnecessary.

local appendixword = "Appendix"
function Pandoc (doc)
if doc.meta.lang and doc.meta.lang["section-title-appendix"] then
appendixword = pandoc.utils.stringify(doc.meta.lang["section-title-appendix"])
end

for i = #doc.blocks, 1, -1 do
if doc.blocks[i].t == "Para" and doc.blocks[i-1].t ~= "Para" then
if doc.blocks[i-1].t == "Header" and doc.blocks[i-1].level > 3 then
--Do nothing
else
doc.blocks:insert(i, pandoc.RawBlock("typst", "#par()[#text(size:0.5em)[#h(0.0em)]]\n#v(-18pt)"))
return {
{
-- Replace LaTeX logo
Math = function(eq)
if eq.mathtype == "InlineMath" then
if eq.text == "\\LaTeX" then
return pandoc.Str("LaTeX")
end
if eq.text == "\\TeX" then
return pandoc.Str("TeX")
end
end
end


if doc.blocks[i].t == "Header" and doc.blocks[i].level == 1 and doc.blocks[i].content[1].text == appendixword then
doc.blocks:insert(i+1, pandoc.RawBlock("typst", "#counter(figure.where(kind: \"quarto-float-fig\")).update(0)\n#appendixcounter.step()"))
},
{
Div = function(div)
-- Center author and affiliation
if div.classes:includes("Author") then
return {pandoc.RawBlock('typst', "#set align(center)"), div, pandoc.RawBlock('typst', "#set align(left)")}
end

-- Hanging indent on refs
if div.identifier == "refs" then
return {pandoc.RawBlock("typst", "#set par(first-line-indent: 0in, hanging-indent: 0.5in)"), div, pandoc.RawBlock("typst","#set par(first-line-indent: 0.5in, hanging-indent: 0in)") }
end

if div.classes:includes("NoIndent") then
return {pandoc.RawBlock('typst', "#set par(first-line-indent: 0mm)"), div, pandoc.RawBlock('typst', "#set par(first-line-indent: firstlineindent)")}
end
end
} ,
{
Pandoc = function (doc)
-- typst aggressively wants to make first paragraphs after something not intented.
-- APA style wants almost all paragraphs to be indented.
-- This function inserts a blank paragraph and then negative vertical space
-- before any first paragraph. Hoping that typst will fix this and that this function
-- becomes unnecessary.
local appendixword = "Appendix"
if doc.meta.lang and doc.meta.lang["section-title-appendix"] then
appendixword = pandoc.utils.stringify(doc.meta.lang["section-title-appendix"])
end

for i = #doc.blocks, 1, -1 do
if doc.blocks[i].t == "Para" and doc.blocks[i-1].t ~= "Para" then
if doc.blocks[i-1].t == "Header" and doc.blocks[i-1].level > 3 then
--Do nothing
else
doc.blocks:insert(i, pandoc.RawBlock("typst", "#par()[#text(size:0.5em)[#h(0.0em)]]\n#v(-18pt)"))
end
end
-- Count appendices
if doc.blocks[i].t == "Header" and doc.blocks[i].level == 1 and doc.blocks[i].content[1].text == appendixword then
doc.blocks:insert(i+1, pandoc.RawBlock("typst", "#counter(figure.where(kind: \"quarto-float-fig\")).update(0)\n#counter(figure.where(kind: \"quarto-float-tbl\")).update(0)\n#appendixcounter.step()"))
end
end
return doc
end
end
return doc
end
}
}
4 changes: 2 additions & 2 deletions _extensions/apaquarto/typst/typst-show.typ
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#show: doc => article(
$if(shorttitle)$
running-head: "$shorttitle$",
runninghead: "$shorttitle$",
$else$
$if(title)$
running-head: "$title$",
runninghead: "$title$",
$endif$
$endif$
$if(paper)$
Expand Down
39 changes: 34 additions & 5 deletions _extensions/apaquarto/typst/typst-template.typ
Original file line number Diff line number Diff line change
@@ -1,30 +1,55 @@

// counts how many appendixes there are
#let appendixcounter = counter("appendix")
// make latex logo
// https://github.com/typst/typst/discussions/1732#discussioncomment-6566999
#let TeX = style(styles => {
set text(font: ("New Computer Modern", "Times", "Times New Roman"))
let e = measure("E", styles)
let T = "T"
let E = text(1em, baseline: e.height * 0.31, "E")
let X = "X"
box(T + h(-0.15em) + E + h(-0.125em) + X)
})
#let LaTeX = style(styles => {
set text(font: ("New Computer Modern", "Times", "Times New Roman"))
let a-size = 0.66em
let l = measure("L", styles)
let a = measure(text(a-size, "A"), styles)
let L = "L"
let A = box(scale(x: 105%, text(a-size, baseline: a.height - l.height, "A")))
box(L + h(-a.width * 0.67) + A + h(-a.width * 0.25) + TeX)
})

#let firstlineindent=0.5in


// make article
#let article(
title: none,
running-head: none,
runninghead: none,
margin: (x: 1in, y: 1in),
paper: "us-letter",
font: ("Times New Roman"),
font: ("Times", "Times New Roman"),
fontsize: 12pt,
leading: 18pt,
spacing: 18pt,
first-line-indent: 0.5in,
firstlineindent: 0.5in,
toc: false,
lang: "en",
cols: 1,
doc,
) = {



set page(
paper: paper,
margin: margin,
header-ascent: 50%,
header: grid(
columns: (9fr, 1fr),
align(left)[#upper[#running-head]],
align(left)[#upper[#runninghead]],
align(right)[#counter(page).display()]
)
)
Expand All @@ -39,7 +64,7 @@ set table(
set par(
justify: false,
leading: leading,
first-line-indent: first-line-indent
first-line-indent: firstlineindent
)

// Also "leading" space between paragraphs
Expand All @@ -51,6 +76,10 @@ set table(
lang: lang
)

// show LaTeX
show "TeX": TeX
show "LaTeX": LaTeX

// format figure captions
show figure.where(kind: "quarto-float-fig"): it => [

Expand Down
16 changes: 15 additions & 1 deletion changelog.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,21 @@ engine: knitr

- Create landscape pages for wide figures and tables.
- Tables in .pdf jou mode should fit automatically.
- Typst version that would allow for easy customization
- Implement missing Typst features
* Journal format
* Line numbering
* Full language option support


# Version 4.0.1 (2024-05-01)

- Improvemements for typst
* Render LaTeX logo correctly
* Implemented NoIndent feature for typst
* Proper indenting for multi-paragraph abstracts
* Fixed numbering of tables in appendices
- Fixed bug that reversed floatsintext to the opposite of expectations.
- On the remote chance that anyone needs more than 26 appendices, subsequent appendices are AA, AB, AC, ..., BA, BB, BC, ... AAA, AAB, ...

# Version 4.0.0 (2024-05-01)

Expand Down
22 changes: 21 additions & 1 deletion docs/changelog.html
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ <h2 id="toc-title">On this page</h2>

<ul>
<li><a href="#future-version-wishlist" id="toc-future-version-wishlist" class="nav-link active" data-scroll-target="#future-version-wishlist">Future Version Wishlist</a></li>
<li><a href="#version-4.0.1-2024-05-01" id="toc-version-4.0.1-2024-05-01" class="nav-link" data-scroll-target="#version-4.0.1-2024-05-01">Version 4.0.1 (2024-05-01)</a></li>
<li><a href="#version-4.0.0-2024-05-01" id="toc-version-4.0.0-2024-05-01" class="nav-link" data-scroll-target="#version-4.0.0-2024-05-01">Version 4.0.0 (2024-05-01)</a></li>
<li><a href="#version-3.5.4-2024-04-30" id="toc-version-3.5.4-2024-04-30" class="nav-link" data-scroll-target="#version-3.5.4-2024-04-30">Version 3.5.4 (2024-04-30)</a></li>
<li><a href="#version-3.5.3-2024-04-23" id="toc-version-3.5.3-2024-04-23" class="nav-link" data-scroll-target="#version-3.5.3-2024-04-23">Version 3.5.3 (2024-04-23)</a></li>
Expand Down Expand Up @@ -201,7 +202,26 @@ <h1>Future Version Wishlist</h1>
<ul>
<li>Create landscape pages for wide figures and tables.</li>
<li>Tables in .pdf jou mode should fit automatically.</li>
<li>Typst version that would allow for easy customization</li>
<li>Implement missing Typst features
<ul>
<li>Journal format</li>
<li>Line numbering</li>
<li>Full language option support</li>
</ul></li>
</ul>
</section>
<section id="version-4.0.1-2024-05-01" class="level1">
<h1>Version 4.0.1 (2024-05-01)</h1>
<ul>
<li>Improvemements for typst
<ul>
<li>Render LaTeX logo correctly</li>
<li>Implemented NoIndent feature for typst</li>
<li>Proper indenting for multi-paragraph abstracts</li>
<li>Fixed numbering of tables in appendices</li>
</ul></li>
<li>Fixed bug that reversed floatsintext to the opposite of expectations.</li>
<li>On the remote chance that anyone needs more than 26 appendices, subsequent appendices are AA, AB, AC, …, BA, BB, BC, … AAA, AAB, …</li>
</ul>
</section>
<section id="version-4.0.0-2024-05-01" class="level1">
Expand Down
2 changes: 1 addition & 1 deletion docs/installation.html
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,7 @@ <h4 class="anchored" data-anchor-id="an-existing-file">An existing file</h4>
});
</script>
</div> <!-- /content -->
<script>var lightboxQuarto = GLightbox({"selector":".lightbox","openEffect":"zoom","loop":false,"closeEffect":"zoom","descPosition":"bottom"});
<script>var lightboxQuarto = GLightbox({"selector":".lightbox","closeEffect":"zoom","openEffect":"zoom","loop":false,"descPosition":"bottom"});
window.onload = () => {
lightboxQuarto.on('slide_before_load', (data) => {
const { slideIndex, slideNode, slideConfig, player, trigger } = data;
Expand Down
2 changes: 1 addition & 1 deletion docs/search.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
"href": "changelog.html",
"title": "Changelog",
"section": "",
"text": "Create landscape pages for wide figures and tables.\nTables in .pdf jou mode should fit automatically.\nTypst version that would allow for easy customization"
"text": "Create landscape pages for wide figures and tables.\nTables in .pdf jou mode should fit automatically.\nImplement missing Typst features\n\nJournal format\nLine numbering\nFull language option support"
},
{
"objectID": "changelog.html#breaking-changes",
Expand Down
Loading

0 comments on commit 28e9f13

Please sign in to comment.