0

I want to use lstlisting to describe two pieces of code. But how can it be divided like this? In addition, can such a list use if else or for statements? What command controls (a) and (b) above the picture? Is it \caption?

If anyone can reproduce the Latex code for this picture, I would be more grateful. Thank you!

enter image description here

Thank you Willoughby for your help. The current result graph is like this.

But the current font is obviously not as good as the picture above. I would like to ask how to modify the font. I tried various methods, but there is no way. Finally read this post: set the font family for lstlisting. The font has changed, but it is still not as good as the picture above. How can I change to the above font? And how to modify the distance between (a), (b) caption and the box below?

enter image description here

\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}
2

the requested listings style Here is one way of doing it. It's not the cleanest, you could improve it by defining an environment etc. Based mostly on this answer

\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}

The colouring/language is just an approximation as it's not important to the question.

3
  • thank you! But the caption is not displayed in the sublist normally. And how to change the position and size of the serial number next to it. How to change the size of the caption? I put the result graph on the question – c yj Mar 22 at 2:01
  • lstlistings is a verbatim like environment, that means that all spaces are put in the document. If you want n spaces of indentation that don't effect the output use the [gobble=n] option. Also verbatim environments won't automatically wrap text so if a line is too long it will just go off the page. Without seeing your code I can't say why a, b, etc. aren't displaying. As for changing the size given that your captions are all bold I'm guessing you have some global setting (maybe in a class) that is doing that and editing that may help – Willoughby Mar 22 at 8:32
  • Thank you. thank you! I added the code and descriptions. – c yj Mar 22 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.