From 28e9f137dbd487bed2fe88993ea118181fe97603 Mon Sep 17 00:00:00 2001 From: "W. Joel Schneider" Date: Thu, 2 May 2024 14:09:18 -0400 Subject: [PATCH] typst improvements --- Makefile | 2 +- _extensions/apaquarto/_extension.yml | 2 +- _extensions/apaquarto/apaafternote.lua | 2 +- _extensions/apaquarto/apafloatstoend.lua | 10 +- _extensions/apaquarto/crossrefprefix.lua | 11 ++- _extensions/apaquarto/frontmatter.lua | 13 ++- _extensions/apaquarto/typst/formattypst.lua | 94 +++++++++++-------- _extensions/apaquarto/typst/typst-show.typ | 4 +- .../apaquarto/typst/typst-template.typ | 39 +++++++- changelog.qmd | 16 +++- docs/changelog.html | 22 ++++- docs/installation.html | 2 +- docs/search.json | 2 +- docs/writing.html | 6 +- example.qmd | 14 ++- 15 files changed, 175 insertions(+), 64 deletions(-) diff --git a/Makefile b/Makefile index bb921d9..e3adddd 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/_extensions/apaquarto/_extension.yml b/_extensions/apaquarto/_extension.yml index 742dacb..73111bb 100644 --- a/_extensions/apaquarto/_extension.yml +++ b/_extensions/apaquarto/_extension.yml @@ -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: diff --git a/_extensions/apaquarto/apaafternote.lua b/_extensions/apaquarto/apaafternote.lua index 12acd2e..158fc80 100644 --- a/_extensions/apaquarto/apaafternote.lua +++ b/_extensions/apaquarto/apaafternote.lua @@ -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 diff --git a/_extensions/apaquarto/apafloatstoend.lua b/_extensions/apaquarto/apafloatstoend.lua index cfb68d7..0aa23a4 100644 --- a/_extensions/apaquarto/apafloatstoend.lua +++ b/_extensions/apaquarto/apafloatstoend.lua @@ -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 @@ -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 diff --git a/_extensions/apaquarto/crossrefprefix.lua b/_extensions/apaquarto/crossrefprefix.lua index 4c61892..f65b717 100644 --- a/_extensions/apaquarto/crossrefprefix.lua +++ b/_extensions/apaquarto/crossrefprefix.lua @@ -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 @@ -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 diff --git a/_extensions/apaquarto/frontmatter.lua b/_extensions/apaquarto/frontmatter.lua index f9c7bd2..90b008f 100644 --- a/_extensions/apaquarto/frontmatter.lua +++ b/_extensions/apaquarto/frontmatter.lua @@ -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}) @@ -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) @@ -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 { diff --git a/_extensions/apaquarto/typst/formattypst.lua b/_extensions/apaquarto/typst/formattypst.lua index 5633425..8580f77 100644 --- a/_extensions/apaquarto/typst/formattypst.lua +++ b/_extensions/apaquarto/typst/formattypst.lua @@ -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 \ No newline at end of file + } +} \ No newline at end of file diff --git a/_extensions/apaquarto/typst/typst-show.typ b/_extensions/apaquarto/typst/typst-show.typ index ddb1837..131d532 100644 --- a/_extensions/apaquarto/typst/typst-show.typ +++ b/_extensions/apaquarto/typst/typst-show.typ @@ -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)$ diff --git a/_extensions/apaquarto/typst/typst-template.typ b/_extensions/apaquarto/typst/typst-template.typ index 976e22e..b7c0599 100644 --- a/_extensions/apaquarto/typst/typst-template.typ +++ b/_extensions/apaquarto/typst/typst-template.typ @@ -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()] ) ) @@ -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 @@ -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 => [ diff --git a/changelog.qmd b/changelog.qmd index d61c2d2..9d2b15d 100644 --- a/changelog.qmd +++ b/changelog.qmd @@ -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) diff --git a/docs/changelog.html b/docs/changelog.html index e0b4c3b..3a0585f 100644 --- a/docs/changelog.html +++ b/docs/changelog.html @@ -134,6 +134,7 @@

On this page