# Listings environment within another environment

\newtheorem{alg}{Algorithm}[subsection]

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


How can I prevent the above code damaging the code frame like below:

I have tried using the caption = ... argument but that limits my freedom without making a lot more code.

• Welcome to TeX.SX! May I ask you what you're trying to achieve? – karlkoeller Dec 11 '13 at 18:15
• I want to label a section of code "Algorithm 4.0.1. (in this case)" using a self defined theorem "alg" for which I can use later to maintain algorithm numbering. – user2850514 Dec 11 '13 at 18:17
• Do you have different algorithms that don't contain listings? – karlkoeller Dec 11 '13 at 18:18
• Yes, which was my initial problem, so I thought this would work but it then damages the listings frame. – user2850514 Dec 11 '13 at 18:21

You can try defining a new theorem style:

\newtheoremstyle{algo}%     <name>
{\topsep}%                <space above>
{\topsep}%                <space below>
{\itshape}%               <body font>
{}%                       <indent amount>


and using it for your alg theorem.

MWE:

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

\newtheoremstyle{algo}%     <name>
{\topsep}%                <space above>
{\topsep}%                <space below>
{\itshape}%               <body font>
{}%                       <indent amount>
\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}


Output:

• @user2850514 You're welcome. Let me know if you need any more adjustment. – karlkoeller Dec 11 '13 at 19:06