Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use LaTeX section numbers in Pandoc cross-reference

The Pandoc documentation says that cross references can be made to section headers in a number of ways. For example, you can create your own ID and reference that ID. For example:

# This is my header {#header}

Will create an ID with value '#header' that can be refenced in the text, as such:

[Link to header](#header)

Which will display the text 'Link to header' with a link to the header.

I couldn't find anywhere how to make the text of the link be the section number when compiled as a LaTeX document.

For example, if my header is compiled to '1.2.3 Section Header', I want my cross-reference to text to display as '1.2.3'.

like image 679
Sam Walpole Avatar asked Jan 10 '19 12:01

Sam Walpole


People also ask

How do I reference a section in R markdown?

Authoring Books and Technical Documents with R Markdown In fact, you can also reference sections using the same syntax \@ref(label) , where label is the section ID. By default, Pandoc will generate an ID for all section headers, e.g., a section # Hello World will have an ID hello-world .

How do you use extensions in Pandoc?

An extension can be enabled by adding +EXTENSION to the format name and disabled by adding -EXTENSION . For example, --from markdown_strict+footnotes is strict Markdown with footnotes enabled, while --from markdown-footnotes-pipe_tables is pandoc's Markdown without footnotes or pipe tables.

What is Pandoc Crossref?

pandoc-crossref is a filter that adds support for figure, table, and equation numbers and cross-references. pandoc-xnos is a suite of filters supporting numbering and cross-referencing figures, equations, tables, and sections.

Can Pandoc convert HTML to markdown?

Pandoc can convert between numerous markup and word processing formats, including, but not limited to, various flavors of Markdown, HTML, LaTeX and Word docx.


2 Answers

This can be achieved by defining the ID as done previously. eg:

# This is my header {#header}

Then in the text, the cross reference can be written as:

\ref{header}

When this compiles to LaTeX, the cross-reference text will be the section number of the referenced heading.

like image 140
Sam Walpole Avatar answered Sep 27 '22 22:09

Sam Walpole


You can use the pandoc-secnos filter, which is part of the pandoc-xnos filter suite.

The header

# This is my header {#sec:header}

is referenced using @sec:header. Alternatively, you can reference

# This is my header

using @sec:this-is-my-header.

Markdown documents coded in this way can be processed by adding --filter pandoc-secnos to the pandoc call. The --number-sections option should be used as well. The output uses LaTeX's native commands (i.e., \label and \ref or \cref).

The benefit to this approach is that output in other formats (html, epub, docx, ...) is also possible.

like image 36
tjd Avatar answered Sep 27 '22 23:09

tjd