另一个环境中的 Listings 环境

另一个环境中的 Listings 环境
\newtheorem{alg}{Algorithm}[subsection]

\begin{alg}\label{alg:1}
\begin{lstlisting}[frame = single, breaklines = true, numbers = left]
test code
\end{lstlisting}
\end{alg}

我怎样才能防止上述代码损坏如下所示的代码框架:

我曾尝试使用该caption = ...参数,但这限制了我的自由,而没有编写更多的代码。

答案1

您可以尝试定义一种新的定理样式:

\newtheoremstyle{algo}%     <name>
  {\topsep}%                <space above>
  {\topsep}%                <space below>
  {\itshape}%               <body font>
  {}%                       <indent amount>
  {\bfseries}%              <theorem head font>
  {}%                       <punctuation after theorem head>
  {\newline}%               <space after theorem head>
  {}%                       <theorem head spec>

并将其用于你的alg定理。

梅威瑟:

\documentclass{article}
\usepackage{listings}
\usepackage{amsthm}

\newtheoremstyle{algo}%     <name>
  {\topsep}%                <space above>
  {\topsep}%                <space below>
  {\itshape}%               <body font>
  {}%                       <indent amount>
  {\bfseries}%              <theorem head font>
  {}%                       <punctuation after theorem head>
  {\newline}%               <space after theorem head>
  {}%                       <theorem head spec>
\theoremstyle{algo}
\newtheorem{alg}{Algorithm}[subsection]

\begin{document}
\begin{alg}\label{alg:1}
\begin{lstlisting}[frame = single, breaklines = true, numbers = left]
test code
\end{lstlisting}
\end{alg}
\end{document} 

输出:

在此处输入图片描述

相关内容