使用listings
'slinerange
或firstline
/时lastline
,tcolorbox
会显示额外的顶部和底部填充,并且字体不同。有没有办法修复这个问题,或者这是一个tcolorbox
需要报告/修复的错误?
梅威瑟:
\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}
输出:
答案1
您尚未指定列表选项的样式。
“这将删除 listings 包的所有选项。这包括 tcblisting 标准样式 tcblatex 和编码预设。如果您想在 tcblisting 之外设置 listings 选项,例如在序言中全局设置,请使用此选项。”
如 tcolorbox 手册第 314 页所述。因此,如果您添加style=tcblatex
它,它将按预期工作。
基于您的代码的 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={style=tcblatex, linerange={2-3}}]
\end{document}