2

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?

2
  • Welcome! Please provide something we can compile to reproduce the problem rather than just fragments of code.
    – cfr
    Sep 14, 2016 at 0:31
  • Thanks! I'm not the right person to answer, unfortunately, but your update will definitely help those who can do so. Off-topic: hyperref should be loaded last, with a few notable exceptions, or you'll have problems.
    – cfr
    Sep 14, 2016 at 2:13

1 Answer 1

4

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

1
  • Thank you! That was the issue. The other issue I faced was not realizing that some of the code needed to go inside of the listing options section.
    – kojow7
    Sep 14, 2016 at 15:50

You must log in to answer this question.

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