1

When using listings's linerange or firstline/lastline, the tcolorbox shows extra top and bottom padding, and the font is different. Is there a way to fix this, or is this a bug with tcolorbox that should be reported/fixed?

MWE:

\documentclass{article}

\begin{filecontents*}[overwrite]{test.txt}
line 1
line 2
line 3
\end{filecontents*}

\usepackage{listings}
\usepackage[listings, xparse]{tcolorbox}
\tcbset{listing only, sharp corners, top=0pc, bottom=0pc, left=0pc, right=0pc}

%\lstset{basicstyle=\small\ttfamily} % Partially fixes the font difference?
\tcbset{listingsstyle/.style={listing engine=listings}}
\NewTCBListing{cmd}{ O{} }{listingsstyle, #1}
\NewTCBInputListing{\cmdinput}{ m O{} }{listing file={#1}, listingsstyle, #2}

\begin{document}

\begin{cmd}[title={Cmd environment}]sample text\end{cmd}
\cmdinput{test.txt}[title={Cmd input command (full)}]
\cmdinput{test.txt}[title={Cmd input command (partial)},listing options={linerange={2-3}}]

\end{document}

Output:

enter image description here

2
  • Add listing style=tcblatex as an argument in the last \cmdinput
    – Nico
    Jul 22, 2020 at 7:13
  • \cmdinput{test.txt}[title={Cmd input command (partial)},listing options={style=tcblatex, linerange={2-3}}]
    – Nico
    Jul 22, 2020 at 7:19

1 Answer 1

2

You have not specified a style for the listing options.

"This removes all options for the listings package. This includes the tcblisting standard style tcblatex and the encoding presets. Use this option, if you want to set the listings options outside of tcblisting, e.g. globally in the preamble."

as written on page 314 of the tcolorbox manual. So if you add style=tcblatex it works as you would expect it.

MWE based on your code

\documentclass{article}

\begin{filecontents*}[overwrite]{test.txt}
line 1
line 2
line 3
\end{filecontents*}

\usepackage{listings}
\usepackage[listings, xparse]{tcolorbox}
\tcbset{listing only, sharp corners, top=0pc, bottom=0pc, left=0pc, right=0pc}

%\lstset{basicstyle=\small\ttfamily} % Partially fixes the font difference?
\tcbset{listingsstyle/.style={listing engine=listings}}
\NewTCBListing{cmd}{ O{} }{listingsstyle, #1}
\NewTCBInputListing{\cmdinput}{ m O{} }{listing file={#1}, listingsstyle, #2}

\begin{document}

\begin{cmd}[title={Cmd environment}]sample text\end{cmd}
\cmdinput{test.txt}[title={Cmd input command (full)}]
\cmdinput{test.txt}[title={Cmd input command (partial)},listing options={style=tcblatex, linerange={2-3}}]

\end{document}

enter image description here

1
  • Adding the style to an \lstset command to make it global also works. Jul 22, 2020 at 7:55

You must log in to answer this question.

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