0

I'm using lstlisting with tcolorbox to show some code, but I have a problem with the width of the box. If one of the lines is longer than the document width, then because I have breaklines and breakatwhitespace to true, the code is splitted but the size of the box continues being as if it weren't splitted. Due to the special size of my document this makes the box to do not end when the document ends. If you insert the colorbox in between an adjustbox you can see how long it is. How can I solve this?

PS. If you remove the "test" words you can see how the box is reduced. Can I have a fixed size box for the width of the page?

\documentclass{article}

\usepackage[includehead,paperwidth=155mm, paperheight=235mm,left=17mm,right=23mm,top=7mm,bottom=23mm,headheight=9mm]{geometry}
\special{papersize=155mm,235mm}
\usepackage{times}
\usepackage [english] {babel}
\usepackage [T1]{fontenc}
\usepackage [utf8]{inputenc}
\selectlanguage{english} 
\usepackage{tcolorbox}  %For codes
\tcbuselibrary{listings,skins}  %For codes
\usepackage{adjustbox} 

\lstdefinestyle{code_bash}{
    language=bash,
    aboveskip=3mm,
    belowskip=3mm,
    showstringspaces=false,
    columns=fullflexible,
    basicstyle={\small\ttfamily},
    numbers=none,
    breaklines=true,
    breakatwhitespace=true,
    tabsize=3
}

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

\begin{document}
%\begin{adjustbox}{max width=\textwidth}
    \begin{bashcode}[hbox,enhanced,drop shadow]{Installing packages}
        sudo apt-get install build-essential
        sudo apt-get install cmake git test test  test test test test test test  test test  test test  test test  test test  
    \end{bashcode}
%end{adjustbox}

\end{document}
2
  • 1
    don't use the hbox option. Dec 29 '20 at 10:41
  • it works. May you post it as an answer so I can give you the tick? Dec 29 '20 at 10:44
1

Remove the hbox option from here:

 \begin{bashcode}[hbox,enhanced,drop shadow]{Installing packages}
                  ^^^^

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.