如何在新环境中合并框架和列表包

如何在新环境中合并框架和列表包

我总是使用这个 LaTeX 代码在我的文章中显示源代码。

\begin{listingbox}{VB style}
\begin{singlespace} 
\begin{lstlisting}[style=BASIC]

Private Sub Form_Load()
Dim tex As String
MsgBox "welcome to LaTeX"
End Sub 

\end{lstlisting}
\end{singlespace}
\end{listingbox}

哪里listingbox是一个基于framed包的环境。

那么,我怎样才能将它们全部组合到一个环境中?也许像这样

\begin{BASIC}
code here
\end{BASIC}

已编辑

这是我的 MWE,

\documentclass{scrartcl}
\usepackage[dvipsnames, cmyk]{xcolor}
\usepackage{listings}
\usepackage{setspace}
\usepackage{lmodern}
\usepackage{inconsolata}
\usepackage{framed}


\newdimen\errorsize \errorsize=0.2pt
% Frame with a label at top
\newcommand\LabFrame[2]{%
    \fboxrule=\FrameRule
    \fboxsep=-\errorsize
    \textcolor{FrameColor}{%
    \fbox{%
      \vbox{\nobreak
      \advance\FrameSep\errorsize
      \begingroup
        \advance\baselineskip\FrameSep
        \hrule height \baselineskip
        \nobreak
        \vskip-\baselineskip
      \endgroup
      \vskip 0.5\FrameSep
      \hbox{\hskip\FrameSep \strut
        \textcolor{TitleColor}{\textbf{#1}}}%
      \nobreak \nointerlineskip
      \vskip 1.3\FrameSep
      \hbox{\hskip\FrameSep
        {\normalcolor#2}%
        \hskip\FrameSep}%
      \vskip\FrameSep
    }}%
}}
\definecolor{FrameColor}{rgb}{0.98,0.83,0.36}
\definecolor{TitleColor}{rgb}{0,0,0}

\newenvironment{contlabelframe}[2][\Frame@Lab\ (cont.)]{% 
  % Optional continuation label defaults to the first label plus
  \def\Frame@Lab{#2}%
  \def\FrameCommand{\LabFrame{#2}}%
  \def\FirstFrameCommand{\LabFrame{#2}}%
  \def\MidFrameCommand{\LabFrame{#1}}%
  \def\LastFrameCommand{\LabFrame{#1}}%
  \MakeFramed{\advance\hsize-\width \FrameRestore} 
}{\endMakeFramed} 


\lstset
{
    breaklines=true,
    tabsize=3,
    showstringspaces=false
}
\definecolor{comments}{rgb}{0,0.6,0}
\definecolor{strings}{rgb}{0.9,0,0}
\definecolor{keywords}{rgb}{0.13,0.13,1}
\definecolor{identifier}{rgb}{0,0,0}

\lstdefinestyle{BASIC}
{
    extendedchars=\true,
    language={[Visual]Basic},  
    showspaces=false,
    showtabs=false,
    breaklines=true,
    showstringspaces=false,
    breakatwhitespace=true,
    escapeinside={(*@}{@*)},
    morekeywords={as,long, Double, String, to, variant, step},
    keywordstyle= \color{keywords}\bfseries,
    identifierstyle=\color{identifier},
    stringstyle=\color{strings},
    commentstyle=\color{comments},
    basicstyle=\fontsize{10}{12}\color{black}\ttfamily    
}

\newcounter{listingbox}[section]
\newenvironment{listingbox}[1]{%  
  \refstepcounter{listingbox}%
  \begin{contlabelframe}{\textbf{Listing \thesection.\thelistingbox:}\quad #1}
 \noindent\ignorespaces}
{\end{contlabelframe}} %

\makeatother

\begin{document}

    \section{Testing}

    \begin{listingbox}{VB style}
    \begin{singlespace} 
    \begin{lstlisting}[style=BASIC]
Private Sub Form_Load()
 Dim tex As String
 MsgBox "welcome to LaTeX"
End Sub         
    \end{lstlisting}
    \end{singlespace}
    \end{listingbox}

\end{document}

答案1

listings是“敏感的”...如果您将新创建的环境内的使用转换\lstnewenvironment为其命令形式的对应项,则可以通过并声明BASIC列表:

在此处输入图片描述

\documentclass{scrartcl}
\usepackage[dvipsnames, cmyk]{xcolor}
\usepackage{listings,setspace,framed}
\usepackage{lmodern,inconsolata}

\newdimen\errorsize \errorsize=0.2pt
% Frame with a label at top
\newcommand\LabFrame[2]{%
    \fboxrule=\FrameRule
    \fboxsep=-\errorsize
    \textcolor{FrameColor}{%
    \fbox{%
      \vbox{\nobreak
      \advance\FrameSep\errorsize
      \begingroup
        \advance\baselineskip\FrameSep
        \hrule height \baselineskip
        \nobreak
        \vskip-\baselineskip
      \endgroup
      \vskip 0.5\FrameSep
      \hbox{\hskip\FrameSep \strut
        \textcolor{TitleColor}{\textbf{#1}}}%
      \nobreak \nointerlineskip
      \vskip 1.3\FrameSep
      \hbox{\hskip\FrameSep
        {\normalcolor#2}%
        \hskip\FrameSep}%
      \vskip\FrameSep
    }}%
}}
\definecolor{FrameColor}{rgb}{0.98,0.83,0.36}
\definecolor{TitleColor}{rgb}{0,0,0}

\newenvironment{contlabelframe}[2][\Frame@Lab\ (cont.)]{% 
  % Optional continuation label defaults to the first label plus
  \def\Frame@Lab{#2}%
  \def\FrameCommand{\LabFrame{#2}}%
  \def\FirstFrameCommand{\LabFrame{#2}}%
  \def\MidFrameCommand{\LabFrame{#1}}%
  \def\LastFrameCommand{\LabFrame{#1}}%
  \MakeFramed{\advance\hsize-\width \FrameRestore} 
}{\endMakeFramed} 


\lstset
{
    breaklines=true,
    tabsize=3,
    showstringspaces=false
}
\definecolor{comments}{rgb}{0,0.6,0}
\definecolor{strings}{rgb}{0.9,0,0}
\definecolor{keywords}{rgb}{0.13,0.13,1}
\definecolor{identifier}{rgb}{0,0,0}

\lstdefinestyle{BASIC}
{
    extendedchars=\true,
    language={[Visual]Basic},  
    showspaces=false,
    showtabs=false,
    breaklines=true,
    showstringspaces=false,
    breakatwhitespace=true,
    escapeinside={(*@}{@*)},
    morekeywords={as,long, Double, String, to, variant, step},
    keywordstyle= \color{keywords}\bfseries,
    identifierstyle=\color{identifier},
    stringstyle=\color{strings},
    commentstyle=\color{comments},
    basicstyle=\fontsize{10}{12}\color{black}\ttfamily    
}

\newcounter{listingbox}[section]
\newenvironment{listingbox}[1]{%  
  \refstepcounter{listingbox}%
  \contlabelframe{\textbf{Listing \thesection.\thelistingbox:}\quad #1}
 \noindent\ignorespaces}
{\endcontlabelframe} %

\lstnewenvironment{BASIC}[1]
  {\listingbox{#1}\singlespace
   \lstset{style=BASIC}}
  {\endsinglespace\endlistingbox}
\makeatother

\begin{document}

\section{Testing}

\begin{listingbox}{VB style}
\begin{singlespace} 
\begin{lstlisting}[style=BASIC]
Private Sub Form_Load()
 Dim tex As String
 MsgBox "welcome to LaTeX"
End Sub         
\end{lstlisting}
\end{singlespace}
\end{listingbox}

\begin{BASIC}{VB style}
Private Sub Form_Load()
 Dim tex As String
 MsgBox "welcome to LaTeX"
End Sub         
\end{BASIC}

\end{document}

我还必须将其更改为contlabelframe内部的命令形式,因为它在内部使用。listingboxBASIC

相关内容