# It is possible to define “styles” in tcolorbox listings? [duplicate]

I am writing a document where I use tcolorbox to list code. I would like to have a couple of commands to list code from an external file, and one (or two) environment for inline snippets. What I have (and it's nicely working) is something along the way of this 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}


... which works, but as you can see, I had to use copy and paste to "repeat" the common part of the options of the boxes. Now, if I want to change the color of the background, I need to change it in two (really they are four) different place.

I tried to put all the common option in a \def\commonoptions{...} but it choked...

Is there a way to put the common part in common "style" so that I can organize it better? Something on the line of the tikz styles?

• Do you mean a listing style or a tcolorbox style? – user31729 Nov 27 '15 at 18:37
• ...really the two of them. – Rmano Nov 27 '15 at 18:52
• Then try the second version of my answer, at the bottom – user31729 Nov 27 '15 at 18:52

Just use \lstdefinestyle{foo}{ your options} and say listing options={style=foo}, this will save a lot of typing (or work).

You can change the listing options after setting the style:

listing options={style=foo,numbers=right}, for example.

Here's a modified version of the O.P. code

\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}


Edit

Here's a version that has common options for tcolorbox as well:

Use

\def\commonboxoptions{    breakable,
width=0.9\linewidth,
bicolor,
lower separated=true,
colback=red!5!,
colframe=red!75!black,
}


and say code={\pgfkeysalsofrom{\commonboxoptions}} in the tcolorbox options (the listing boxes here!)

\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}

• Yes, it works! I can also put the listing options={style=rmano} into the macro \commonboxoptions and it works! – Rmano Nov 27 '15 at 18:59
• @Rmano: Yes, I considered that too, but I thought you would specifiy slightly different options to the different boxes, that's why I kept it outside! – user31729 Nov 27 '15 at 19:01