如何为列表添加标题(?)环境,自定义 Python 语法高亮环境

如何为列表添加标题(?)环境,自定义 Python 语法高亮环境

我在用这个答案将语法高亮的 Python 代码片段添加到我的文档中

\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{newtxtt}

% 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=\ttfamily,
    morekeywords={self},              % Add keywords here
    keywordstyle=\bfseries\color{deepblue},
    emph={MyClass,__init__},          % Custom highlighting
    emphstyle=\bfseries\color{deepred},    % Custom highlighting style
    stringstyle=\color{deepgreen},
    frame=tb,                         % Any extra options here
    showstringspaces=false,
  }%
}

\lstset{literate={µ}{\textmu}{1}}


% Python environment
\lstnewenvironment{python}[1][]
  {\pythonstyle\lstset{#1}}
  {}

% Python for external files
\newcommand\pythonexternal[2][]{{%
  \pythonstyle
  \lstinputlisting[#1]{#2}%
}}

% Python for inline
\newcommand\pythoninline[1]{{\pythonstyle\lstinline!#1!}}

\begin{document}
\begin{python}
µ = 5
for f
\end{python}
\end{document}

在此处输入图片描述

我想给它加标题,类似于给表格或图形加标题。

我试过了

\begin{python}[label=code1]
do_some_code = []
\end{python}
\captionof{lstlisting}{This is how some code was done}

它看起来不错,但给出了警告“\captionof 超出框或环境”,所以我担心随着文档变得越来越复杂,它会变得不可靠或崩溃。

我也尝试过按照建议

\begin{python}[label=code1,caption={This is how some code was done}]
do_some_code = []
\end{python}

但这会将标题置于代码之上,这是不可取的。

我希望将标题放在代码下方。

答案1

有时,阅读手册会有所帮助。第 4.3.8 节专门介绍字幕和节目

在此处输入图片描述

因此,您可以设置标题位置每一个列出带有标题的列表

\lstset{captionpos=b}

在文件序言中。

\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{newtxtt}

% Default fixed font does not support bold face
%\DeclareFixedFont{\ttb}{T1}{txtt}{bx}{n}{12} % for bold
%\DeclareFixedFont{\ttm}{T1}{txtt}{m}{n}{12}  % 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=\ttfamily,
    morekeywords={self},              % Add keywords here
    keywordstyle=\bfseries\color{deepblue},
    emph={MyClass,__init__},          % Custom highlighting
    emphstyle=\ttb\color{deepred},    % Custom highlighting style
    stringstyle=\color{deepgreen},
    frame=tb,                         % Any extra options here
    showstringspaces=false,
  }%
}

\lstset{
  captionpos=b,
  literate={µ}{\textmu}{1},
}


% Python environment
\lstnewenvironment{python}[1][]
  {\pythonstyle\lstset{#1}}
  {}

% Python for external files
\newcommand\pythonexternal[2][]{{%
  \pythonstyle
  \lstinputlisting[#1]{#2}%
}}

% Python for inline
\newcommand\pythoninline[1]{{\pythonstyle\lstinline!#1!}}

\begin{document}

\begin{python}[caption={Text of the caption},label=whatever]
µ = 5
for f
\end{python}

\end{document}

在此处输入图片描述

相关内容