1

I use lstset to print my code. My problem is, that my code is too long for one page, so it gets cut off. But i want just new pages instead of this cutting. I tried escapechar with the parameter '$', but this doesn't worked.

\documentclass[11pt,a4paper]{scrartcl}
\setlength{\parindent}{0pt}

\lstset{
    basicstyle  =   \footnotesize,
    keywordstyle    = \color{keywordcolor}\bfseries,
    stringstyle     = \color{strings},
    identifierstyle = \color{black},
    commentstyle    = \color{commentgreen},
    breaklines=true,
    numbersep=-10pt,
    stepnumber=1,
    showspaces=false,
    escapechar=§,
    showstringspaces=false,
    showtabs=false,
    frame=single,  
    rulecolor=\color{black},
    tabsize=4,              
    captionpos=t,           
    breaklines=true,
    breakatwhitespace=false,
    numbers=left,
    extendedchars=\true,
    emph=[3]{href},
    emphstyle=[3]{\color{keywordcolor}},
    backgroundcolor=\color{bggrey} 
}

\begin{document}

\begin{minipage}{\textwidth} 
\lstset{language=bash,caption={My bash script},label=DescriptiveLabel}
\lstinputlisting[language=bash]{bash.sh}
\end{minipage}

\end{document}

But this one just cut off my code at the end of the page and thats it - no new page (also, in my bash.sh there is at exactly this point the parameter '$' for a break). Here's a screenshot of my problem:

example

  • Are you fixed to lstlisting or would you accept an approach with tcolorbox? – TeXnician Mar 17 '17 at 19:45
  • I'm flexible. Whats your solution approach with tcolorbox? – idontknowwhoiamgodhelpme Mar 17 '17 at 19:56
2

Here's some code for breakable listings (partially from tcolorbox documentation). It defines the environment codeblock.

listings (breakable)

\documentclass[11pt,a5paper,parindent=false]{scrartcl}

\usepackage[svgnames]{xcolor}
\usepackage{tcolorbox}
\tcbuselibrary{skins,breakable,listings}

\lstset{
    basicstyle  =   \footnotesize,
    keywordstyle    = \color{keywordcolor}\bfseries,
    stringstyle     = \color{strings},
    identifierstyle = \color{black},
    commentstyle    = \color{commentgreen},
    breaklines=true,
    numbersep=3pt,
    stepnumber=1,
    showspaces=false,
    escapechar=§,
    showstringspaces=false,
    showtabs=false,
    rulecolor=\color{black},
    tabsize=4,              
    captionpos=t,           
    breaklines=true,
    breakatwhitespace=false,
    numbers=left,
    extendedchars=\true,
    emph=[3]{href},
    emphstyle=[3]{\color{keywordcolor}}
}

\newtcblisting[use counter=lstlisting]{codeblock}[3][]{%
        enhanced,noparskip,breakable,colback=LightGray!50,colframe=DarkSlateGray,opacitybacktitle=.8,%
        sharp corners=downhill,attach boxed title to top center = {yshift=-0.25mm-\tcboxedtitleheight/2,yshifttext=2mm-\tcboxedtitleheight/2},%
        boxed title style = {size=small,boxrule=0.5mm,frame code={\path[tcb fill frame] ([xshift=-4mm]frame.west)
                -- (frame.north west) -- (frame.north east) -- ([xshift=4mm]frame.east)
                -- (frame.south east) -- (frame.south west) -- cycle;},
            interior code={\path[tcb fill interior] ([xshift=-2mm]interior.west)
                -- (interior.north west) -- (interior.north east)
                -- ([xshift=2mm]interior.east) -- (interior.south east) -- (interior.south west)
                -- cycle;}
        },fonttitle=\bfseries,title={\strut\lstlistingname~\thelstlisting:~#2},%
        title after break={\centering\footnotesize\itshape\strut\lstlistingname~\thelstlisting~--~cont.},%
        listing only,label=#3,#1%
    }


\begin{document}
\lstset{language=bash,caption={My bash script},label=DescriptiveLabel}
\begin{codeblock}[]{Title}
foo(x)
foo(x)
foo(x)
foo(x)
foo(x)
foo(x)
foo(x)
foo(x)
foo(x)
foo(x)
foo(x)
foo(x)
foo(x)
foo(x)
foo(x)
foo(x)
foo(x)
foo(x)
foo(x)
foo(x)
foo(x)
foo(x)
foo(x)
foo(x)
foo(x)
foo(x)
foo(x)
foo(x)
foo(x)
foo(x)
foo(x)
foo(x)
foo(x)
foo(x)
foo(x)
foo(x)
\end{codeblock}
\end{document}
| improve this answer | |
  • Looks good, but i always get this error: Package inputenc Error: inputenc is not designed for xetex or l uatex. (inputenc) only UTF-8 supported. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.168 \end{codeblock} – idontknowwhoiamgodhelpme Mar 17 '17 at 20:36
  • @ipo I edited the code. Now only a few tcblibraries are used and that error belongs to the past. – TeXnician Mar 17 '17 at 20:56

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.