2

I am attempting to have a listing inside of an enumerate environment, however, the text falls outside of the box like this:

enter image description here

Can you tell me how to adjust the text or widen the framed box so that the text appears correctly?

Here is the code:

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{listings,skins}

\lstdefinestyle{mystyle}{
numbers=left,
numberstyle=\small,
numbersep=8pt,
language=Pascal
}

\newtcblisting{mylisting}[2][]{
    arc=0pt, outer arc=0pt,
    listing only,
    listing style=mystyle,
    title=#2,
    #1
    }

\begin{document}

\begin{enumerate}
\item This is an example
\noindent
\begin{mylisting}[hbox,enhanced,drop shadow,width=6in]{\bfseries{vpasolve for $\mathbf{f(y)}$}}
syms y
vpasolve((1.2*y)*(1-(y/15)) - (4*y)/(0.5+y) == 0, y)
\end{mylisting}
\end{enumerate}

\end{document}
1
  • You should read some questions about line breaks in listings.
    – TeXnician
    May 27, 2017 at 20:09

2 Answers 2

4

Here's a version with linebreak. It has to use an own style, because you probably don't want to decrease the line width for all of your listings (outside from tcolorboxes).

listing

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{listings,skins}

\lstdefinestyle{mystyle}{
numbers=left,
numberstyle=\small,
numbersep=8pt,
language=Pascal,
}

\lstdefinestyle{mytcbstyle}{
numbers=left,
numberstyle=\small,
numbersep=8pt,
language=Pascal,
breaklines=true,
postbreak=\raisebox{0ex}[0ex][0ex]{\ensuremath{\color{red}\hookrightarrow\space}},
linewidth=.8\linewidth
}

\newtcblisting{mylisting}[2][]{
    arc=0pt, outer arc=0pt,
    listing only,
    listing style=mytcbstyle,
    title=#2,
    #1
    }

\begin{document}

\begin{enumerate}
\item This is an example
\noindent
\begin{mylisting}[hbox,enhanced,drop shadow,width=6in]{\bfseries{vpasolve for $\mathbf{f(y)}$}}
syms y
vpasolve((1.2*y)*(1-(y/15)) - (4*y)/(0.5+y) == 0, y)
\end{mylisting}
\end{enumerate}

\end{document}
4
  • thanks for your help. Is there a solution to possibly shift the text closer to the left side of the box instead so that the text will fit inside the box? Thanks!
    – Joe
    May 27, 2017 at 20:22
  • 1
    @Joe You could try left=1pt or any length you find appropriate as option to newtcblisting.
    – TeXnician
    May 27, 2017 at 20:23
  • Thanks, that apparently did not work as expected. Although the text shifted, there was still some fallout. I then tried to increase the box width width = 5cm, but that did not work. How then to increase the box width?
    – Joe
    May 27, 2017 at 20:27
  • 1
    @Joe Use text width, but that will probably go to the right. Your problem seems to be specific to listings, because the internal lstlisting will have a left margin (you should look it up in its documentation).
    – TeXnician
    May 28, 2017 at 7:16
3

Loading enumitem and using the wide option for enumerate seems to solve the problems:

\documentclass{article}
\usepackage{tcolorbox}
\usepackage{showframe, enumitem}
\renewcommand\ShowFrameLinethickness{0.3pt}
\tcbuselibrary{listings,skins}

\lstdefinestyle{mystyle}{
numbers=left,
numberstyle=\small,
numbersep=8pt,
language=Pascal
}

\newtcblisting{mylisting}[2][]{
    arc=0pt, outer arc=0pt,
    listing only,
    listing style=mystyle,
    title=#2,
    #1
    }

\begin{document}

\begin{enumerate}[wide]
\item This is an example
\begin{mylisting}[hbox,enhanced,drop shadow]{\bfseries{vpasolve for $\mathbf{f(y)}$}}
syms y
vpasolve((1.2*y)*(1-(y/15)) - (4*y)/(0.5+y) == 0, y)
\end{mylisting}
\end{enumerate}

\end{document} 

enter image description here

4
  • Thanks for your solution. Can this be made local to one specific item? I have other enumerate blocks and other items. Your solution is the closest that seems to satisfy my requirement, however, it is affecting the layout of the other enumerate environments. Can your solution be tailored to just that specific item? Thanks again!
    – Joe
    May 27, 2017 at 21:50
  • 1
    It is very easy: you can close the enumerate environment right after this item, and open immediately a new one with option resume which will start the counter from the previous one. See details in the enumitem documentation.
    – Bernard
    May 27, 2017 at 21:56
  • Thank you! Is there an easy option however to increase the width of the box itself without affecting anything else? I tried width = 5cm, but that had no effect. Thanks again!
    – Joe
    May 27, 2017 at 22:05
  • 1
    I don't know very well tcolorbox, but my first idea – temporarily increasing the right margin – doesn't work. I'll to try to see the details for tcbox widths later.
    – Bernard
    May 27, 2017 at 22:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .