我想用 lstlisting 来描述两段代码。但是这样怎么分呢?另外,这样的列表能用 if else 或 for 语句吗?上图 (a) 和 (b) 是由什么命令控制的?是 \caption 吗?
如果有人能重现此图片的 Latex 代码,我将不胜感激。谢谢!
感谢 Willoughby 的帮助。目前的结果图是这样的。
但是现在的字体明显没有上图好看,请问如何修改字体,试了各种方法,都没有办法,最后看了这篇帖子:设置 lstlisting 的字体系列。字体改了,但是还是没有上图好看。请问如何改成上面的字体?还有如何修改(a)、(b)标题和下方方框之间的距离?
\documentclass{article}
\usepackage{listings}
\usepackage{framed}
\usepackage{capt-of}
\usepackage{newfloat}
\usepackage{subfig}
\usepackage{graphicx}
\usepackage{xcolor}
\definecolor{keywordcolor}{RGB}{157,0,129}
\definecolor{commentcolor}{RGB}{157,0,129}
\definecolor{numbercolor}{RGB}{120,120,120}
\DeclareFloatingEnvironment[fileext=frm,placement={!ht},name=Listing]{listing}
\usepackage[ruled,lined]{algorithm2e}
\usepackage{mathtools}
\usepackage{enumitem}
\usepackage{pifont}
\newlength{\restofline}
\usepackage{multirow}
\usepackage{tablefootnote}
\usepackage{lipsum} % Just for dummy text. Do not use in actual document.
\begin{document}
\lipsum
\begin{listing}[t]
\noindent
\centering
\begin{minipage}[b]{\textwidth}
\begin{framed}
\begin{lstlisting}[gobble=16, language=C,numbers=left,
numberstyle={\color{numbercolor}\normalfont},
numbersep={-0.5cm},keywordstyle={\color{keywordcolor}},
breaklines=true]
if (secret)
access line 0
else
access line 1
\end{lstlisting}
\captionof{subfigure}{function1}
\end{framed}
\end{minipage}%
\\[-0.15em]
\begin{minipage}[b]{\textwidth}
\begin{framed}
\begin{lstlisting}[gobble=16, language=C,numbers=left,numberstyle={\color{numbercolor}\normalfont},
numbersep={-0.5cm},keywordstyle={\color{keywordcolor}}]
if (secret)
access line 0
else
access line 1
\end{lstlisting}
\captionof{subfigure}{function2}
\end{framed}
\end{minipage}
\caption{exploitation}
\end{listing}
\lipsum
\end{document}
答案1
这是其中一种方法。它不是最干净的,您可以通过定义环境等来改进它。主要基于这个答案
\documentclass{article}
\usepackage{listings}
\usepackage{framed}
\usepackage{capt-of}
\usepackage{newfloat}
\usepackage{subfig}
\usepackage{xcolor}
\definecolor{keywordcolor}{RGB}{100,0,100}
\definecolor{commentcolor}{RGB}{50,100,50}
\DeclareFloatingEnvironment[fileext=frm,placement={!ht},name=Listing]{listing}
\lstset{
numbers=left,
numberstyle={\color{lightgray}\footnotesize},
numbersep={0.1cm},
keywordstyle={\color{keywordcolor}},
commentstyle={\color{commentcolor}}
}
\begin{document}
\begin{listing}
\noindent
\begin{minipage}[b]{\textwidth}
\begin{framed}
\begin{lstlisting}[gobble=16, language=C, escapechar=|]
if (x < bound) // |\color{commentcolor}\(b_{v0}\)|
if (array1[x]) // |\color{commentcolor}\(b_v\)|
<some_operations>
\end{lstlisting}
\captionof{subfigure}{Two-level conditional branches}
\end{framed}
\end{minipage}%
\\[-0.15em]
\begin{minipage}[b]{\textwidth}
\begin{framed}
\begin{lstlisting}[gobble=16, language=C, escapechar=|]
if (x < bound) // |\color{commentcolor}\(b_{v0}\)|
for (int i = 0; i < bound; i++)
if (array1[x + i]) // |\color{commentcolor}\(b_v\)|
<some_operations>
\end{lstlisting}
\captionof{subfigure}{Multi-level speculation}
\end{framed}
\end{minipage}
\\[-0.15em]
\begin{minipage}[b]{\textwidth}
\begin{framed}
\begin{lstlisting}[gobble=16, language=C, escapechar=|]
for (int i = x; i < bound; i++) // |\color{commentcolor}\(b_{v0}\)|
if (array1[i]) // |\color{commentcolor}\(\b_v\)|
<some_operations>
\end{lstlisting}
\captionof{subfigure}{Loop-based speculation}
\end{framed}
\end{minipage}
\caption{BranchSpec gadgets for side channel exploitation}
\end{listing}
\end{document}
颜色/语言只是一种近似值,因为它对于问题来说并不重要。