• label on the left side of minted or listings

    From Jinsong Zhao@21:1/5 to All on Mon Mar 3 12:03:02 2025
    Hi there,

    Hi there,

    I am typesetting code using minted or listings package. It works fine.
    Now I am hoping to add a label, similar to equation label, on the left
    side of the code block, looks like following. (%o1) is the label.

    (%o1) code here here here
    code here here
    code here
    code

    However, I can't find a way to do this. Any hint will be appreciated.

    Best wishes,

    Jinsong

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From =?utf-8?Q?Denis_Bitouz=C3=A9?=@21:1/5 to All on Mon Mar 3 09:38:02 2025
    Hi,

    Le 03/03/25 à 12h03, Jinsong Zhao a écrit :

    I am typesetting code using minted or listings package. It works fine. Now I am
    hoping to add a label, similar to equation label, on the left side of the code
    block, looks like following. (%o1) is the label.

    (%o1) code here here here
    code here here
    code here
    code

    However, I can't find a way to do this. Any hint will be appreciated.

    IIUC, what you're after is a way to “escape” in LaTeX within the
    listing. This can be done thanks to the `escapechar` option of listing
    (I guess minted provides it as well). Here a MWE:

    --8<---------------cut here---------------start------------->8--- \documentclass{article}
    \usepackage{listings}
    \lstset{
    basicstyle=\ttfamily,
    numbers=left,
    language=TeX,
    alsolanguage=[LaTeX]TeX,
    escapechar="
    }
    \begin{document}
    In the following minimal file, the line~\ref{begin} begins the document and the line~\ref{end} ends the document.
    \begin{lstlisting}
    \documentclass{article}
    \begin{document}"\label{begin}"
    Foo
    \end{document}"\label{end}"
    \end{lstlisting}
    \end{document}
    --8<---------------cut here---------------end--------------->8---

    Best regards.
    --
    Denis

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jinsong Zhao@21:1/5 to All on Mon Mar 3 21:15:34 2025
    On 2025/3/3 16:38, Denis Bitouzé wrote:
    Hi,

    Le 03/03/25 à 12h03, Jinsong Zhao a écrit :

    I am typesetting code using minted or listings package. It works fine. Now I am
    hoping to add a label, similar to equation label, on the left side of the code
    block, looks like following. (%o1) is the label.

    (%o1) code here here here
    code here here
    code here
    code

    However, I can't find a way to do this. Any hint will be appreciated.

    IIUC, what you're after is a way to “escape” in LaTeX within the
    listing. This can be done thanks to the `escapechar` option of listing
    (I guess minted provides it as well). Here a MWE:

    --8<---------------cut here---------------start------------->8--- \documentclass{article}
    \usepackage{listings}
    \lstset{
    basicstyle=\ttfamily,
    numbers=left,
    language=TeX,
    alsolanguage=[LaTeX]TeX,
    escapechar="
    }
    \begin{document}
    In the following minimal file, the line~\ref{begin} begins the document and the
    line~\ref{end} ends the document.
    \begin{lstlisting}
    \documentclass{article}
    \begin{document}"\label{begin}"
    Foo
    \end{document}"\label{end}"
    \end{lstlisting}
    \end{document}
    --8<---------------cut here---------------end--------------->8---

    Best regards.

    Thanks for your reply.

    I may not have been clear enough in the previous post. What I want is
    not to add a label to a specific line and then reference it, which seems
    to be what your reply was addressing. Instead, I am looking for a
    numbering system similar to the one used in the equation environment,
    where the equation number is printed to the left or right of the
    formula. For my question, I want the first line of this block of code to
    print a label on the left side, specifically (%o1).

    Thank you again for your reply.

    Best wishes,
    Jinsong

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Peter Flynn@21:1/5 to Jinsong Zhao on Mon Mar 3 21:45:32 2025
    On 03/03/2025 04:03, Jinsong Zhao wrote:
    Hi there,

    Hi there,

    I am typesetting code using minted or listings package. It works fine.
    Now I am hoping to add a label, similar to equation label, on the left
    side of the code block, looks like following. (%o1) is the label.

    (%o1)  code here here here
           code here here
           code here
           code

    However, I can't find a way to do this. Any hint will be appreciated.

    I would think the simplest is to put the code listing in a list item, eg

    \usepackage{enumitem,listings}
    ...
    \begin{enumerate}[label={(\%ol)}]
    \item \begin{lstlisting}
    foo
    bar
    \end{lstlisting}
    \end{enumerate}

    That way (enumitem) you have good control over the label and the spacing.

    Peter

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jinsong Zhao@21:1/5 to Peter Flynn on Tue Mar 11 00:10:48 2025
    On 2025/3/4 5:45, Peter Flynn wrote:
    On 03/03/2025 04:03, Jinsong Zhao wrote:
    Hi there,

    Hi there,

    I am typesetting code using minted or listings package. It works fine.
    Now I am hoping to add a label, similar to equation label, on the left
    side of the code block, looks like following. (%o1) is the label.

    (%o1)  code here here here
            code here here
            code here
            code

    However, I can't find a way to do this. Any hint will be appreciated.

    I would think the simplest is to put the code listing in a list item, eg

    \usepackage{enumitem,listings}
    ...
    \begin{enumerate}[label={(\%ol)}]
    \item \begin{lstlisting}
    foo
    bar
    \end{lstlisting}
    \end{enumerate}

    That way (enumitem) you have good control over the label and the spacing.

    Peter

    Thank you very much. The code in your reply is exactly what I need. It
    is simple and robust (I think).

    I'm just getting back to you because my last test (for some reason)
    didn't work, but today it did. It's really weird.

    Best,
    Jinsong

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)