1

I'd like to use the hbox option with \tcbinputlisting since it automatically adjust the background color and the box to the width of the code listing. The problem is that if the code listing is more than one page long, it only shows the first page (it can't break the hbox over multiple pages?)

I'd like to ask if someone knows a trick to allow one to use hbox and still show all the pages? Without hbox option, I lose the main advantage of using this package, and can just use the listing package directly.

Here is a MWE of the problem

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[margin=1in]{geometry}
\usepackage{listings}
\usepackage{color}
\definecolor{bg}{RGB}{240,240,240}    
\usepackage{upquote} %to fix string quotes
\usepackage[most]{tcolorbox}
\tcbuselibrary{listings}
\usepackage{matlab-prettifier}

\begin{document}
\tcbinputlisting{nobeforeafter,enhanced,listing only,hbox,
   breakable,listing file=HW2.m,size=small,
   listing options={framerule=0.05pt,style=Matlab-editor,numbers=left,
       numberstyle=\scriptsize,basicstyle=\ttfamily\small,breaklines = true,
       escapechar=`,showspaces=false,mlshowsectionrules = true}}
\end{document}

output is

Mathematica graphics

This problem also happens when using \begin{tcblisting}..code..\end{tbclisting} using same options.

You can use any file to see the problem, as long as the file is over a page long in code. If you like to use the above HW2.m file used in the example above, here it is HW2.m

Again, the main reason I want to use hbox, is that it shrinks the background to the code size, which makes it look better. Here is reference how-to-crop-background-color-in-listings-to-the-longest-line-in-the-code

  • Perhaps the blend into=listings is a way to get of this trouble! Personally, I don't see any advantage of hbox – user31729 Jun 23 '16 at 12:18
4

hbox don't break over page -- see the documentation. But you can store the box in an array and retrieve the width:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[margin=1in]{geometry}
\usepackage{listings}
\usepackage[most]{tcolorbox}

\begin{filecontents}{mylisting.txt}
blbub
blblb
\end{filecontents}
\begin{document}
\newboxarray{mylisting}
\tcbinputlisting{nobeforeafter,enhanced,listing only,hbox,
                 listing file=mylisting.txt,
                 size=small,
                 store to box array=mylisting,
                 listing options={framerule=0.05pt,numbers=left,
                                  numberstyle=\scriptsize,basicstyle=\ttfamily\small,
                                  breaklines = true,showspaces=false}}

\boxarraygetwidth[mylisting]{\mylistingwidth}{1}

\tcbinputlisting{nobeforeafter,enhanced,listing only,
                 breakable,listing file=mylisting.txt,size=small,
                 width=\mylistingwidth,
                 listing options={framerule=0.05pt,numbers=left,
                                  numberstyle=\scriptsize,
                                  basicstyle=\ttfamily\small,
                                  breaklines = true,showspaces=false}}

\end{document}

enter image description here

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.