如何显示突出显示代码的目录?

如何显示突出显示代码的目录?

我从一个论坛上找到了这种方法\newcounter{bash},但我忘记了这里的链接。我想知道如何创建代码目录就像\tableofcontents这样显示的是内部代码的页面\begin{bash} ... \end{bash}

所以应该是这样的

code-1/caption-1 .... page 1
code-2/caption-2 .... page 3

code-n/caption-n .... page 45

这是 MWE:


\documentclass[twoside]{book}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{latexsym}
\usepackage{enumerate}
\usepackage{wrapfig}
\usepackage{siunitx}
\usepackage{cite}
\usepackage{cancel}
\usepackage{ulem}
\usepackage{makecell}
\usepackage{newunicodechar}
\usepackage[utf8]{inputenc}

\usepackage{marginnote}
\usepackage[table, dvipsnames]{xcolor}
\usepackage{float}
\usepackage{listings}
\usepackage{regexpatch}
\DeclareFixedFont{\ttb}{T1}{txtt}{bx}{n}{9}
\DeclareFixedFont{\ttm}{T1}{txtt}{m}{n}{9}  
\usepackage{color}

\newcounter{bash}
\makeatletter


\newcommand{\lstlistbashname}{List of Julia Codes}
\lst@UserCommand\lstlistofbash{\bgroup
    \let\contentsname\lstlistbashname
    \let\lst@temp\@starttoc \def\@starttoc##1{\lst@temp{lop}}%
    \tableofcontents \egroup}
\lstnewenvironment{bash}[1][]{%
    \renewcommand{\lstlistingname}{Julia Code}%
    \let\c@lstlisting=\c@bash
    \let\thelstlisting=\thebash
    \xpatchcmd*{\lst@MakeCaption}{lol}{lop}{}{}%
    \lstset{language=bash,
        keywordstyle=\sffamily\ttm,
        basicstyle=\sffamily\ttm,
        numbersep=5pt,
        frame=tb,
        columns=fullflexible,
        backgroundcolor=\color{yellow!20},
        linewidth=0.95\linewidth,
        xleftmargin=0.05\linewidth,
        breaklines=true,
        captionpos=b,
        #1}}
{}
\makeatother



%----------------------------------------------------------------------------------------

\begin{document}
    \tableofcontents
\begin{bash}
    sudo -i
    
\end{bash}


\end{document}

先行致谢。

答案1

两个变化

  1. 注释掉\xpatchcmd*{\lst@MakeCaption}{lol}{lop}{}{}%
  2. 使用\addcontentsline{toc}{section}{Listings}后跟\lstlistoflistings(来源:https://tex.stackexchange.com/a/97476/133968
\documentclass[twoside]{book}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{latexsym}
\usepackage{enumerate}
\usepackage{wrapfig}
\usepackage{siunitx}
\usepackage{cite}
\usepackage{cancel}
\usepackage{ulem}
\usepackage{makecell}
\usepackage{newunicodechar}
\usepackage[utf8]{inputenc}

\usepackage{marginnote}
\usepackage[table, dvipsnames]{xcolor}
\usepackage{float}
\usepackage{listings}
\usepackage{regexpatch}
\DeclareFixedFont{\ttb}{T1}{txtt}{bx}{n}{9}
\DeclareFixedFont{\ttm}{T1}{txtt}{m}{n}{9}  
\usepackage{color}

\newcounter{bash}
\makeatletter


\newcommand{\lstlistbashname}{List of Julia Codes}
\lst@UserCommand\lstlistofbash{\bgroup
    \let\contentsname\lstlistbashname
    \let\lst@temp\@starttoc \def\@starttoc##1{\lst@temp{lop}}%
    \tableofcontents \egroup}
\lstnewenvironment{bash}[1][]{%
    \renewcommand{\lstlistingname}{Julia Code}%
    \let\c@lstlisting=\c@bash
    \let\thelstlisting=\thebash
    %\xpatchcmd*{\lst@MakeCaption}{lol}{lop}{}{}%
    \lstset{language=bash,
        keywordstyle=\sffamily\ttm,
        basicstyle=\sffamily\ttm,
        numbersep=5pt,
        frame=tb,
        columns=fullflexible,
        backgroundcolor=\color{yellow!20},
        linewidth=0.95\linewidth,
        xleftmargin=0.05\linewidth,
        breaklines=true,
        captionpos=b,
        #1}}
{}
\makeatother



%----------------------------------------------------------------------------------------

\begin{document}
    \addcontentsline{toc}{section}{Listings}
    \lstlistoflistings
    \begin{bash}[caption={Listing 1}]
        sudo -i
    \end{bash}
    
    
\end{document}

在此处输入图片描述

相关内容