lierdakil / pandoc-crossref

Pandoc filter for cross-references
https://lierdakil.github.io/pandoc-crossref/
GNU General Public License v2.0
911 stars 72 forks source link

Reference no longer correctly displayed when combining with `minted.lua` #379

Closed amine-aboufirass closed 1 year ago

amine-aboufirass commented 1 year ago

As a follow up to my comment to https://github.com/lierdakil/pandoc-crossref/issues/29 I'm now trying very hard to combine three filters into my workflow:

The reason I need to combine these filters is that pandoc-crossref does not provide the following capabilities for listings:

I've already had an extensive discussion with Bastien Dumont from the pandoc google group and was not able to find a solution. I was however able to boil down my problem a bit further.

Here are the files I'm working with:

test.md

---
header-includes:
- \usepackage{minted}
- \makeatletter
- \let\listoflistings\@undefined
- \makeatother
- \definecolor{bg}{rgb}{0.85, 0.85, 0.85}
- \setminted{breaklines=true, bgcolor=bg, breakanywhere}
---

referencing [@Lst:test]

~~~{#lst:test .cs caption=test include=test.py}
~~~

test.py

def f(x):
    s = "908df0sdfdsfsd78g89f7g98fd7gh98fdhfd7h98df7h98dfh99h8dfa897hf9d7h98fd7hf98dh"
    return x

Compilation

I found out that if I run the filters in specific order and I include the header-includes shown in test.md I can at the very least get the LaTeX file to compile with no errors.

pandoc -s -L include-code-files.lua -F pandoc-crossref -L minted.lua --from markdown --to latex test-combined.md -o test.tex latexmk --shell-escape -pdflatex test.tex

Result

The reference is not recognized, it looks like minted.lua removes the \label associated with the codelisting environment defined by pandoc-crossref.

image

Question

How do I get the reference to still work when adding minted.lua to the Pandoc call?

lierdakil commented 1 year ago

Try using pandoc's native listings maybe? pandoc ... -Mlistings --listings ...

Pandoc-crossref does some horrible hacks with listings in LaTeX, if you're going to process the file further, you'll want to avoid those.

amine-aboufirass commented 1 year ago

I did:

pandoc -s -Mlistings --listings -L include-code-files.lua -F pandoc-crossref -L minted.lua --from markdown --to latex test-combined.md -o test.tex
latexmk --shell-escape -pdflatex test.tex

But that just results in:

image

That's not exactly what I'm looking for.

lierdakil commented 1 year ago

I see. Well, I've taken a look at what minted.lua does, and it just unconditionally strips the code block identifier altogether. As far as I'm concerned, that's an issue with minted.lua, but there is a relatively straightforward bodge I believe:

replace https://github.com/pandoc/lua-filters/blob/40a1a0b6e3c5b3347aaecb0ad697368e16289f86/minted/minted.lua#L408 with something like

      "\\label{%s}\\begin{minted}[%s]{%s}\n%s\n\\end{minted}",
      block.identifier,

This won't work in general, as in general you'd have to check that identifier is not empty and only then add the label. And I'm not sure whether it's better to put the label inside or outside minted environment.

Disclaimer: I'm not familiar with minted and I didn't test any of this, but on paper it seems like it should work.

amine-aboufirass commented 1 year ago

@lierdakil sounds good. The adjustment you propose to the Lua file does work. I made a pull request in the official repo for minted.lua https://github.com/pandoc/lua-filters/pull/264

Hopefully the maintainers can see the value of making these two filters play nice with each other and accept the change.