Skip to content
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

Can pandoc support figure reference in org-ref style ? #7133

Closed
jiucenglou opened this issue Mar 9, 2021 · 7 comments
Closed

Can pandoc support figure reference in org-ref style ? #7133

jiucenglou opened this issue Mar 9, 2021 · 7 comments

Comments

@jiucenglou
Copy link

Currently pandoc has basic support for the cite:citeKey syntax in .org file, which is the org-ref style of citing citations. It is thus possible for one to be able to use org & org-ref to write and use pandoc to export to multiple formats such as .pdf, .docx, and so forth. However, the syntax to refer to a figure is incompatible between pandoc and org-ref. Specifically, pandoc supports the [@Fig:XXX] syntax to refer to a figure in .org file, while org-ref uses org-links such as ref:XXX to utilize org-machinery. Could you consider make pandoc support the ref:XXX syntax to refer to a figure in .org file, which is the org-ref style of referring to figures ?

mwe_name.org

* Sample title

The Himalaya mountain is shown in the figure [[fig:himalaya]] and ref:fig:himalaya and [@fig:himalaya].

#+CAPTION: Himalaya mountain
#+name: fig:himalaya
[[./himalaya.jpg]]

native output

$ pandoc  --filter=pandoc-crossref --citeproc mwe_name.org -s -t native
WARNING: pandoc-crossref was compiled with pandoc 2.11.4 but is being run through 2.12. This is not supported. Strange things may (and likely will) happen silently.
Undefined cross-reference: fig:himalaya
Pandoc (Meta {unMeta = fromList [("autoEqnLabels",MetaBool False),("autoSectionLabels",MetaBool False),("ccsDelim",MetaInlines [Str ",",Space]),("ccsLabelSep",MetaInlines [Space,Str "\8212",Space]),("ccsTemplate",MetaInlines [Math DisplayMath "i",Math DisplayMath "ccsLabelSep",Math DisplayMath "t"]),("chapDelim",MetaInlines [Str "."]),("chapters",MetaBool False),("chaptersDepth",MetaString "1"),("codeBlockCaptions",MetaBool False),("cref",MetaBool False),("crossrefYaml",MetaString "pandoc-crossref.yaml"),("eqLabels",MetaString "arabic"),("eqnPrefix",MetaList [MetaInlines [Str "eq."],MetaInlines [Str "eqns."]]),("eqnPrefixTemplate",MetaInlines [Math DisplayMath "p",Str "\160",Math DisplayMath "i"]),("figLabels",MetaString "arabic"),("figPrefix",MetaList [MetaInlines [Str "fig."],MetaInlines [Str "figs."]]),("figPrefixTemplate",MetaInlines [Math DisplayMath "p",Str "\160",Math DisplayMath "i"]),("figureTemplate",MetaInlines [Math DisplayMath "figureTitle",Space,Math DisplayMath "i",Math DisplayMath "titleDelim",Space,Math DisplayMath "t"]),("figureTitle",MetaInlines [Str "Figure"]),("lastDelim",MetaInlines [Str ",",Space]),("linkReferences",MetaBool False),("listingTemplate",MetaInlines [Math DisplayMath "listingTitle",Space,Math DisplayMath "i",Math DisplayMath "titleDelim",Space,Math DisplayMath "t"]),("listingTitle",MetaInlines [Str "Listing"]),("listings",MetaBool False),("lofTitle",MetaBlocks [Header 1 ("",[],[]) [Str "List",Space,Str "of",Space,Str "Figures"]]),("lolTitle",MetaBlocks [Header 1 ("",[],[]) [Str "List",Space,Str "of",Space,Str "Listings"]]),("lotTitle",MetaBlocks [Header 1 ("",[],[]) [Str "List",Space,Str "of",Space,Str "Tables"]]),("lstLabels",MetaString "arabic"),("lstPrefix",MetaList [MetaInlines [Str "lst."],MetaInlines [Str "lsts."]]),("lstPrefixTemplate",MetaInlines [Math DisplayMath "p",Str "\160",Math DisplayMath "i"]),("nameInLink",MetaBool False),("numberSections",MetaBool False),("pairDelim",MetaInlines [Str ",",Space]),("rangeDelim",MetaInlines [Str "-"]),("refDelim",MetaInlines [Str ",",Space]),("refIndexTemplate",MetaInlines [Math DisplayMath "i",Math DisplayMath "suf"]),("secHeaderDelim",MetaInlines [Space]),("secHeaderTemplate",MetaInlines [Math DisplayMath "i",Math DisplayMath "secHeaderDelim[n]",Math DisplayMath "t"]),("secLabels",MetaString "arabic"),("secPrefix",MetaList [MetaInlines [Str "sec."],MetaInlines [Str "secs."]]),("secPrefixTemplate",MetaInlines [Math DisplayMath "p",Str "\160",Math DisplayMath "i"]),("sectionsDepth",MetaString "0"),("subfigGrid",MetaBool False),("subfigLabels",MetaString "alpha a"),("subfigureChildTemplate",MetaInlines [Math DisplayMath "i"]),("subfigureRefIndexTemplate",MetaInlines [Math DisplayMath "i",Math DisplayMath "suf",Space,Str "(",Math DisplayMath "s",Str ")"]),("subfigureTemplate",MetaInlines [Math DisplayMath "figureTitle",Space,Math DisplayMath "i",Math DisplayMath "titleDelim",Space,Math DisplayMath "t",Str ".",Space,Math DisplayMath "ccs"]),("tableEqns",MetaBool False),("tableTemplate",MetaInlines [Math DisplayMath "tableTitle",Space,Math DisplayMath "i",Math DisplayMath "titleDelim",Space,Math DisplayMath "t"]),("tableTitle",MetaInlines [Str "Table"]),("tblLabels",MetaString "arabic"),("tblPrefix",MetaList [MetaInlines [Str "tbl."],MetaInlines [Str "tbls."]]),("tblPrefixTemplate",MetaInlines [Math DisplayMath "p",Str "\160",Math DisplayMath "i"]),("titleDelim",MetaInlines [Str ":"])]})
[Header 1 ("sample",[],[]) [Str "Sample"]
,Para [Str "The",Space,Str "Himalaya",Space,Str "mountain",Space,Str "is",Space,Str "shown",Space,Str "in",Space,Str "the",Space,Str "figure",Space,Link ("",[],[]) [Str "fig:himalaya"] ("fig:himalaya",""),Space,Str "and",Space,Str "ref:fig:himalaya",Space,Str "and",Space,Str "fig.\160",Strong [Str "\191fig:himalaya?"],Str "."]
,Para [Image ("",[],[]) [Str "Himalaya",Space,Str "mountain"] ("./himalaya.jpg","fig:himalaya")]]
$
$ pandoc  --filter=pandoc-crossref --citeproc mwe_name.org -s -o mwe_name.docx
WARNING: pandoc-crossref was compiled with pandoc 2.11.4 but is being run through 2.12. This is not supported. Strange things may (and likely will) happen silently.
Undefined cross-reference: fig:himalaya
$

