# Problem with lstlistings inside tcolorbox

i have defined my new command:

% sets for lstlisting
\lstset{
basicstyle=\small\ttfamily,
columns=flexible,
breaklines=true,
frame=single,
rulecolor=\color{colorCodeBorder}
}

\newcommand{\mynote}[1]{
\begin{tcolorbox}[colframe=black!50]
{#1}
\end{tcolorbox}
}


and try to use it with lstlisting

\mynote{
always use this command:
\begin{lstlisting}
some code
\end{lstlisting}
}


but get error:

Package Listings Warning: Text dropped after begin of listing on input line ...


How could it be resolved?

EDIT: if i skip my command then it is ok:

\begin{tcolorbox}[colframe=black!20]
Always use this code:
\begin{lstlisting}
some code
\end{lstlisting}
\end{tcolorbox}


but i need to call it by \mynote due to other issues... So am i doing sth wrong with defining my command? Be gratefull for your any help..

You can't conveniently put \verb-like code (such as a listing) inside a macro argument. See here for an explanation. But you can use an environment instead, and the \newtcblisting command from tcolorbox helps a lot to define one:

\documentclass{article}
\usepackage{listings}
\usepackage{tcolorbox}
\tcbuselibrary{listingsutf8}    % or 'listings' if you use XeTeX

\lstset{
basicstyle=\small\ttfamily,
columns=flexible,
breaklines=true,
}

\newtcblisting{mynotesidebyside}[1]{listing only, colframe=black!50,
comment side listing, comment={#1}}

\newtcblisting{mynoteinside}[1]{listing only, colframe=black!50,
comment and listing, comment={#1}}

\newtcblisting{mynoteabove}[1]{listing only, colframe=black!50,
comment above listing, comment={#1}}

\begin{document}

\begin{mynotesidebyside}{Always use this code:}
a = (b < c) ? d : e;
f = &a;
\end{mynotesidebyside}

\begin{mynoteinside}{Always use this code:}
a = (b < c) ? d : e;
f = &a;
\end{mynoteinside}

\begin{mynoteabove}{Always use this code:}
a = (b < c) ? d : e;
f = &a;
\end{mynoteabove}
\end{document}


• Hi, but what i want to have such a panel together with normal text and some code? .. i'll edit my question.. it is not possible? Jun 12, 2019 at 6:15
• Your question didn't include normal text, it didn't even have a minimal working example! Please see the updated answer. Jun 12, 2019 at 6:50