8000 Adding new features to the Typst template by christopherkenny · Pull Request #9970 · jgm/pandoc · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Adding new features to the Typst template #9970

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion MANUAL.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2728,7 +2728,7 @@ Currently the following pipes are predefined:
documents

`abstract-title`
: title of abstract, currently used only in HTML, EPUB, and docx.
: title of abstract, currently used only in HTML, EPUB, docx, and Typst.
This will be set automatically to a localized value,
depending on `lang`, but can be manually overridden.

Expand Down Expand Up @@ -3391,6 +3391,19 @@ The `--css` option also affects the output.
`columns`
: Number of columns for body text.

`thanks`
: contents of acknowledgments footnote after document title

`mathfont`, `codefont`
: Name of system font to use for math and code, respectively.

`linestretch`
: adjusts line spacing, e.g. `1.25`, `1.5`

`linkcolor`, `filecolor`, `citecolor`
: color for external links, internal links, and citation links,
respectively: expects a hexadecimal color code

### Variables for ms

`fontfamily`
Expand Down
24 changes: 24 additions & 0 deletions data/templates/default.typst
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,15 @@ $endif$
$if(region)$
region: "$region$",
$endif$
$if(abstract-title)$
abstract-title: [$abstract-title$],
$endif$
$if(abstract)$
abstract: [$abstract$],
$endif$
$if(thanks)$
thanks: [$thanks$],
$endif$
$if(margin)$
margin: ($for(margin/pairs)$$margin.key$: $margin.value$,$endfor$),
$endif$
Expand All @@ -86,10 +92,28 @@ $endif$
$if(fontsize)$
fontsize: $fontsize$,
$endif$
$if(mathfont)$
mathfont: ($for(mathfont)$"$mathfont$",$endfor$),
$endif$
$if(codefont)$
codefont: ($for(codefont)$"$codefont$",$endfor$),
$endif$
Comment on lines +95 to +100
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious why you used $for$ here. mathfont would normally be expected to be a string in pandoc templates.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit of an odd one, but Typst uses a vector of fonts because it automatically falls back to the next font if one is not found. This use of $for$ keeps that idea, where you might set (for example) EB Garamond and then Garamond. If EB Garamond is downloaded, then the template uses that. If not, it falls back to the (less featured, but more common) Garamond. This goes to the design choice that Typst will cycle through all the fonts chosen until it finds one available or ends up back at the default.

$if(linestretch)$
linestretch: $linestretch$,
$endif$
$if(section-numbering)$
sectionnumbering: "$section-numbering$",
$endif$
pagenumbering: $if(page-numbering)$"$page-numbering$"$else$none$endif$,
$if(linkcolor)$
linkcolor: [$linkcolor$],
$endif$
$if(citecolor)$
citecolor: [$citecolor$],
$endif$
Comment on lines +111 to +113
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works in content like this, with the escaped #?

citecolor: [\#ffffff]

You've tested I assume?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can confirm that this works, as I'm currently using it in a custom template. If there's something that you don't like about it, happy to brainstorm alternatives.

$if(filecolor)$
filecolor: [$filecolor$],
$endif$
cols: $if(columns)$$columns$$else$1$endif$,
doc,
)
Expand Down
100 changes: 63 additions & 37 deletions data/templates/template.typst
Original file line number Diff line number Diff line change
Expand Up @@ -15,74 +15,100 @@
authors: (),
keywords: (),
date: none,
abstract-title: none,
abstract: none,
thanks: none,
cols: 1,
margin: (x: 1.25in, y: 1.25in),
paper: "us-letter",
lang: "en",
region: "US",
font: (),
fontsize: 11pt,
mathfont: none,
codefont: none,
linestretch: 1,
sectionnumbering: none,
linkcolor: none,
citecolor: none,
filecolor: none,
pagenumbering: "1",
doc,
) = {
set document(
title: title,
author: authors.map(author => content-to-string(author.name)),
author: authors.map(author => content-to-string(author.name)).join(", ", last: " & "),
keywords: keywords,
)
set page(
paper: paper,
margin: margin,
numbering: pagenumbering,
columns: cols,
)
set par(justify: true)
)

set par(
justify: true,
leading: linestretch * 0.65em
)
Comment on lines +51 to +52
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does the leading do here, and why is it needed with linestretch? Is the result the same as Typst's defaults when the default linestretch is used?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leading sets the space between lines. Here, mixing it with linestretch ensures that a value of 1 * 0.65em = 0.65em or Typst's default single-spaced. For double spacing, then 1.3 = 2 * 0.65em. Here, this setting just makes it explicit so that setting 1 for single, etc, all map onto what is expected.

set text(lang: lang,
region: region,
font: font,
size: fontsize)

show math.equation: set text(font: mathfont) if mathfont != none
show raw: set text(font: codefont) if codefont != none

set heading(numbering: sectionnumbering)

place(top, float: true, scope: "parent", clearance: 4mm)[
#if title != none {
align(center)[#block(inset: 2em)[
#text(weight: "bold", size: 1.5em)[#title]
#(if subtitle != none {
parbreak()
text(weight: "bold", size: 1.25em)[#subtitle]
})
]]
show link: set text(fill: rgb(content-to-string(linkcolor))) if linkcolor != none
show ref: set text(fill: rgb(content-to-string(citecolor))) if citecolor != none
show link: this => {
if filecolor != none and type(this.dest) == label {
text(this, fill: rgb(content-to-string(filecolor)))
}
}

#if authors != none and authors != [] {
let count = authors.len()
let ncols = calc.min(count, 3)
grid(
columns: (1fr,) * ncols,
row-gutter: 1.5em,
..authors.map(author =>
align(center)[
#author.name \
#author.affiliation \
#author.email
]
block(below: 4mm)[
#if title != none {
align(center)[#block(inset: 2em)[
#text(weight: "bold", size: 1.5em)[#title #if thanks != none {
footnote(thanks, numbering: "*")
counter(footnote).update(n => n - 1)
}]
#(
if subtitle != none {
parbreak()
text(weight: "bold", size: 1.25em)[#subtitle]
}
)
]]
}

#if authors != none and authors != [] {
let count = authors.len()
let ncols = calc.min(count, 3)
grid(
columns: (1fr,) * ncols,
row-gutter: 1.5em,
..authors.map(author => align(center)[
#author.name \
#author.affiliation \
#author.email
])
)
)
}
}

#if date != none {
align(center)[#block(inset: 1em)[
#date
]]
}
#if date != none {
align(center)[#block(inset: 1em)[
#date
]]
}

#if abstract != none {
block(inset: 2em)[
#text(weight: "semibold")[Abstract] #h(1em) #abstract
]
}
#if abstract != none {
block(inset: 2em)[
#text(weight: "semibold")[#abstract-title] #h(1em) #abstract
]
}
]

doc
Expand Down
100 changes: 63 additions & 37 deletions test/writer.typst
Original file line number Diff line number Diff line change
Expand Up @@ -39,74 +39,100 @@
authors: (),
keywords: (),
date: none,
abstract-title: none,
abstract: none,
thanks: none,
cols: 1,
margin: (x: 1.25in, y: 1.25in),
paper: "us-letter",
lang: "en",
region: "US",
font: (),
fontsize: 11pt,
mathfont: none,
codefont: none,
linestretch: 1,
sectionnumbering: none,
linkcolor: none,
citecolor: none,
filecolor: none,
pagenumbering: "1",
doc,
) = {
set document(
title: title,
author: authors.map(author => content-to-string(author.name)),
author: authors.map(author => content-to-string(author.name)).join(", ", last: " & "),
keywords: keywords,
)
set page(
paper: paper,
margin: margin,
numbering: pagenumbering,
columns: cols,
)
set par(justify: true)
)

set par(
justify: true,
leading: linestretch * 0.65em
)
set text(lang: lang,
region: region,
font: font,
size: fontsize)

show math.equation: set text(font: mathfont) if mathfont != none
show raw: set text(font: codefont) if codefont != none

set heading(numbering: sectionnumbering)

place(top, float: true, scope: "parent", clearance: 4mm)[
#if title != none {
align(center)[#block(inset: 2em)[
#text(weight: "bold", size: 1.5em)[#title]
#(if subtitle != none {
parbreak()
text(weight: "bold", size: 1.25em)[#subtitle]
})
]]
show link: set text(fill: rgb(content-to-string(linkcolor))) if linkcolor != none
show ref: set text(fill: rgb(content-to-string(citecolor))) if citecolor != none
show link: this => {
if filecolor != none and type(this.dest) == label {
text(this, fill: rgb(content-to-string(filecolor)))
}
}

#if authors != none and authors != [] {
let count = authors.len()
let ncols = calc.min(count, 3)
grid(
columns: (1fr,) * ncols,
row-gutter: 1.5em,
..authors.map(author =>
align(center)[
#author.name \
#author.affiliation \
#author.email
]
block(below: 4mm)[
#if title != none {
align(center)[#block(inset: 2em)[
#text(weight: "bold", size: 1.5em)[#title #if thanks != none {
footnote(thanks, numbering: "*")
counter(footnote).update(n => n - 1)
}]
#(
if subtitle != none {
parbreak()
text(weight: "bold", size: 1.25em)[#subtitle]
}
)
]]
}

#if authors != none and authors != [] {
let count = authors.len()
let ncols = calc.min(count, 3)
grid(
columns: (1fr,) * ncols,
row-gutter: 1.5em,
..authors.map(author => align(center)[
#author.name \
#author.affiliation \
#author.email
])
)
)
}
}

#if date != none {
align(center)[#block(inset: 1em)[
#date
]]
}
#if date != none {
align(center)[#block(inset: 1em)[
#date
]]
}

#if abstract != none {
block(inset: 2em)[
#text(weight: "semibold")[Abstract] #h(1em) #abstract
]
}
#if abstract != none {
block(inset: 2em)[
#text(weight: "semibold")[#abstract-title] #h(1em) #abstract
]
}
]

doc
Expand Down
Loading
0