向代码列表添加标题的一种方法如下:
\documentclass{article}
\usepackage{fancyvrb}
\usepackage{fvextra}
\usepackage{xcolor}
\usepackage{caption}
\usepackage{float}
\newfloat{codelisting}{h}{lop}
\floatname{codelisting}{Listing}
\captionsetup[subfloat]{margin=0.5em}
\begin{document}
\begin{codelisting}
\caption{test}
test
\end{codelisting}
\end{document}
然而,我确实不是我希望我的codelisting
元素是浮动的,因为我想确保很长的列表能够分页。
我尝试了以下操作:
\documentclass{article}
\usepackage{fancyvrb}
\usepackage{fvextra}
\usepackage{xcolor}
\usepackage{caption}
\newenvironment{codelisting}{\captionsetup{type=listing}}{}
\begin{document}
\begin{codelisting}
\caption{test}
test
\end{codelisting}
\end{document}
但是我收到一个错误:
LaTeX 错误:没有定义计数器“列表”。
如何在 LaTeX 中将标题应用于非浮动元素?特别是使用fancyvrb
和fvextra
包的列表?
编辑
回应@Skillmon的评论。 Pandoc 不嵌套\label
在里面\caption
,因此我尝试了以下方法:
\documentclass{article}
\usepackage{fancyvrb}
\usepackage{fvextra}
\usepackage{xcolor}
\usepackage{float}
\usepackage{caption}
\newfloat{codelisting}{h}{lop}
\floatname{codelisting}{Listing}
\renewenvironment{codelisting}
{\captionsetup{type=codelisting}}
{}
\begin{document}
text pre
\begin{codelisting}
\label{test}
\caption{test}
\VerbatimInput[breaknonspaceingroup, breaklines, breakanywhere]{verbatim.py}
\end{codelisting}
text post
AS shown in Listing \ref{test}
\end{document}
它确实可以编译,但是引用尚未解析,如下图所示:
答案1
\documentclass{article}
\usepackage{fancyvrb}
\usepackage{fvextra}
\usepackage{xcolor}
\usepackage{float}
\usepackage{caption}
\newfloat{codelisting}{h}{lop}
\floatname{codelisting}{Listing}
\newenvironment{code}[2][]
{\captionof{codelisting}{#2}\ifx\relax#1\relax\else\label{#1}\fi}
{}
\begin{document}
\begin{code}{test}
test
\end{code}
foo
\begin{code}[lst:1]{testA} % with label
test
\end{code}
See Listing~\ref{lst:1}
\end{document}
答案2
由于您似乎无法按照评论所建议的那样做,因此这里有一个按照我的评论所建议的答案:
\documentclass{article}
\usepackage{fancyvrb}
\usepackage{fvextra}
\usepackage{xcolor}
\usepackage{float}
\usepackage{caption}
\newfloat{codelisting}{h}{lop}
\floatname{codelisting}{Listing}
\renewenvironment{codelisting}
{\captionsetup{type=codelisting}}
{}
\begin{document}
text pre
\begin{codelisting}
\caption{test}
some code
\end{codelisting}
text post
\end{document}
这是一个具有正确间距的版本(如果嵌套等,则没有检查间距是否正确\VerbatimInput
):
\documentclass{article}
\usepackage{fancyvrb}
\usepackage{fvextra}
\usepackage{xcolor}
\usepackage{float}
\usepackage{caption}
\newfloat{codelisting}{h}{lop}
\floatname{codelisting}{Listing}
\makeatletter
\renewenvironment{codelisting}
{%
\par
\vspace*{\intextsep}% vertical spacing as a float
\captionsetup{type=codelisting}%
}
{%
\par
\vspace*{\intextsep}% vertical spacing as a float
\@endpetrue % following text is only indented if blank line after \end
}
\makeatother
\begin{document}
text pre
\begin{codelisting}
\caption{test\label{code:works}}
some code
\end{codelisting}
text post \ref{code:works}
\end{document}
获取上述\label
不嵌套的输出的替代输入\caption
:
\documentclass{article}
\usepackage{fancyvrb}
\usepackage{fvextra}
\usepackage{xcolor}
\usepackage{float}
\usepackage{caption}
\newfloat{codelisting}{h}{lop}
\floatname{codelisting}{Listing}
\makeatletter
\renewenvironment{codelisting}
{%
\par
\vspace*{\intextsep}% vertical spacing as a float
\captionsetup{type=codelisting}%
}
{%
\par
\vspace*{\intextsep}% vertical spacing as a float
\@endpetrue % following text is only indented if blank line after \end
}
\makeatother
\begin{document}
text pre
\begin{codelisting}
\caption{test}
\label{code:works}
some code
\end{codelisting}
text post \ref{code:works}
\end{document}
输出看起来相同(但请注意,最佳输入语法是\label
嵌套在强制参数中的\caption
——存在输出可能看起来不相同的边界情况)。