mwe_label.org

* Sample title

The Himalaya mountain is shown in the figure [[fig:himalaya]] and ref:fig:himalaya and [@fig:himalaya].

#+CAPTION: Himalaya mountain
#+label: fig:himalaya
[[./himalaya.jpg]]

native output

$ pandoc  --filter=pandoc-crossref --citeproc mwe_label.org -s -t native
WARNING: pandoc-crossref was compiled with pandoc 2.11.4 but is being run through 2.12. This is not supported. Strange things may (and likely will) happen silently.
Pandoc (Meta {unMeta = fromList [("autoEqnLabels",MetaBool False),("autoSectionLabels",MetaBool False),("ccsDelim",MetaInlines [Str ",",Space]),("ccsLabelSep",MetaInlines [Space,Str "\8212",Space]),("ccsTemplate",MetaInlines [Math DisplayMath "i",Math DisplayMath "ccsLabelSep",Math DisplayMath "t"]),("chapDelim",MetaInlines [Str "."]),("chapters",MetaBool False),("chaptersDepth",MetaString "1"),("codeBlockCaptions",MetaBool False),("cref",MetaBool False),("crossrefYaml",MetaString "pandoc-crossref.yaml"),("eqLabels",MetaString "arabic"),("eqnPrefix",MetaList [MetaInlines [Str "eq."],MetaInlines [Str "eqns."]]),("eqnPrefixTemplate",MetaInlines [Math DisplayMath "p",Str "\160",Math DisplayMath "i"]),("figLabels",MetaString "arabic"),("figPrefix",MetaList [MetaInlines [Str "fig."],MetaInlines [Str "figs."]]),("figPrefixTemplate",MetaInlines [Math DisplayMath "p",Str "\160",Math DisplayMath "i"]),("figureTemplate",MetaInlines [Math DisplayMath "figureTitle",Space,Math DisplayMath "i",Math DisplayMath "titleDelim",Space,Math DisplayMath "t"]),("figureTitle",MetaInlines [Str "Figure"]),("lastDelim",MetaInlines [Str ",",Space]),("linkReferences",MetaBool False),("listingTemplate",MetaInlines [Math DisplayMath "listingTitle",Space,Math DisplayMath "i",Math DisplayMath "titleDelim",Space,Math DisplayMath "t"]),("listingTitle",MetaInlines [Str "Listing"]),("listings",MetaBool False),("lofTitle",MetaBlocks [Header 1 ("",[],[]) [Str "List",Space,Str "of",Space,Str "Figures"]]),("lolTitle",MetaBlocks [Header 1 ("",[],[]) [Str "List",Space,Str "of",Space,Str "Listings"]]),("lotTitle",MetaBlocks [Header 1 ("",[],[]) [Str "List",Space,Str "of",Space,Str "Tables"]]),("lstLabels",MetaString "arabic"),("lstPrefix",MetaList [MetaInlines [Str "lst."],MetaInlines [Str "lsts."]]),("lstPrefixTemplate",MetaInlines [Math DisplayMath "p",Str "\160",Math DisplayMath "i"]),("nameInLink",MetaBool False),("numberSections",MetaBool False),("pairDelim",MetaInlines [Str ",",Space]),("rangeDelim",MetaInlines [Str "-"]),("refDelim",MetaInlines [Str ",",Space]),("refIndexTemplate",MetaInlines [Math DisplayMath "i",Math DisplayMath "suf"]),("secHeaderDelim",MetaInlines [Space]),("secHeaderTemplate",MetaInlines [Math DisplayMath "i",Math DisplayMath "secHeaderDelim[n]",Math DisplayMath "t"]),("secLabels",MetaString "arabic"),("secPrefix",MetaList [MetaInlines [Str "sec."],MetaInlines [Str "secs."]]),("secPrefixTemplate",MetaInlines [Math DisplayMath "p",Str "\160",Math DisplayMath "i"]),("sectionsDepth",MetaString "0"),("subfigGrid",MetaBool False),("subfigLabels",MetaString "alpha a"),("subfigureChildTemplate",MetaInlines [Math DisplayMath "i"]),("subfigureRefIndexTemplate",MetaInlines [Math DisplayMath "i",Math DisplayMath "suf",Space,Str "(",Math DisplayMath "s",Str ")"]),("subfigureTemplate",MetaInlines [Math DisplayMath "figureTitle",Space,Math DisplayMath "i",Math DisplayMath "titleDelim",Space,Math DisplayMath "t",Str ".",Space,Math DisplayMath "ccs"]),("tableEqns",MetaBool False),("tableTemplate",MetaInlines [Math DisplayMath "tableTitle",Space,Math DisplayMath "i",Math DisplayMath "titleDelim",Space,Math DisplayMath "t"]),("tableTitle",MetaInlines [Str "Table"]),("tblLabels",MetaString "arabic"),("tblPrefix",MetaList [MetaInlines [Str "tbl."],MetaInlines [Str "tbls."]]),("tblPrefixTemplate",MetaInlines [Math DisplayMath "p",Str "\160",Math DisplayMath "i"]),("titleDelim",MetaInlines [Str ":"])]})
[Header 1 ("sample",[],[]) [Str "Sample"]
,Para [Str "The",Space,Str "Himalaya",Space,Str "mountain",Space,Str "is",Space,Str "shown",Space,Str "in",Space,Str "the",Space,Str "figure",Space,Link ("",[],[]) [Str "fig:himalaya"] ("fig:himalaya",""),Space,Str "and",Space,Str "ref:fig:himalaya",Space,Str "and",Space,Str "fig.\160\&1."]
,Para [Image ("fig:himalaya",[],[]) [Str "Figure",Space,Str "1:",Space,Str "Himalaya",Space,Str "mountain"] ("./himalaya.jpg","fig:")]]
$
$ pandoc  --filter=pandoc-crossref --citeproc mwe_label.org -s -o mwe_label.docx
WARNING: pandoc-crossref was compiled with pandoc 2.11.4 but is being run through 2.12. This is not supported. Strange things may (and likely will) happen silently.
$
@tarleb
Copy link
Collaborator

