Home > Software design >  Cross references to headings with leading numbers in PDF
Cross references to headings with leading numbers in PDF

Time:12-02

I am using Pandoc to convert a markdown file to a PDF and I have some issues with creating references to headings with leading numbers.

Here is the code:


Take me to the [first paragraph](#1-paragraph) 

## 1 Paragraph

In the converted PDF the link does not work.

When I remove the leading number everything works fine.

So whats the correct way to link to this kind of headings?

CodePudding user response:

A good way to go about this is to look at pandoc's “native” output, i.e., the internal representation of the document after parsing:

$ echo '## 1 Paragraph' | pandoc -t native test.md
[ Header
    2
    ( "paragraph" , [] , [] )
    [ Str "1" , Space , Str "Paragraph" ]
]

The auto-generated ID for the heading is paragraph. The reason for that is that HTML4 doesn't allow identifiers that start with numbers, so pandoc skips those. Hence, [first paragraph](#paragraph) will work.

However, GitHub Flavored Markdown is written with HTML5 in mind, and numbers are allowed as the first id character in that case. Pandoc supports GitHub's scheme as well, and those auto-identifiers are enabled with --from=markdown gfm_auto_identifiers.

Probably better than manual numbering of headings is to call pandoc with --number-sections (or -N); the numbering will be performed automatically.

  • Related