# lstlisting in landscape and heading in portrait

I am trying to include some code files in landscape mode into my document which is in portrait mode. I do this using the

\usepackage{lscape}


and

\usepackage{listings}


So far so good. The problem kicks in when a new section begins. The heading is shown in portrait mode (which is fine!) followed by a blank page. On the next page the code appears. Is it possible to combine the portrait mode heading with the landscape mode code on one page? Below is a little example

\documentclass[12pt,a4paper,leqno]{book}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\usepackage{lscape}

\begin{document}
\section*{This is a section}
\begin{landscape}
\begin{lstlisting}
and here comes the code
\end{lstlisting}
\end{landscape}
\end{document}

• Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – user31729 Aug 14 '14 at 6:09
• With lscape this is not possible. If you have some textbefore or after the lstlisting environment, you may use the afterpage package to fill in the space after section*. – user11232 Aug 14 '14 at 6:24

You could add a rotated box with a (short) listing after the section title. My answer uses the tcolorbox package for the job. The given width of the box is the height of the rotated box. Obviously, the width/height has to be small enough to fit inside the page. This solution is not adequate, if you want to the listing to continue to the next page. Any options for the listing can be put inside the listing options key.

\documentclass[12pt,a4paper,leqno]{book}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[listings,skins]{tcolorbox}

\begin{document}
\section*{This is a section}

\begin{tcblisting}{width=19cm,listing only,blank,tikz={rotate=90},
listing options={basicstyle=\ttfamily}}
and here comes the code
\end{tcblisting}

\end{document}


• Thank you for the help Thomas, unfortunately my listing goes over several pages :( – Fredo Aug 14 '14 at 11:09
• @Fredo That's a pitty... :-( – Thomas F. Sturm Aug 14 '14 at 11:51