tarleb commented Mar 9, 2021

Sorry, this is currently out of scope for pandoc. The figure support is part of pandoc-crossref, you could raise the issue there.

The problem is well suited for a (Lua) filter: a filter which matches on "ref" Str and Link elements, and converts them to Cite elements, would be only a couple of lines. Please reach out if you need help with this approach.

@tarleb
Copy link
Collaborator

tarleb commented Mar 9, 2021

To be more precise, this isn't really out of scope, but requires other features first. See also this long-standing issue: #813

@jiucenglou
Copy link
Author

The problem is well suited for a (Lua) filter: a filter which matches on "ref" Str and Link elements, and converts them to Cite elements, would be only a couple of lines. Please reach out if you need help with this approach.

Do you suggest that "ref:XXX" should be converted to "cite:XXX", which is for citation ? :D Could you help provide the Lua filter if possible ? Many thanks !

@tarleb
Copy link
Collaborator

tarleb commented Mar 9, 2021

Yes, that's basically it. We need to make sure that echo 'ref:foo' | pandoc -f org -t native -L some-filter.lua gives the same (or similar) result as echo '[@ref:foo]' | pandoc -f org -t native.

For Str, we'd match on the text content:

function Str (s)
  if s.text:match 'ref:.*' then
    return pandoc.Cite({}, {pandoc.Citation(s.text, "NormalCitation")})
  end
