2

I have my own lstlistings environments which are framed. Since I wanted to adjust the width so the frame starts with the text I used a minipage inside the lstlisting. (See MWE).

But as I have quite long code fragments I'd like to allow Page-Breaks inside the listing which is not possible using minipage.

I was not able to achieve the same thing using \adjustwidth{..}.

Any ideas? (I am using XeLatex)

\documentclass[a4paper]{article}
\usepackage{polyglossia}
\setdefaultlanguage[]{english}

\usepackage{listings}
\lstset{frame=single}
\usepackage{ifthen}
\lstnewenvironment{example}[1]
  {
  \flushleft \textbf{Example }\def\ExampleDescription{#1\\}\\
  \ExampleDescription
  \hspace*{\dimexpr\fboxrule+\fboxsep}% 
  \minipage{\dimexpr\textwidth-\fboxsep-\fboxrule}% 
  }{
  \endminipage
  }
\usepackage{lipsum}
\begin{document}

\lipsum[2-6]
\begin{example}{Description}
hello
this is a code example
that should
be long
enough
to provoke a pagebreak
\end{example}
\end{document}
  • minipage is unbreakable. Why don't you just use the linewidth option of the listings? – masu Nov 18 '13 at 11:51
3

(This doesn't really concern Xetex so I excluded that from my example.)

Sorry, I've misunderstood your question at first. A minipage is unbreakable by default, but you can use the xleftmargin=3pt and xrightmargin=3pt option of lstlisting. 3pt comes from the default value of the framesep option. If you change it, change these too (it can be automatized, but it's huge overkill if you don't really need it).

enter image description here

\documentclass[a4paper]{article}

\usepackage{listings}
\lstset{frame=single, xleftmargin=3pt, xrightmargin=3pt}
\usepackage{ifthen}
\lstnewenvironment{example}[1]
  {
  \flushleft \textbf{Example }\def\ExampleDescription{#1\\}\\
  \ExampleDescription
  \hspace*{\dimexpr\fboxrule+\fboxsep}%
  }{
  }
\usepackage{lipsum}
\begin{document}

\lipsum[2-6]
\begin{example}{Description}
hello
this is a code example
that should
be long
enough
to provoke a pagebreak
\end{example}
\end{document}
  • Well the frame still goes outside the text-range... I tried that as well – Kathiieee Nov 18 '13 at 13:47
  • @Kathiieee I misunderstood your question. Is it okay now? – masu Nov 18 '13 at 15:03
  • it works perfectly, you saved my day :) – Kathiieee Nov 18 '13 at 15:10
  • @Kathiieee You should give that an upvote then. ;) – masu Nov 18 '13 at 16:36

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.