3

I always use this LaTeX code to display a source code in my article.

\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}

Where listingbox is an environment based on framed packages.

So, how can I combine all of them into one environment? maybe like this one

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

Edited

This my 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}
  • 3
    How is listingbox defined? Why are you not using the framing options of the lstings package? – Andrew Swann May 28 '13 at 15:07
  • 2
    Something along \lstnewenvironment{BASIC}{\listingbox{VB style}\singlespacing\lstset{style=BASIC}}{\listingbox} should work – clemens May 28 '13 at 15:11
  • Welcome to TeX.SX! You can have a look on our starter guide to familiarize yourself further with our format. – mafp May 28 '13 at 15:21
  • @cgnieder Presumably you meant {\endlistingbox} as the final group. – Andrew Swann May 28 '13 at 15:30
  • @AndrewSwann of course! – clemens May 28 '13 at 15:39
0

listings is "sensitive"... If you convert the usage of your environments inside a newly created \lstnewenvironment to their command-form counterparts, you can get by and declare a BASIC listing:

enter image description here

\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}

I had to change to the command-form of contlabelframe inside listingbox as well, since it is internally used by BASIC.

  • Thank you. This is exactly what I mean. Just one environment for replacing the old. – Pattisahusiwa Sep 12 '13 at 6:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.