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

You can try defining a new theorem style:

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

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

Output:

enter image description here

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

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.