分类列表

分类列表

是否可以按类型对列表进行分组?

我有一个包含两种类型列表的文档,一个是源代码,一个是一系列 shell 命令(及其输出)。

那么,是否可以将\lstlistingname一种类型称为“列表”,将另一种类型称为“shell-transcript”?理想情况下,我还想在\lstlistoflistings每种类型中将这些类型分开。(类似于\printbibliography[type=book]- 因此我最终会得到一个实际源代码列表和一个 shell 演示列表)

答案1

使用具有特定名称的单独列表环境shell-scripts非常容易,请参阅\lstnewenvironment代码。

.lol更困难的是和设置的拆分.los\lst@MakeCaption必须重新定义并改为使用los而不是lol

hyperref请注意:由于锚点的存在,当前版本无法很好地工作!

\documentclass{article}

\usepackage{listings}
\newcounter{shelllistcntr}[section]

\makeatletter
\def\lst@MakeOtherCaption#1{%
  \lst@ifdisplaystyle
  \ifx #1t%
  \ifx\lst@@caption\@empty\expandafter\lst@HRefStepCounter \else
  \expandafter\refstepcounter
  \fi {lstlisting}%
  \ifx\lst@label\@empty\else \label{\lst@label}\fi
  \let\lst@arg\lst@intname \lst@ReplaceIn\lst@arg\lst@filenamerpl
  \global\let\lst@name\lst@arg \global\let\lstname\lst@name
  \lst@ifnolol\else
  \ifx\lst@@caption\@empty
  \ifx\lst@caption\@empty
  \ifx\lst@intname\@empty \else \def\lst@temp{ }%
  \ifx\lst@intname\lst@temp \else
  \addcontentsline{los}{lstlisting}\lst@name
  \fi\fi
  \fi
  \else
  \addcontentsline{los}{lstlisting}%
  {\protect\numberline{\thelstlisting}\lst@@caption}%
  \fi
  \fi
  \fi
  \ifx\lst@caption\@empty\else
  \lst@IfSubstring #1\lst@captionpos
  {\begingroup \let\@@vskip\vskip
    \def\vskip{\afterassignment\lst@vskip \@tempskipa}%
    \def\lst@vskip{\nobreak\@@vskip\@tempskipa\nobreak}%
    \par\@parboxrestore\normalsize\normalfont % \noindent (AS)
    \ifx #1t\allowbreak \fi
    \ifx\lst@title\@empty
    \lst@makecaption\fnum@lstlisting{\ignorespaces \lst@caption}
    \else
    \lst@maketitle\lst@title % (AS)
    \fi
    \ifx #1b\allowbreak \fi
    \endgroup}{}%
  \fi
  \fi
}

\AtBeginDocument{%    
\lstnewenvironment{shellstuff}[1][]{%
  \let\lst@MakeCaption\lst@MakeOtherCaption%
  \let\c@lstlisting\c@shelllistcntr% Use the other counter
  \let\thelstlisting\theshelllistcntr% Use a different counter format if really needed
  \lstset{language={bash},#1}% Define the language etc. 
  \renewcommand{\lstlistingname}{Shell stuff}%
}{%
}
}

\newcommand{\lstshellscriptlistingname}{List of Shell Scripts}

\newcommand{\lstshellscriptlistings}{% The `\tableofshellscripts` ;-)
  \bgroup
  \let\contentsname\lstshellscriptlistingname%
  \let\lst@temp\@starttoc \def\@starttoc##1{\lst@temp{los}}%
  \tableofcontents \egroup
}


\makeatother



\begin{document}

\lstlistoflistings
\lstshellscriptlistings
\begin{shellstuff}[caption={Foo shell}]
  rm *.*
  ls -l
  echo ``Ooops''
\end{shellstuff}

\begin{lstlisting}[language={C},caption={Hello world}]
#include<stdio.h>

int main(int argc,char **argv)
{
  printf("Hello World!\n");
  return(0);
}
\end{lstlisting}


\end{document}

在此处输入图片描述

更新“有限”hyperref支持和addition to Toc

\documentclass{book}

\usepackage{tocbibind}
\usepackage{listings}

\makeatletter
\newif\ifclassprovideschapter

\@ifundefined{chapter}{%
  \global\classprovideschapterfalse
  \newcounter{shelllistcntr}[section]
}{%
  \global\classprovideschaptertrue
  \newcounter{shelllistcntr}[chapter]
}

\makeatother


\usepackage{hyperref}


\makeatletter
\def\lst@MakeOtherCaption#1{%
  \lst@ifdisplaystyle
  \ifx #1t%
  \ifx\lst@@caption\@empty\expandafter\lst@HRefStepCounter \else
  \expandafter\refstepcounter
  \fi {shelllistcntr}%
  \ifx\lst@label\@empty\else \label{\lst@label}\fi
  \let\lst@arg\lst@intname \lst@ReplaceIn\lst@arg\lst@filenamerpl
  \global\let\lst@name\lst@arg \global\let\lstname\lst@name
  \lst@ifnolol\else
  \ifx\lst@@caption\@empty
  \ifx\lst@caption\@empty
  \ifx\lst@intname\@empty \else \def\lst@temp{ }%
  \ifx\lst@intname\lst@temp \else
  \addcontentsline{los}{lstlisting}\lst@name
  \fi\fi
  \fi
  \else
  \addcontentsline{los}{lstlisting}%
  {\protect\numberline{\theshelllistcntr}\lst@@caption}%
  \fi
  \fi
  \fi
  \ifx\lst@caption\@empty\else
  \lst@IfSubstring #1\lst@captionpos
  {\begingroup \let\@@vskip\vskip
    \def\vskip{\afterassignment\lst@vskip \@tempskipa}%
    \def\lst@vskip{\nobreak\@@vskip\@tempskipa\nobreak}%
    \par\@parboxrestore\normalsize\normalfont % \noindent (AS)
    \ifx #1t\allowbreak \fi
    \ifx\lst@title\@empty
    \lst@makecaption\fnum@lstlisting{\ignorespaces \lst@caption}
    \else
    \lst@maketitle\lst@title % (AS)
    \fi
    \ifx #1b\allowbreak \fi
    \endgroup}{}%
  \fi
  \fi
}


\AtBeginDocument{%
\lstnewenvironment{shellstuff}[1][]{%
  \let\lst@MakeCaption\lst@MakeOtherCaption%
  \let\c@lstlisting\c@shelllistcntr%
  \let\thelstlisting\theshelllistcntr%
  \renewcommand{\theHlstlisting}{shelllist.\theshelllistcntr}%
  \lstset{language={bash},#1}%
  \renewcommand{\lstlistingname}{Shell stuff}%
}{%
}
}

\newcommand{\lstshellscriptlistingname}{List of Shell Scripts}

\newcommand{\lstshellscriptlistings}{%
  \bgroup
  \let\contentsname\lstshellscriptlistingname%
  \let\lst@temp\@starttoc \def\@starttoc##1{\lst@temp{los}}%
  \tableofcontents 
  \egroup
}


\makeatother



\begin{document}
\tableofcontents
\clearpage
\lstlistoflistings
\lstshellscriptlistings

\clearpage

\begin{shellstuff}[caption={Foo shell}]
  rm *.*
  ls -l
  echo ``Ooops''
\end{shellstuff}

\clearpage

\begin{lstlisting}[language={C},caption={Hello world}]
#include<stdio.h>

int main(int argc,char **argv)
{
  printf("Hello World!\n");
  return(0);
}
\end{lstlisting}


\end{document}

  \begingroup
  \section*{Foo}
  \@starttoc{los}
  \endgroup
}

相关内容