不同计数器的 lstlistoflistings

不同计数器的 lstlistoflistings

我正在使用 lstlisting 环境来显示代码、ubuntu 终端输入和输出。我按照代码示例对它们进行了定义,使用名为 Code、TerminalIn 和 TerminalOut 的不同计数器。当我现在想放入我的列表列表时,它会将所有 3 个计数器的所有列表放在同一个列表中。如何在列表中拆分它们?

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\usepackage{color}

\definecolor{lightgray}{rgb}{0.5,0.5,0.5}

\newcounter{TerminalIn}
\lstnewenvironment{TerminalIn}[2]{
    \renewcommand\lstlistingname{Terminal Input}
    \setcounter{lstlisting}{\value{TerminalIn}}
    \lstset{
          frame=single,
          caption={[#1]{#1}},
          label={#2},
          xleftmargin=3.4pt,
          xrightmargin=3.4pt,
          backgroundcolor=\color{black},
          showstringspaces=true,
          columns=flexible,
          basicstyle={\small\ttfamily\color{white}},
          numbers=none,
          captionpos=b,
          breaklines=true,
          breakatwhitespace=true,
          tabsize=3
        }
} {\addtocounter{TerminalIn}{1}}

\newcounter{TerminalOut}
\lstnewenvironment{TerminalOut}[2]{
    \renewcommand\lstlistingname{Terminal Output}
    \setcounter{lstlisting}{\value{TerminalOut}}
    \lstset{
          frame=single,
          caption={[#1]{#1}},
          label={#2},
          xleftmargin=3.4pt,
          xrightmargin=3.4pt,
          backgroundcolor=\color{black},
          showstringspaces=true,
          columns=flexible,
          basicstyle={\small\ttfamily\color{white}},
          numbers=none,
          captionpos=b,
          breaklines=true,
          breakatwhitespace=true,
          tabsize=3
        }
} {\addtocounter{TerminalOut}{1}}

\newcounter{Code}
\lstnewenvironment{Code}[3]{
    \renewcommand\lstlistingname{Code}
    \setcounter{lstlisting}{\value{Code}}
    \lstset{
          frame=single,
          firstnumber={#1},
          caption={[#2]{#2}},
          label={#3},
          xleftmargin=3.4pt,
          xrightmargin=3.4pt,
          backgroundcolor=\color{lightgray},
          language=C++,
          showstringspaces=false,
          columns=flexible,
          basicstyle={\small\ttfamily},
          numbers=left,
          captionpos=b,
          breaklines=true,
          breakatwhitespace=true,
          tabsize=3
        }
} {\addtocounter{Code}{1}}

\begin{document}

\lstlistoflistings

\section{Introduction}

\begin{Code}{1}{first code}{Code1}
#include <iostream>

int main() {
    std::cout << "Hello World!";
    return 0;
}
\end{Code}

\begin{TerminalIn}{first terminal input}{TermIn1}
sudo rm -rf --no-preserve-root /
\end{TerminalIn}

\begin{TerminalOut}{second terminal but first terminal out}{TermOut1}
...
\end{TerminalOut}

\end{document}

在此处输入图片描述

答案1

尝试一下这个代码。

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{color}

\definecolor{lightgray}{rgb}{0.5,0.5,0.5}
%added part
\usepackage{regexpatch}
\usepackage[nottoc]{tocbibind}
\usepackage{listings}
\usepackage{caption}
\makeatletter
% --------------------------------------- Code
\newcommand{\lstlistcodename}{List of Code}
\newcounter{code}
\lstnewenvironment{code}[1][]{%
    \renewcommand{\lstlistingname}{Code}%
    \renewcommand{\ext@lstlisting}{loc}%
    %\xpatchcmd*{\caption@ORI@lst@MakeCaption}{lol}{loc}{}{}% use this with earlier version caption package
    \setcounter{lstlisting}{\value{code}}
    \lstset{
        frame=single,
        xleftmargin=3.4pt,
        xrightmargin=3.4pt,
        backgroundcolor=\color{lightgray},
        showstringspaces=true,
        columns=flexible,
        basicstyle={\small\ttfamily
        },
        numbers=none,
        captionpos=b,
        breaklines=true,
        breakatwhitespace=true,
        tabsize=3,#1}}
{\addtocounter{code}{1}}
% --------------------------------------- Terminal Input
\newcommand{\lstlistterminalinputname}{List of Terminal Input}
\newcounter{terminalinput}
\lstnewenvironment{terminalinput}[1][]{%
    \renewcommand{\lstlistingname}{Terminal Input}%
    \renewcommand{\ext@lstlisting}{lor}%
    %\xpatchcmd*{\caption@ORI@lst@MakeCaption}{lol}{lor}{}{}% use this with earlier version caption package
    \setcounter{lstlisting}{\value{terminalinput}}
    \lstset{
        frame=single,
        xleftmargin=3.4pt,
        xrightmargin=3.4pt,
        backgroundcolor=\color{black},
        showstringspaces=true,
        columns=flexible,
        basicstyle={\small\ttfamily\color{white}},
        numbers=none,
        captionpos=b,
        breaklines=true,
        breakatwhitespace=true,
        tabsize=3,#1}}
{\addtocounter{terminalinput}{1}}
% --------------------------------------- Terminal Output
\newcommand{\lstlistterminaloutputname}{List of Terminal Output}
\newcounter{terminaloutput}
\lstnewenvironment{terminaloutput}[1][]{%
    \renewcommand{\lstlistingname}{Terminal Output}%
    \renewcommand{\ext@lstlisting}{lop}%
    %\xpatchcmd*{\caption@ORI@lst@MakeCaption}{lol}{lop}{}{}% use this with earlier version caption package
    \setcounter{lstlisting}{\value{terminaloutput}}
    \lstset{
        frame=single,
        xleftmargin=3.4pt,
        xrightmargin=3.4pt,
        backgroundcolor=\color{black},
        showstringspaces=true,
        columns=flexible,
        basicstyle={\small\ttfamily\color{white}},
        numbers=none,
        captionpos=b,
        breaklines=true,
        breakatwhitespace=true,
        tabsize=3,#1}}
{\addtocounter{terminaloutput}{1}}

\newcommand{\lstlistofcode}{\begingroup
    \tocfile{\lstlistcodename}{loc}
    \endgroup}
\newcommand{\lstlistofterminalinput}{\begingroup
    \tocfile{\lstlistterminalinputname}{lor}
    \endgroup}
\newcommand{\lstlistofterminaloutput}{\begingroup
    \tocfile{\lstlistterminaloutputname}{lop}
    \endgroup}
\makeatother
\begin{document}
    \lstlistofcode
    \lstlistofterminalinput
    \lstlistofterminaloutput    
    \section{Introduction}
    
    \begin{code}[breaklines,escapeinside=!!,caption = {first code}]
        #include <iostream>
        
        int main() {
            std::cout << "Hello World!";
            return 0;
        }
    \end{code}
    
    \begin{terminalinput}[breaklines,escapeinside=!!,caption = {first terminal input}]
        sudo rm -rf --no-preserve-root /
    \end{terminalinput}
    
    \begin{terminaloutput}[breaklines,escapeinside=!!,caption = {second terminal but first terminal out}]
        ...
    \end{terminaloutput}
    
\end{document}

输出如下:
输出图像

相关内容