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.
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.
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.
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
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 481 |
Nodes: | 16 (2 / 14) |
Uptime: | 15:49:03 |
Calls: | 9,540 |
Files: | 13,653 |
Messages: | 6,139,706 |