Skip to content

Commit

Permalink
latex custom crossref improvements: correct listof commands, spacing …
Browse files Browse the repository at this point in the history
…customization.
  • Loading branch information
cscheid committed Oct 22, 2023
1 parent 024a04d commit 861e3df
Show file tree
Hide file tree
Showing 9 changed files with 227 additions and 66 deletions.
35 changes: 27 additions & 8 deletions src/resources/editor/tools/vs-code.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12110,6 +12110,12 @@ var require_yaml_intelligence_resources = __commonJS({
object: {
description: "A custom cross reference type.",
closed: true,
required: [
"kind",
"prefix",
"name",
"ref-type"
],
properties: {
kind: {
enum: [
Expand All @@ -12127,19 +12133,30 @@ var require_yaml_intelligence_resources = __commonJS({
description: "The prefix used in captions when referencing this type."
}
},
"space-before-numbering": {
default: true,
boolean: {
description: "If false, use no space between crossref prefixes and numbering."
}
},
"ref-type": {
string: {
description: 'The prefix string used in references ("dia-", etc.) when referencing this type.'
}
},
"latex-env": {
string: {
description: "The name of the custom LaTeX environment that quarto will use to create this type of crossreferenceable object in LaTeX output."
description: "In LaTeX output, the name of the custom environment to be used."
}
},
"latex-list-of-file-extension": {
string: {
description: 'In LaTeX output, the extension of the auxiliary file used by LaTeX to collect names to be used in the custom "list of" command. If omitted, a string with prefix `lo` and suffix with the value of `ref-type` is used.'
}
},
"latex-list-of-name": {
"latex-list-of-description": {
string: {
description: 'The name of the custom LaTeX "list of" command that quarto will use to create this type of crossreferenceable object in LaTeX output.'
description: 'The description of the crossreferenceable object to be used in the title of the "list of" command. If unspecified, the field `name` is used.'
}
}
}
Expand Down Expand Up @@ -20294,9 +20311,11 @@ var require_yaml_intelligence_resources = __commonJS({
"The kind of cross reference (currently only \u201Cfloat\u201D is\nsupported).",
"The prefix used in rendered citations when referencing this type.",
"The prefix used in captions when referencing this type.",
"If false, use no space between crossref prefixes and numbering.",
"The prefix string used in references (\u201Cdia-\u201D, etc.) when referencing\nthis type.",
"The name of the custom LaTeX environment that quarto will use to\ncreate this type of crossreferenceable object in LaTeX output.",
"The name of the custom LaTeX \u201Clist of\u201D command that quarto will use\nto create this type of crossreferenceable object in LaTeX output.",
"In LaTeX output, the name of the custom environment to be used.",
"In LaTeX output, the extension of the auxiliary file used by LaTeX to\ncollect names to be used in the custom \u201Clist of\u201D command.",
"The description of the crossreferenceable object to be used in the\ntitle of the \u201Clist of\u201D command. If unspecified, the field\n<code>name</code> is used.",
"Use top level sections (H1) in this document as chapters.",
"The delimiter used between the prefix and the caption.",
"The title prefix used for figure captions.",
Expand Down Expand Up @@ -22152,12 +22171,12 @@ var require_yaml_intelligence_resources = __commonJS({
mermaid: "%%"
},
"handlers/mermaid/schema.yml": {
_internalId: 173366,
_internalId: 173650,
type: "object",
description: "be an object",
properties: {
"mermaid-format": {
_internalId: 173358,
_internalId: 173642,
type: "enum",
enum: [
"png",
Expand All @@ -22173,7 +22192,7 @@ var require_yaml_intelligence_resources = __commonJS({
exhaustiveCompletions: true
},
theme: {
_internalId: 173365,
_internalId: 173649,
type: "anyOf",
anyOf: [
{
Expand Down
35 changes: 27 additions & 8 deletions src/resources/editor/tools/yaml/web-worker.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 27 additions & 8 deletions src/resources/editor/tools/yaml/yaml-intelligence-resources.json
Original file line number Diff line number Diff line change
Expand Up @@ -5082,6 +5082,12 @@
"object": {
"description": "A custom cross reference type.",
"closed": true,
"required": [
"kind",
"prefix",
"name",
"ref-type"
],
"properties": {
"kind": {
"enum": [
Expand All @@ -5099,19 +5105,30 @@
"description": "The prefix used in captions when referencing this type."
}
},
"space-before-numbering": {
"default": true,
"boolean": {
"description": "If false, use no space between crossref prefixes and numbering."
}
},
"ref-type": {
"string": {
"description": "The prefix string used in references (\"dia-\", etc.) when referencing this type."
}
},
"latex-env": {
"string": {
"description": "The name of the custom LaTeX environment that quarto will use to create this type of crossreferenceable object in LaTeX output."
"description": "In LaTeX output, the name of the custom environment to be used."
}
},
"latex-list-of-file-extension": {
"string": {
"description": "In LaTeX output, the extension of the auxiliary file used by LaTeX to collect names to be used in the custom \"list of\" command. If omitted, a string with prefix `lo` and suffix with the value of `ref-type` is used."
}
},
"latex-list-of-name": {
"latex-list-of-description": {
"string": {
"description": "The name of the custom LaTeX \"list of\" command that quarto will use to create this type of crossreferenceable object in LaTeX output."
"description": "The description of the crossreferenceable object to be used in the title of the \"list of\" command. If unspecified, the field `name` is used."
}
}
}
Expand Down Expand Up @@ -13266,9 +13283,11 @@
"The kind of cross reference (currently only “float” is\nsupported).",
"The prefix used in rendered citations when referencing this type.",
"The prefix used in captions when referencing this type.",
"If false, use no space between crossref prefixes and numbering.",
"The prefix string used in references (“dia-”, etc.) when referencing\nthis type.",
"The name of the custom LaTeX environment that quarto will use to\ncreate this type of crossreferenceable object in LaTeX output.",
"The name of the custom LaTeX “list of” command that quarto will use\nto create this type of crossreferenceable object in LaTeX output.",
"In LaTeX output, the name of the custom environment to be used.",
"In LaTeX output, the extension of the auxiliary file used by LaTeX to\ncollect names to be used in the custom “list of” command.",
"The description of the crossreferenceable object to be used in the\ntitle of the “list of” command. If unspecified, the field\n<code>name</code> is used.",
"Use top level sections (H1) in this document as chapters.",
"The delimiter used between the prefix and the caption.",
"The title prefix used for figure captions.",
Expand Down Expand Up @@ -15124,12 +15143,12 @@
"mermaid": "%%"
},
"handlers/mermaid/schema.yml": {
"_internalId": 173366,
"_internalId": 173650,
"type": "object",
"description": "be an object",
"properties": {
"mermaid-format": {
"_internalId": 173358,
"_internalId": 173642,
"type": "enum",
"enum": [
"png",
Expand All @@ -15145,7 +15164,7 @@
"exhaustiveCompletions": true
},
"theme": {
"_internalId": 173365,
"_internalId": 173649,
"type": "anyOf",
"anyOf": [
{
Expand Down
67 changes: 53 additions & 14 deletions src/resources/filters/crossref/custom.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,29 @@ function initialize_custom_crossref_categories(meta)
-- luacov: enable
end
local keys = {
"default-caption-location",
"kind",
"name",
"prefix",
"ref-type",
"latex-env",
"latex-list-of-name"
["default-caption-location"] = function(v) return pandoc.utils.stringify(v) end,
["kind"] = function(v) return pandoc.utils.stringify(v) end,
["name"] = function(v) return pandoc.utils.stringify(v) end,
["prefix"] = function(v) return pandoc.utils.stringify(v) end,
["ref-type"] = function(v) return pandoc.utils.stringify(v) end,
["latex-env"] = function(v) return pandoc.utils.stringify(v) end,
["latex-list-of-file-extension"] = function(v) return pandoc.utils.stringify(v) end,
["latex-list-of-description"] = function(v) return pandoc.utils.stringify(v) end,
["space-before-numbering"] = function(v) return v end,
}
local obj_mapping = {
["default-caption-location"] = "default_caption_location",
["latex-env"] = "latex_env",
["latex-list-of-name"] = "latex_list_of_name",
["ref-type"] = "ref_type"
["latex-list-of-file-extension"] = "latex_list_of_file_extension",
["latex-list-of-description"] = "latex_list_of_description",
["ref-type"] = "ref_type",
["space-before-numbering"] = "space_before_numbering",
}
for _, v in ipairs(custom) do
local entry = {}
for _, key in ipairs(keys) do
for key, xform in pairs(keys) do
if v[key] ~= nil then
entry[key] = pandoc.utils.stringify(v[key])
entry[key] = xform(v[key])
end
end
if entry["default-caption-location"] == nil then
Expand All @@ -62,19 +66,54 @@ function initialize_custom_crossref_categories(meta)
local name = entry["name"]
local env_prefix = entry["prefix"]
local ref_type = entry["ref-type"]
local list_of_name = entry["latex-list-of-name"]
local list_of_name = entry["latex-list-of-file-extension"] or ("lo" .. ref_type)
local list_of_description = entry["latex-list-of-description"] or name
local space_before_numbering = entry["space-before-numbering"]
if space_before_numbering == nil then
space_before_numbering = true
end

-- FIXME do we need different "lop" extensions for each category?
-- we should be able to test this by creating a document with listings and diagrams

inject(
usePackage("float") .. "\n" ..
"\\floatstyle{plain}\n" ..
"\\@ifundefined{c@chapter}{\\newfloat{" .. env_name .. "}{h}{lop}}{\\newfloat{" .. env_name .. "}{h}{lop}[chapter]}\n" ..
"\\@ifundefined{c@chapter}{\\newfloat{" .. env_name .. "}{h}{" .. list_of_name .. "}}{\\newfloat{" .. env_name .. "}{h}{" .. list_of_name .. "}[chapter]}\n" ..
"\\floatname{".. env_name .. "}{" .. titleString(ref_type, env_prefix) .. "}\n"
)

-- FIXME this is a bit of hack for the case of custom categories with
-- space-before-numbering: false
--
-- the real unlock here is the custom ref command, which we should
-- eventually just make extensible entirely by the user
--
-- and we should probably be using cleveref instead of hyperref

if not space_before_numbering and name:match(" ") then
-- extract last word from name
local last_word = name:match("([^ ]+)$")
local first_words = name:sub(1, #name - #last_word - 1)
local custom_cmd_name = "quarto" .. ref_type .. "ref"
local ref_command = "\\newcommand*\\" .. custom_cmd_name .. "[1]{" .. first_words .. " \\hyperref[#1]{" .. last_word .. "\\ref{#1}}}"
inject(ref_command)

-- mark crossref category as having a custom ref command
-- so we can use it in the rendering
crossref.categories.by_ref_type[ref_type].custom_ref_command = custom_cmd_name


-- inject the caption package includes here because they need to appear before DeclareCaptionFormat
inject(usePackage("caption"))
-- also declare a custom caption format in this case, so caption
-- format also skips spaces:
inject("\\DeclareCaptionLabelFormat{" .. custom_cmd_name .. "labelformat}{#1#2}")
inject("\\captionsetup[" .. env_name .. "]{labelformat=" .. custom_cmd_name .. "labelformat}")
end

inject(
"\\newcommand*\\listof" .. env_name .. "s{\\listof{" .. env_name .. "}{" .. listOfTitle(list_of_name, "List of " .. name .. "s") .. "}}\n"
"\\newcommand*\\listof" .. env_name .. "s{\\listof{" .. env_name .. "}{" .. listOfTitle(list_of_name, "List of " .. list_of_description .. "s") .. "}}\n"
)
end)
end
Expand Down
17 changes: 10 additions & 7 deletions src/resources/filters/crossref/format.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@ function titleString(type, default)
return pandoc.utils.stringify(title(type, default))
end

function titlePrefix(type, default, order, with_delimiter)
if with_delimiter == nil then
with_delimiter = true
function titlePrefix(ref_type, default, order, with_title_delimiter)
if with_title_delimiter == nil then
with_title_delimiter = true
end

local prefix = title(type, default)
table.insert(prefix, nbspString())
tappend(prefix, numberOption(type, order))
if with_delimiter then
local prefix = title(ref_type, default)
local category = crossref.categories.by_ref_type[ref_type]
if category == nil or category.space_before_numbering ~= false then
table.insert(prefix, nbspString())
end
tappend(prefix, numberOption(ref_type, order))
if with_title_delimiter then
tappend(prefix, titleDelim())
table.insert(prefix, pandoc.Space())
end
Expand Down
Loading

0 comments on commit 861e3df

Please sign in to comment.