Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Markdown: How to reference an item in a numbered list, by number (like LaTeX's \ref / \label)?

Tags:

markdown

latex

Is there any way in markdown to do the equivalent of the cross-referencing in this LaTeX snippet? (Taken from here.)

\begin{enumerate}
    \item \label{itm:first} This is a numbered item
    \item Another numbered item \label{itm:second}
    \item \label{itm:third} Same as \ref{itm:first}
\end{enumerate}
Cross-referencing items \ref{itm:second} and \ref{itm:third}.

This LaTeX produces

1. This is a numbered item
2. This is another numbered item
3. Same as 1

Cross-referencing items 2 and 3.

That is, I would like to be able to refer to items in a markdown list without explicitly numbering them, so that I could change the above list to the following without having to manually update the cross references:

1. This is the very first item
2. This is a numbered item
3. This is another numbered item
4. Same as 2

Cross-referencing items 3 and 4.
like image 271
Alex Coventry Avatar asked May 06 '16 15:05

Alex Coventry


People also ask

How do you reference an item in a list in LaTeX?

The easiest way to refer to all types of lists, is to use the packages enumitem and enumitem-zref. Then you may refer to all three types of list. If you want to refer items in a bullet list, you have to set up enumitem-zref correctly.

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 I reference a section in LaTeX?

In LaTeX you can easily reference a section by using \label{} next to a section and then \ref{} to create the reference. However, the reference only includes the number of the section, or the page with \pageref{} .


2 Answers

Maybe you need to use the H1.. H6 and then Markdown generates an anchor that you can link to:

# H1
## H2
### H3
#### H4
##### H5
###### H6

Something like:

###### 1. This is a numbered item
###### 2. This is another numbered item
###### 3. Same as 1

Generates:

<h6 id="1-this-is-a-numbered-item">1. This is a numbered item</h6>
<h6 id="2-this-is-another-numbered-item">2. This is another numbered item</h6>
<h6 id="3-same-as-1">3. Same as 1</h6>
like image 195
numediaweb Avatar answered Oct 01 '22 00:10

numediaweb


HTML can't even do that and Markdown is a subset of HTML, so the answer is no.

For example, your list would be represented like so (when rendered by Markdown):

<ol>
    <li>This is a numbered item</li>
    <li>This is another numbered item</li>
    <li>Same as 1</li>
</ol>

Notice that there is no indication of which item is which as far as the numbering goes. That is all inferred at render time by the browser. However, the number values are not stored within the document and are not referenceable or linkable. They are for display only and serve no other purpose.

Now you could write some custom HTML to uniquely identify each list item and make them referenceable:

<ol>
    <li id="item1">This is a numbered item</li>
    <li id="item2">This is another numbered item</li>
    <li id="item3">Same as <a href="#item1>1</a></li>
</ol>

However, those IDs are hardcoded and have no relation to the numbers used to display the items. Although, I suppose that's what you want. To make your updated changes:

<ol>
    <li id="item0">This is the very first item</li>
    <li id="item1">This is a numbered item</li>
    <li id="item2">This is another numbered item</li>
    <li id="item3">Same as <a href="#item1">2</a></li>
</ol>

The IDs stay with the item as intended. However, lets move on to the links to those list items. Note that in the first iteration we had:

<a href="#item1">1</a>

And with the update we had:

<a href="#item1">2</a>

The only difference being the link's label (changed from "1" to "2"). That is actually changing the document text through some sort of macro magic stuff. Not something HTML can do, at least not without JavaScript and/or CSS to help.

In other words, the text of every reference to the item would need to be manually updated throughout the document every time the list is updated. And that is for HTML. What about Markdown? As the rules state:

Markdown is not a replacement for HTML, or even close to it. Its syntax is very small, corresponding only to a very small subset of HTML tags.

Therefore in standard Markdown there is not even any way to assign IDs to the list items.

Seems to me you either need to use something other than lists or use something other than Markdown/HTML.

like image 27
Waylan Avatar answered Sep 30 '22 22:09

Waylan