我定义了两个列表环境,一个用于 Python,另一个用于 Bash,两者都有不同的设置,这是我正在使用的最小示例:
\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
% Some definitions to use color
% Default fixed font does not support bold face
\DeclareFixedFont{\ttb}{T1}{txtt}{bx}{n}{9} % for bold
\DeclareFixedFont{\ttm}{T1}{txtt}{m}{n}{9} % for normal
% Custom colors
\usepackage{color}
\definecolor{deepblue}{rgb}{0,0,0.5}
\definecolor{deepred}{rgb}{0.6,0,0}
\definecolor{deepgreen}{rgb}{0,0.5,0}
\usepackage{listings}
% Python style for highlighting
\newcommand\pythonstyle{\lstset{
language=Python,
basicstyle=\footnotesize,
otherkeywords={self},
tabsize=1,
keywordstyle=\ttb\color{deepblue},
emph={__init__, update_port},
emphstyle=\ttb\color{deepred},
stringstyle=\ttb\color{deepgreen},
frame=single,
showstringspaces=false,
float=htpb,
numbers=left,
captionpos=b,
numbersep=5pt,
linewidth=0.95\linewidth,
xleftmargin=0.05\linewidth,
breaklines=true
breakwhitespace=false,
}}
% Python environment
\lstnewenvironment{python}[1][]
{
\pythonstyle
\lstset{#1}
}
{}
% Bash style for highlighting
\newcommand\bashstyle{
\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
}}
% Bash environment
\lstnewenvironment{bash}[1][]
{
\bashstyle
\lstset{#1}
}
{}
\begin{document}
\lstlistoflistings
\section{test}
\begin{python}[caption=python example]
class MyClass(Yourclass):
def __init__(self, my, yours):
bla = '5 1 2 3 4'
print bla
\end{python}
\begin{bash}[caption=bash example]
$ sudo yum update -y
$ sudo yum install -y https://rdo.fedorapeople.org/rdo-release.rpm
$ sudo yum install packstack
\end{bash}
\end{document}
结果是这样的:
我们可以看到,两者都属于相同的列表,但是最好为这两种样式提供两个不同的列表,它们应该有单独的列表,具有单独的索引名称和单独的编号。例如,我应该有一个“Python 代码 1:python 示例”和一个“bash 代码 1:bash 示例”,我还应该有一个 python 代码列表和一个 bash 代码列表。
有可能做到吗?如果可以,该怎么做?
已编辑,更加清晰。
答案1
您可以尝试此代码,如果有问题请告诉我。我结合了此处的信息,以便计数器这里是两个不同的列表。
\documentclass{article}
\usepackage{regexpatch}% http://ctan.org/pkg/regexpatch
\usepackage{listings}% http://ctan.org/pkg/listings
\usepackage{xcolor}
% Some definitions to use color
% Default fixed font does not support bold face
\DeclareFixedFont{\ttb}{T1}{txtt}{bx}{n}{9} % for bold
\DeclareFixedFont{\ttm}{T1}{txtt}{m}{n}{9} % for normal
% Custom colors
\usepackage{color}
\definecolor{deepblue}{rgb}{0,0,0.5}
\definecolor{deepred}{rgb}{0.6,0,0}
\definecolor{deepgreen}{rgb}{0,0.5,0}
\newcounter{python}
\newcounter{bash}
\makeatletter
% --------------------------------------- Python
\newcommand{\lstlistpythonname}{List of Python Codes}
\lst@UserCommand\lstlistofpython{\bgroup
\let\contentsname\lstlistpythonname
\let\lst@temp\@starttoc \def\@starttoc##1{\lst@temp{lor}}%
\tableofcontents \egroup}
\lstnewenvironment{python}[1][]{%
\renewcommand{\lstlistingname}{Python Code}%
\let\c@lstlisting=\c@python
\let\thelstlisting=\thepython
\xpatchcmd*{\lst@MakeCaption}{lol}{lor}{}{}%
\lstset{language=Python,
basicstyle=\footnotesize,
otherkeywords={self},
tabsize=1,
keywordstyle=\ttb\color{deepblue},
emph={__init__, update_port},
emphstyle=\ttb\color{deepred},
stringstyle=\ttb\color{deepgreen},
frame=single,
showstringspaces=false,
float=htpb,
numbers=left,
captionpos=b,
numbersep=5pt,
linewidth=0.95\linewidth,
xleftmargin=0.05\linewidth,
breaklines=true
breakwhitespace=false,
#1}}
{}
% --------------------------------------- Bash
\newcommand{\lstlistbashname}{List of Bash 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}{Bash 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}
\lstlistofpython
\lstlistofbash
%\lstlistofpseudocode
\begin{bash}[caption=bash example]
$ sudo yum update -y
$ sudo yum install -y https://rdo.fedorapeople.org/rdo-release.rpm
$ sudo yum install packstack
\end{bash}
\begin{python}[caption=python example]
class MyClass(Yourclass):
def __init__(self, my, yours):
bla = '5 1 2 3 4'
print bla
\end{python}
\begin{python}[caption=python example]
class MyClass(Yourclass):
def __init__(self, my, yours):
bla = '5 1 2 3 4'
print bla
\end{python}
\begin{bash}[caption=bash example]
$ sudo yum update -y
$ sudo yum install -y https://rdo.fedorapeople.org/rdo-release.rpm
$ sudo yum install packstack
\end{bash}
\begin{python}[caption=python example]
class MyClass(Yourclass):
def __init__(self, my, yours):
bla = '5 1 2 3 4'
print bla
\end{python}
\begin{python}[caption=python example25]
class MyClass(Yourclass):
def __init__(self, my, yours):
bla = '5 1 2 3 4'
print bla
\end{python}
\end{document}