Search code examples
latexmarkdown

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


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.

Solution

  • 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.