[Tex/LaTex] Breaking lines in a tcolorbox

line-breakingtcolorbox

I am creating a book in TeXstudio and I have the following code:

\newtcblisting[auto counter,number within=chapter]{sourcecode}[2][]{sharp corners, breakable,
    fonttitle=\bfseries, colframe=gray, listing only, 
    listing options={basicstyle=\ttfamily,language=php, showstringspaces=false, breakatwhitespace=true, breaklines=true, tabsize=4}, 
    title=Code Snippet \thetcbcounter: #2, #1}

I managed to use the breakable option to break across pages, but trying to use breaklines=true to break individual lines does not work as it doesn't seem to be a valid option for tcolorbox.

By itself the following code works to break lines:

\lstset{
    breaklines=true,
    postbreak=\raisebox{0ex}[0ex][0ex]{\ensuremath{\color{red}\hookrightarrow\space}}
}

but then I lose my colorbox style. How can I combine these two styles into one? I do not necessarily need the nice red arrow, but a bit of indentation on the broken line is preferred.

A complete code snippet may look something like this:

\documentclass{book}
\usepackage{hyperref}
\usepackage[table]{xcolor}
\usepackage{listings}
\usepackage[most]{tcolorbox}
\usepackage{inconsolata}
\usepackage{graphicx}
\tcbuselibrary{breakable}

\newtcblisting[auto counter,number within=chapter]{sourcecode}[2][]{sharp corners, breakable,
    fonttitle=\bfseries, colframe=gray, listing only, 
    listing options={basicstyle=\ttfamily,language=php, showstringspaces=false, breakatwhitespace=true, breaklines=true, tabsize=4}, 
    title=Code Snippet \thetcbcounter: #2, #1}


\begin{document}

\begin{sourcecode}{}
    <?php

    function abc($file_name){

        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="'.$file_name.'"');
        header('Cache-Control: max-age=0');
        $writer->save('php://output');
    }

\end{sourcecode}

\end{document}

When compiled it looks like this:

enter image description here

How can I break this long line and have it indented so that it flows within the box and lines up correctly in the code?

Best Answer

The choice breakatwhitespace=true allows line breaks only at white space, while that your line code does not contain white space, just remove your choice breakatwhitespace=true

Code

\documentclass{book}
\usepackage{hyperref}
\usepackage[table]{xcolor}
\usepackage{listings}
\usepackage[most]{tcolorbox}
\usepackage{inconsolata}
\usepackage{graphicx}
\tcbuselibrary{breakable}

\newtcblisting[auto counter,number within=chapter]{sourcecode}[2][]{sharp corners, breakable,
    fonttitle=\bfseries, colframe=gray, listing only, 
    listing options={basicstyle=\ttfamily,language=php, showstringspaces=false, 
    breaklines=true, postbreak={\raisebox{0ex}[0ex][0ex]{\ensuremath{\color{red}\hookrightarrow\space}}}, tabsize=4
  }, 
    title=Code Snippet \thetcbcounter: #2, #1}


\begin{document}

\begin{sourcecode}{}
    <?php

    function abc($file_name){

        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="'.$file_name.'"');
        header('Cache-Control: max-age=0');
        $writer->save('php://output');
    }

\end{sourcecode}

\end{document}

enter image description here

Related Question