我正在编写一个用于列出代码的文档tcolorbox
。我希望有几个命令可以列出来自外部文件的代码,以及一个(或两个)用于内联代码片段的环境。我所拥有的(并且运行良好)是此 MWE 过程中的一些内容:
\RequirePackage{filecontents}
\begin{filecontents}{test.c}
#include <stdio.h>
int main() {
return 0;
}
\end{filecontents}
\documentclass{article}
\usepackage[listings, breakable, skins, fitting]{tcolorbox}
% from http://tex.stackexchange.com/a/163617/38080
\newtcbinputlisting[auto counter]{\putcodecomment}[2]{
listing file={#1},
title={Listing \thetcbcounter: #1}, fonttitle=\bfseries,
listing and comment,
comment={#2},
%%% start of common things here
breakable,
width=0.9\linewidth,
bicolor,
lower separated=true,
colback=red!5,
colframe=red!75!black,
listing options={
language = {[Visual]C++}, % Language
basicstyle = \ttfamily, % Default format
keywordstyle = \color{blue}, % Keywordformat
breaklines = true, % Automatic line breaks
breakatwhitespace = true, % Line breaks only at white spaces
tabsize = 4, % Number of spaces equal to a tab
showspaces = false, % Show spaces in the code
showtabs = false, % Show tabs in the code
showstringspaces = false, % Show spaces among strings
},
}
\newtcblisting{justcode}[0]{
listing only,
breakable,
width=0.9\linewidth,
%%% start of common things here
breakable,
width=0.9\linewidth,
bicolor,
lower separated=true,
colback=red!5,
colframe=red!75!black,
listing options={
language = {[Visual]C++}, % Language
basicstyle = \ttfamily, % Default format
keywordstyle = \color{blue}, % Keywordformat
breaklines = true, % Automatic line breaks
breakatwhitespace = true, % Line breaks only at white spaces
tabsize = 4, % Number of spaces equal to a tab
showspaces = false, % Show spaces in the code
showtabs = false, % Show tabs in the code
showstringspaces = false, % Show spaces among strings
},
}
\begin{document}
\putcodecomment{test.c}{%
No comment here
}
\begin{justcode}
a = a + 1;
\end{justcode}
\end{document}
... 这样做是可行的,但正如您所见,我必须使用复制和粘贴来“重复”框选项的公共部分。现在,如果我想更改背景颜色,我需要在两个(实际上是四个)不同的地方进行更改。
我尝试将所有常见选项都放入其中,\def\commonoptions{...}
但它却卡住了……
有没有办法将公共部分放在公共“样式”中,以便我更好地组织它?样式方面有什么问题tikz
吗?
答案1
只需使用\lstdefinestyle{foo}{ your options}
并说listing options={style=foo}
,这将节省大量的打字(或工作)。
设置样式后,您可以更改列表选项:
listing options={style=foo,numbers=right}
, 例如。
以下是修改后的 OP 代码
\RequirePackage{filecontents}
\begin{filecontents}{test.c}
#include <stdio.h>
int main() {
return 0;
}
\end{filecontents}
\documentclass{article}
\usepackage[listings, breakable, skins, fitting]{tcolorbox}
\lstdefinestyle{rmano}{ language = {[Visual]C++}, % Language
basicstyle = \ttfamily, % Default format
keywordstyle = \color{blue}, % Keywordformat
breaklines = true, % Automatic line breaks
breakatwhitespace = true, % Line breaks only at white spaces
tabsize = 4, % Number of spaces equal to a tab
showspaces = false, % Show spaces in the code
showtabs = false, % Show tabs in the code
showstringspaces = false, % Show spaces among strings
}
% from http://tex.stackexchange.com/a/163617/38080
\newtcbinputlisting[auto counter]{\putcodecomment}[2]{
listing file={#1},
title={Listing \thetcbcounter: #1}, fonttitle=\bfseries,
listing and comment,
comment={#2},
%%% start of common things here
breakable,
width=0.9\linewidth,
bicolor,
lower separated=true,
colback=red!5,
colframe=red!75!black,
listing options={style=rmano}
}
\newtcblisting{justcode}[0]{
listing only,
breakable,
width=0.9\linewidth,
%%% start of common things here
breakable,
width=0.9\linewidth,
bicolor,
lower separated=true,
colback=red!5,
colframe=red!75!black,
listing options={style={rmano},
},
}
\begin{document}
\putcodecomment{test.c}{%
No comment here
}
\begin{justcode}
a = a + 1;
\end{justcode}
\end{document}
编辑
这是一个具有常用选项的版本tcolorbox
:
使用
\def\commonboxoptions{ breakable,
width=0.9\linewidth,
bicolor,
lower separated=true,
colback=red!5!,
colframe=red!75!black,
}
code={\pgfkeysalsofrom{\commonboxoptions}}
并在选项中说tcolorbox
(listing boxes
这里!)
\RequirePackage{filecontents}
\begin{filecontents}{test.c}
#include <stdio.h>
int main() {
return 0;
}
\end{filecontents}
\documentclass{article}
\usepackage[listings, breakable, skins, fitting]{tcolorbox}
\lstdefinestyle{rmano}{ language = {[Visual]C++}, % Language
basicstyle = \ttfamily, % Default format
keywordstyle = \color{blue}, % Keywordformat
breaklines = true, % Automatic line breaks
breakatwhitespace = true, % Line breaks only at white spaces
tabsize = 4, % Number of spaces equal to a tab
showspaces = false, % Show spaces in the code
showtabs = false, % Show tabs in the code
showstringspaces = false, % Show spaces among strings
}
\def\commonboxoptions{ breakable,
width=0.9\linewidth,
bicolor,
lower separated=true,
colback=red!5!,
colframe=red!75!black,
}
% from http://tex.stackexchange.com/a/163617/38080
\newtcbinputlisting[auto counter]{\putcodecomment}[2]{
listing file={#1},
title={Listing \thetcbcounter: #1}, fonttitle=\bfseries,
listing and comment,
comment={#2},
%%% start of common things here
code={\pgfkeysalsofrom{\commonboxoptions}},
listing options={style=rmano}
}
\newtcblisting{justcode}[0]{
listing only,
breakable,
width=0.9\linewidth,
%%% start of common things here
code={\pgfkeysalsofrom{\commonboxoptions}},
listing options={style={rmano},
},
}
\begin{document}
\putcodecomment{test.c}{%
No comment here
}
\begin{justcode}
a = a + 1;
\end{justcode}
\end{document}