github / cmark-gfm

GitHub's fork of cmark, a CommonMark parsing and rendering library and program in C
Other
904 stars 173 forks source link

Implement footnotes for LaTeX output #314

Open yihui opened 1 year ago

yihui commented 1 year ago

Currently LaTeX output for footnotes is not implemented:

https://github.com/github/cmark-gfm/blob/c32ef78bae851cb83b7ad52d0fbff880acdcd44a/src/latex.c#L447-L449

I don't have much expertise on C but I wonder if the team is interested in getting it implemented. Basically what we need to do is to add the footnote content to \footnote{} in the case of CMARK_NODE_FOOTNOTE_REFERENCE, and output nil in the case of CMARK_NODE_FOOTNOTE_DEFINITION.

  case CMARK_NODE_FOOTNOTE_DEFINITION:
    break;
  case CMARK_NODE_FOOTNOTE_REFERENCE:
    if (entering) {
      LIT("\\footnote{");
      // How can I get the footnote content here?
    } else {
      LIT("}");
    }
    break;

Thanks!

yihui commented 1 month ago

HTML (if the footnotes extension is enabled):

<p>This is a test<sup class="footnote-ref"><a href="#fn-1" id="fnref-1" data-footnote-ref>1</a></sup></p>
<section class="footnotes" data-footnotes>
<ol>
<li id="fn-1">
<p>Nothing special. <a href="#fnref-1" class="footnote-backref" data-footnote-backref data-footnote-backref-idx="1" aria-label="Back to reference 1">↩</a></p>
</li>
</ol>
</section>

LaTeX (not implemented):

This is a test\footnote{Nothing special.}
yoonghm commented 1 month ago

https://github.com/github/cmark-gfm/pull/362 have a solution and I have tested it that it works except that it could not handle footnote with non-numerical value.

abc.md:

---
title: "footnote"
---

- A footnote[^1]
- A duplicated[^1]
- Another one[^2]
- A non-numeric[^tim]

[^1]: Footnote 1
[^2]: Footnote 2 with a line
      break
[^tim]: Time is essential

Using the command: cmark-gfm.exe --extension footnotes --to latex abc.md, we could get

\begin{center}\rule{0.5\linewidth}{\linethickness}\end{center}

\subsection{title: \textquotedbl{}footnote\textquotedbl{}}

\begin{itemize}
\item A footnote\footnote{1}

\item A duplicated\footnote{1}

\item Another one\footnote{2}

\item A non-numeric\footnote{3}

\end{itemize}

\footnote{1Footnote 1

}\footnote{2Footnote 2 with a line
break

}\footnote{timTime is essential

}

So, from your example, LaTeX expects the footnote definition is in the body text, not at the end of the document. LaTeX will automatically generate reference numbers.

yihui commented 1 month ago

That will be a great step forward! I really wish Github could merge #362, although it appears to me that they don't pay much attention to bug reports or PRs from the community.