5

I want to include listings of external files in my document. These listings should have a solid background and rounded corners. The problem is, that if I define a style with frameround and backgroundcolor, the background overlaps the rounded corners.

\lstdefinestyle{cftemplate}{
    frameround=tttt,
    rulecolor=\color[rgb]{0.7, 0.7, 0.7},
    frame=single,
    backgroundcolor=\color[rgb]{0.95, 0.95, 0.95}
}

There already exists a question how this behavior can be prevented in a lstlisting. In particular the solution presented in this answer works well for me. So I tried to extend this solution so that it also works with \lstinputlisting. However, the obvious attempt to replace "lstlisting" with "lstinputlisting" in the code snippet did not work.

So my question is: How can I create a \lstinputlisting with a background and rounded corners, where the background does not overlap the rounded corners?

6

If you are willing to use the tcolorbox package and its listings library, this is easy; using \newtcbinputlisting you can define a customizable macro which behaves pretty much as \lstinputlisting and with \newtcblisting you define your customizable version of lstlisting:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{beramono}% just for the example
\usepackage[many]{tcolorbox}
\tcbuselibrary{listings}

\newtcbinputlisting{\myinputlisting}[2][]{%
  listing engine=listings,
  listing file={#2},
  colback=red!15,
  colframe=red!75!black,
  arc=12pt,
  listing only,
  listing options={
    language={[LaTeX]TeX},
    basicstyle=\ttfamily\small
  },
  breakable,
  #1
}
\newtcblisting{mylisting}[1][]{%
  listing engine=listings,
  colback=red!15,
  colframe=red!75!black,
  arc=12pt,
  listing only,
  listing options={
    language={[LaTeX]TeX},
    basicstyle=\ttfamily\small
  },
  breakable,
  #1
}


\usepackage{filecontents}
\begin{filecontents*}{Gtest1.tex}  
\newlength\mylena
\newlength\mylenb
\setlength\mylena{2cm}
\setlength\mylenb{4cm}
\addtolength\mylena{\mylenb}
\end{filecontents*}

\begin{document}

\myinputlisting{Gtest1.tex}

\begin{mylisting}[]
\documentclass{article}
\begin{document}
test
\end{document}
\end{mylisting}

\end{document}

enter image description here

  • It was a bit tricky, because apparently I had an outdated version of the tcolorbox package. But now it looks very nice. Thanks! – Michael Osl Jul 16 '14 at 20:38

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.