Have pandoc footnotes at bottom of page or section in HTML?
Asked Answered
C

3

10

I've started using inline footnotes in Markdown:

Some text^[an aside here]. More text.

When I use pandoc to export to HTML they appear at the end of the whole document, but in PDF they appear at the end of the page. I prefer them at the end of the page, and I'm wondering if there is a way to get them that way in HTML?

I realize end-of-page would get complicated with HTML; end of section would work just as well for me. In fact, putting them at end of the section in the PDF, instead of end of the page, might also be useful. (I've tried putting --- as a section break, but the footnotes still end up at the end of the document.)

(I've also tried making the pdf, then pdf2html, which kind of works but is really hard on the eyes. Pandoc doesn't seem to support pdf to html, I get "Cannot decode byte '\xd0' ...")


(This is not a duplicate of: Generate inline rather than list-style footnotes in Pandoc Markdown output? That question is about the way footnotes are handled when moving to markdown format, from another format.)

Coattail answered 31/3, 2016 at 10:22 Comment(1)
Possible duplicate of Generate inline rather than list-style footnotes in Pandoc Markdown output?Rodenhouse
I
1

No clue how to do this for a (printed) page (I'm looking for that myself), but for footnotes by section, couldn't you just break the input document into the sections before you pass them to pandoc, and then reassemble the parts afterwards?

E.g. suppose your markdown file looks like this (myfile.md):

Some text with a footnote.^[First note.]
----
Some more text with a footnote.^[Second note.]
----
And a third passage.^[Third note.]

Then you could use, e.g., the following PHP script (though I'm sure there a million ways to do this), i.e., pandoc-sections.php.

<?php

$input_file = $argv[1];

if (!file_exists($input_file)) {
    exit('File not found.');
}

$chunks = preg_split('/\n-----*\n/',file_get_contents($input_file));

for ($i=0; $i<count($chunks); $i++) {
    $chunk = $chunks[$i];
    $idprefix = "section" . ($i+1);

    $descriptorspec = array(
      0 => array("pipe", "r"), // stdin
      1 => array("pipe", "w"), // stdout 
      2 => array("pipe", "w")  // stderr
    );

    $pandoc_process = proc_open('pandoc --id-prefix=' . 
         $idprefix, $descriptorspec, $pipes);

    if (is_resource($pandoc_process)) {
      fwrite($pipes[0], $chunk);
      fclose($pipes[0]);
      echo stream_get_contents($pipes[1]);
      fclose($pipes[1]);
      fclose($pipes[2]);
      proc_close($pandoc_process);
   }

}    

?>

Then run

php pandoc-sections.php myfile.md

And you get:

<p>Some text with a footnote.<a href="#section1fn1" class="footnote-ref" id="section1fnref1"><sup>1</sup></a></p>
<section class="footnotes">
<hr />
<ol>
<li id="section1fn1"><p>First note.<a href="#section1section1fnref1" class="footnote-back">↩</a></p></li>
</ol>
</section>
<p>Some more text with a footnote.<a href="#section2fn1" class="footnote-ref" id="section2fnref1"><sup>1</sup></a></p>
<section class="footnotes">
<hr />
<ol>
<li id="section2fn1"><p>Second note.<a href="#section2section2fnref1" class="footnote-back">↩</a></p></li>
</ol>
</section>
<p>And a third passage.<a href="#section3fn1" class="footnote-ref" id="section3fnref1"><sup>1</sup></a></p>
<section class="footnotes">
<hr />
<ol>
<li id="section3fn1"><p>Third note.<a href="#section3section3fnref1" class="footnote-back">↩</a></p></li>
</ol>
</section>

Adapt as needed.

Interpellation answered 9/1, 2018 at 21:56 Comment(1)
Hey, I just exposed a bug with how pandoc handles backlinks when the "--id-prefix" option is used. I'll file a bug report. They're very quick in fixing things.Interpellation
F
1

As of pandoc v3.1.12.3, for HTML output, footnotes can be placed at section's end with the command line option --reference-location=section. Example:

[input.md]

# Lorem

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum efficitur
neque id ante vestibulum, at ultrices massa ultricies. ^[Nunc ut nibh
elementum, accumsan purus at, iaculis eros.] Sed aliquam est porttitor ligula
ornare, in auctor massa malesuada. Quisque sodales odio ornare placerat
lobortis.

Fusce sit amet egestas dolor, sit amet venenatis ipsum. Aliquam nec mi non
felis pellentesque fermentum.  ^[Aenean consectetur ligula in nibh imperdiet
cursus. Ut vitae malesuada lacus, placerat finibus ipsum.] Interdum et
malesuada fames ac ante ipsum primis in faucibus. Sed mollis porttitor est, et
convallis quam elementum tempor.

# Ipsum

Suspendisse et felis odio. Nullam porttitor
justo id mauris gravida vulputate. ^[Fusce iaculis a nibh eleifend
faucibus.] Vivamus suscipit, felis et malesuada faucibus, est ante facilisis
urna, molestie tempus sapien ligula id libero. ^[Vivamus vel orci sit amet risus sodales
sodales nec dapibus urna. Sed imperdiet orci ut elit blandit, id feugiat velit
vulputate.] Sed eleifend ultricies feugiat.

Call:

$ pandoc --standalone --reference-location=section --from markdown --to html5 -o output.html input.md

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>
  <meta charset="utf-8" />
  <meta name="generator" content="pandoc" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
  <title>input</title>
  <style>
    html {
      color: #1a1a1a;
      background-color: #fdfdfd;
    }
    body {
      margin: 0 auto;
      max-width: 36em;
      padding-left: 50px;
      padding-right: 50px;
      padding-top: 50px;
      padding-bottom: 50px;
      hyphens: auto;
      overflow-wrap: break-word;
      text-rendering: optimizeLegibility;
      font-kerning: normal;
    }
    @media (max-width: 600px) {
      body {
        font-size: 0.9em;
        padding: 12px;
      }
      h1 {
        font-size: 1.8em;
      }
    }
    @media print {
      html {
        background-color: white;
      }
      body {
        background-color: transparent;
        color: black;
        font-size: 12pt;
      }
      p, h2, h3 {
        orphans: 3;
        widows: 3;
      }
      h2, h3, h4 {
        page-break-after: avoid;
      }
    }
    p {
      margin: 1em 0;
    }
    a {
      color: #1a1a1a;
    }
    a:visited {
      color: #1a1a1a;
    }
    img {
      max-width: 100%;
    }
    svg {
      height: auto;
      max-width: 100%;
    }
    h1, h2, h3, h4, h5, h6 {
      margin-top: 1.4em;
    }
    h5, h6 {
      font-size: 1em;
      font-style: italic;
    }
    h6 {
      font-weight: normal;
    }
    ol, ul {
      padding-left: 1.7em;
      margin-top: 1em;
    }
    li > ol, li > ul {
      margin-top: 0;
    }
    blockquote {
      margin: 1em 0 1em 1.7em;
      padding-left: 1em;
      border-left: 2px solid #e6e6e6;
      color: #606060;
    }
    code {
      font-family: Menlo, Monaco, Consolas, 'Lucida Console', monospace;
      font-size: 85%;
      margin: 0;
      hyphens: manual;
    }
    pre {
      margin: 1em 0;
      overflow: auto;
    }
    pre code {
      padding: 0;
      overflow: visible;
      overflow-wrap: normal;
    }
    .sourceCode {
     background-color: transparent;
     overflow: visible;
    }
    hr {
      background-color: #1a1a1a;
      border: none;
      height: 1px;
      margin: 1em 0;
    }
    table {
      margin: 1em 0;
      border-collapse: collapse;
      width: 100%;
      overflow-x: auto;
      display: block;
      font-variant-numeric: lining-nums tabular-nums;
    }
    table caption {
      margin-bottom: 0.75em;
    }
    tbody {
      margin-top: 0.5em;
      border-top: 1px solid #1a1a1a;
      border-bottom: 1px solid #1a1a1a;
    }
    th {
      border-top: 1px solid #1a1a1a;
      padding: 0.25em 0.5em 0.25em 0.5em;
    }
    td {
      padding: 0.125em 0.5em 0.25em 0.5em;
    }
    header {
      margin-bottom: 4em;
      text-align: center;
    }
    #TOC li {
      list-style: none;
    }
    #TOC ul {
      padding-left: 1.3em;
    }
    #TOC > ul {
      padding-left: 0;
    }
    #TOC a:not(:hover) {
      text-decoration: none;
    }
    code{white-space: pre-wrap;}
    span.smallcaps{font-variant: small-caps;}
    div.columns{display: flex; gap: min(4vw, 1.5em);}
    div.column{flex: auto; overflow-x: auto;}
    div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
    /* The extra [class] is a hack that increases specificity enough to
       override a similar rule in reveal.js */
    ul.task-list[class]{list-style: none;}
    ul.task-list li input[type="checkbox"] {
      font-size: inherit;
      width: 0.8em;
      margin: 0 0.8em 0.2em -1.6em;
      vertical-align: middle;
    }
    .display.math{display: block; text-align: center; margin: 0.5rem auto;}
  </style>