end

Converting links would be similar, but one would match on target instead. The filter would have to run before pandoc-crossref, so it must come first in the command.

Does this help?

@jiucenglou
Copy link
Author

jiucenglou commented Mar 10, 2021

Do you suggest that "ref:XXX" should be converted to "cite:XXX", which is for citation ?

Yes, that's basically it.

Could you explain your idea more ? The filter that you posted converts a reference to a figure to a citation, which citeproc will not be able to recognize. For example, using the MWE files in the first post pandoc -L some-filter.lua --filter=pandoc-crossref --citeproc mwe_label.org -s -o mwe_label.docx complains [WARNING] Citeproc: citation ref:fig:himalaya not found.

@tarleb
Copy link
Collaborator

tarleb commented Mar 10, 2021

Please try with this improved filter:

function Link (link)
  if link.target:match 'fig:.*' then
    return pandoc.Cite(
      link.content,
      {pandoc.Citation(link.target, "NormalCitation")}
    )
  end
end

@jiucenglou
Copy link
Author

Please try with this improved filter:

function Link (link)
  if link.target:match 'fig:.*' then
    return pandoc.Cite(
      link.content,
      {pandoc.Citation(link.target, "NormalCitation")}
    )
  end
end

Amazing ! The new filter recognizes the [[fig:himalaya]] syntax in the .org file. Thank you very much ! Thanks to your examples, the Pandoc Lua page, and the Lua manual on match, the ref:XXX syntax can also be recognized now :

function Str (s)
  if s.text:match 'ref:(.*)' then
    return pandoc.Cite({}, {pandoc.Citation(s.text:match 'ref:(.*)', "NormalCitation")})
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants