我想在代码片段下添加标题,类似于对图形或表格的操作。只是似乎不知道该怎么做,因为放在\caption
里面verbatim
会逐字逐句地显示我写的内容,呵呵 :)。将其放在下面最终会变成一个与逐字块没有任何关联的段落。
实际上,我希望下面的代码片段看起来像一个逐字逐句的文本块,后面跟着一个独特的描述,所以它看起来不像只是类似于图像标题的工作方式的正文。
\begin{verbatim}
SDSSJ1206+4332
-0.664,.0137,1.748,.028
#1.320,0.147,5.999,0.148
#-2.052,0.200,2.397,0.152
A,0,0.11,0,0.01,18.05,.02
B,-0.098,0.006,2.894,.009,18.38,.02
\end{verbatim}
\caption{Behold this fully functional input file.}
更新:
尝试让 Werners 的解决方案发挥作用。我尝试将他的解决方案放入我现有的文档中,但得到的只是两条水平线,标题在顶部。将他的解决方案放入新文档中是可行的,所以我认为它应该在序言的某个地方。下面是我目前的主要文档的样子。
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[backend=biber, style=numeric,]{biblatex}
\addbibresource{MA.bib}
\usepackage{hyperref}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{siunitx}
\usepackage{listings}
\lstnewenvironment{myverbatim}[1][]{%
\lstset{
basicstyle=\ttfamily,
frame=tb,
captionpos=b,
#1
}%
}{}
\title{MAThe}
\author{Michkov}
\date{January 2021}
\begin{document}
答案1
也许您可以使用listings
:
\documentclass{article}
\usepackage{listings,lipsum}
\lstnewenvironment{myverbatim}[1][]{%
\lstset{
basicstyle=\ttfamily,
frame=tb,
#1
}%
}{}
\begin{document}
\lipsum[1]
\begin{myverbatim}[title={Behold this fully functional input file.}]
SDSSJ1206+4332
-0.664,.0137,1.748,.028
#1.320,0.147,5.999,0.148
#-2.052,0.200,2.397,0.152
A,0,0.11,0,0.01,18.05,.02
B,-0.098,0.006,2.894,.009,18.38,.02
\end{myverbatim}
\lipsum[2]
\end{document}
答案2
这里我使用verbatimbox
水平盒子逐字捕获。输出时可以带边框也可以不带边框\fbox
。
\documentclass{article}
\usepackage{verbatimbox,caption,float,lipsum}
\newfloat{Code}
\captionsetup{Code}
\begin{document}
\lipsum[1]
\begin{verbbox}
SDSSJ1206+4332
-0.664,.0137,1.748,.028
#1.320,0.147,5.999,0.148
#-2.052,0.200,2.397,0.152
A,0,0.11,0,0.01,18.05,.02
B,-0.098,0.006,2.894,.009,18.38,.02
\end{verbbox}
{\centering
\theverbbox
\captionof{Code}{Behold this fully functional input file.}\par
}
{\centering
\fbox{\theverbbox}
\captionof{Code}{Behold this fully functional input file.}\par
}
\end{document}