</head>
<body>
<h1 id="lorem">Lorem</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum
efficitur neque id ante vestibulum, at ultrices massa ultricies. <a
href="#fn1" class="footnote-ref" id="fnref1"
role="doc-noteref"><sup>1</sup></a> Sed aliquam est porttitor ligula
ornare, in auctor massa malesuada. Quisque sodales odio ornare placerat
lobortis.</p>
<p>Fusce sit amet egestas dolor, sit amet venenatis ipsum. Aliquam nec
mi non felis pellentesque fermentum. <a href="#fn2" class="footnote-ref"
id="fnref2" role="doc-noteref"><sup>2</sup></a> Interdum et malesuada
fames ac ante ipsum primis in faucibus. Sed mollis porttitor est, et
convallis quam elementum tempor.</p>
<aside id="footnotes" class="footnotes footnotes-end-of-section"
role="doc-footnote">
<hr />
<ol>
<li id="fn1"><p>Nunc ut nibh elementum, accumsan purus at, iaculis
eros.<a href="#fnref1" class="footnote-back"
role="doc-backlink">↩︎</a></p></li>
<li id="fn2"><p>Aenean consectetur ligula in nibh imperdiet cursus. Ut
vitae malesuada lacus, placerat finibus ipsum.<a href="#fnref2"
class="footnote-back" role="doc-backlink">↩︎</a></p></li>
</ol>
</aside>
<h1 id="ipsum">Ipsum</h1>
<p>Suspendisse et felis odio. Nullam porttitor justo id mauris gravida
vulputate. <a href="#fn3" class="footnote-ref" id="fnref3"
role="doc-noteref"><sup>3</sup></a> Vivamus suscipit, felis et malesuada
faucibus, est ante facilisis urna, molestie tempus sapien ligula id
libero. <a href="#fn4" class="footnote-ref" id="fnref4"
role="doc-noteref"><sup>4</sup></a> Sed eleifend ultricies feugiat.</p>
<aside id="footnotes-2" class="footnotes footnotes-end-of-section"
role="doc-footnote">
<hr />
<ol start="3">
<li id="fn3"><p>Fusce iaculis a nibh eleifend faucibus.<a href="#fnref3"
class="footnote-back" role="doc-backlink">↩︎</a></p></li>
<li id="fn4"><p>Vivamus vel orci sit amet risus sodales sodales nec
dapibus urna. Sed imperdiet orci ut elit blandit, id feugiat velit
vulputate.<a href="#fnref4" class="footnote-back"
role="doc-backlink">↩︎</a></p></li>
</ol>
</aside>
</body>
</html>
Frulla answered 17/5 at 14:27 Comment(0)
R
0

Since PDF output is through LaTeX, you can achieve this using the endnotes package. It shouldn't be necessary to write your own pandoc LaTeX template neither, instead, at the beginning of your Markdown document, write

---
header-includes: |
    \includepackage{endnotes}
    \let\footnote=\endnote
---

and at the end of your Markdown document where you want all footnotes, add \theendnotes.

I haven't tried this directly, but it works in my custom pandoc template for LaTeX.

Refinery answered 8/4 at 13:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.