![我如何引用 lstlisting](https://linux22.com/image/454951/%E6%88%91%E5%A6%82%E4%BD%95%E5%BC%95%E7%94%A8%20lstlisting%20.png)
我刚刚得到以下代码。
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{geometry}
\usepackage{blindtext}
\usepackage{enumerate}
\geometry{paper=a4paper, left=24mm, right=23mm, top=20mm, bottom=20mm}
\usepackage{listings}
\lstset{
basicstyle=\ttfamily\footnotesize,
xleftmargin=.1\textwidth,
}
\begin{document}
\blindtext
\begin{lstlisting}[language=Python]
def generate_grid(data, basemap, delta=1):
grid = {
'lon': np.arange(-180, 180, delta),
'lat': np.arange(-89.9,89.9, delta)
}
grid["x"], grid["y"] = np.meshgrid(grid["lon"], grid["lat"])
grid["x"], grid["y"] = basemap(grid["x"], grid["y"])
return grid
\end{lstlisting}
\blindtext
\end{document}
但是我希望左边有一个数字,因为我有几个列表,我想引用它们,我该怎么做?
(抱歉我的画不好)
答案1
我结合了`listings` 和 `sidecap` - 列表的侧边标题和将文字放在图片的右侧将标题置于列表的右侧中央。然后添加括号,并使用空选项labelformat=parens
删除单词“Listing” 。name=
\DeclareFloatingEnvironment
请注意,标签和标题需要以常规方式设置(因此不是作为选项),并且需要手动添加语句lstlisting
周围的括号,因为不会扩展到的输出。\ref
labelformat
\ref
梅威瑟:
% sources:
% https://tex.stackexchange.com/questions/125106/listings-and-sidecap-side-captions-for-listings
% https://tex.stackexchange.com/questions/49979/put-text-to-the-right-of-figures
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{geometry}
\usepackage{blindtext}
\usepackage{enumerate}
\geometry{paper=a4paper, left=24mm, right=23mm, top=20mm, bottom=20mm}
\usepackage{listings}
\lstset{
basicstyle=\ttfamily\footnotesize,
xleftmargin=.1\textwidth,
}
\usepackage[wide]{sidecap}
\usepackage{caption}
\usepackage{newfloat}
\DeclareFloatingEnvironment[placement={!ht},name=]{listing}
\captionsetup[listing]{labelformat=parens}
\sidecaptionvpos{listing}{c}
\makeatletter
\makeatother
\begin{document}
\blindtext
\begin{SClisting}
\begin{lstlisting}[language=Python]
def generate_grid(data, basemap, delta=1):
grid = {
'lon': np.arange(-180, 180, delta),
'lat': np.arange(-89.9,89.9, delta)
}
grid["x"], grid["y"] = np.meshgrid(grid["lon"], grid["lat"])
grid["x"], grid["y"] = basemap(grid["x"], grid["y"])
return grid
\end{lstlisting}
\caption{}
\label{lst:grid}
\end{SClisting}
\blindtext
The grid algorithm is described in (\ref{lst:grid}).
\end{document}
结果:
答案2
和聪明人及其宏\crefname
,您可以创建一个自定义计数器并为其分配一个标签 - 我已经添加了python 代码用于演示。然后,使用minipage
,您可以在代码的右侧为数字创建一些空间;我已经使用 完成了此操作minipage
,默认情况下将数字对齐在中间。
在下面的代码中,pythoncode
如果您出于某种原因想要增加/减少缩进,我还建议使用带有可选参数的自定义环境。当然,您可以参数化环境的任何其他选项。
\documentclass{article}
% \usepackage[utf8]{inputenc}
\usepackage{geometry}
\geometry{paper=a4paper, left=24mm, right=23mm, top=20mm, bottom=20mm}
\usepackage{blindtext}
\usepackage{enumerate}
\usepackage{listings}
\lstnewenvironment{pythoncode}[1][.1]{%
\lstset{
basicstyle=\ttfamily\footnotesize,
xleftmargin=#1\textwidth,
language=Python,
}}{}
\usepackage[noabbrev]{cleveref}
%%% New counter controlling python code numbers
\newcounter{lst}
\renewcommand*{\thelst}{(\arabic{lst})} % Format the counter (currently (<number>)
\crefname{lst}{python code}{python codes} % Adds a label when referencing
\AtBeginEnvironment{minipage}{\noindent} % Removes indentation at the beginning of each minipage
\begin{document}
\blindtext
\begin{minipage}{\dimexpr\textwidth-3em}%
\refstepcounter{lst}\label{lst:a}%
\begin{pythoncode}
def generate_grid(data, basemap, delta=1):
grid = {
'lon': np.arange(-180, 180, delta),
'lat': np.arange(-89.9,89.9, delta)
}
grid["x"], grid["y"] = np.meshgrid(grid["lon"], grid["lat"])
grid["x"], grid["y"] = basemap(grid["x"], grid["y"])
return grid
\end{pythoncode}%
\end{minipage}{\raggedleft\thelst\par}
\blindtext
\begin{minipage}{\dimexpr\textwidth-3em}
\refstepcounter{lst}\label{lst:b}%
\begin{pythoncode}
def foo(x):
return x**2
\end{pythoncode}
\end{minipage}{\raggedleft\thelst\par}
\Cref{lst:a} and \cref{lst:b}.
\end{document}