4

How can I emphasize a string in a tcblisting like it is possible with the "moredelim" option in a lstlisting environment? The goal ist to be able to situational epmhasize arbitrary strings in a tcblisting.

With the listings package I can do:

\documentclass[]{scrbook}
\usepackage{xcolor}
\usepackage{listings}
\lstdefinestyle{somebox}{
    frame=single,
    moredelim=**[is][\color{green}]{@}{@},
}    

\begin{document}
\begin{lstlisting}[style=somebox]
    black @green@ black
\end{lstlisting}
\end{document}

enter image description here

Now, I want to use the listings library along with tcolorbox like this:

\documentclass[]{scrbook}
\usepackage[most]{tcolorbox}
\newtcblisting{somebox}{
    listing only,
    listing options={style=tcblatex}
}

\begin{document}
\begin{somebox}
black green black
\end{somebox}
\end{document}

enter image description here

But tcolorbox does not know "moredelim":

\documentclass[]{scrbook}
\usepackage[most]{tcolorbox}
\newtcblisting{somebox}{
    listing only,
    listing options={style=tcblatex},
    moredelim=**[is][\color{green}]{@}{@}
}

\begin{document}
\begin{somebox}
black @green@ black
\end{somebox}
\end{document}

Error is:

Package pgfkeys Error: I do not know the key '/tcb/moredelim', to which you pmisspelled it. \begin{somebox}

  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – karlkoeller Dec 29 '14 at 12:19
5

You must pass listings options inside the argument of the listings options key. If you want to change the listings options locally you could pass it through an optional argument (put braces around the argument of moredelim to protect the []):

\documentclass[]{scrbook}
\usepackage[most]{tcolorbox}
\newtcblisting{somebox}[1][]{
    listing only,
    listing options={style=tcblatex,moredelim={**[is][\color{green}]{@}{@}},#1},
}

\begin{document}
\begin{somebox}
black @green@ black
\end{somebox}


\begin{somebox}[moredelim={**[is][\color{red}]{@}{@}}]
black @green@ black
\end{somebox}

\end{document}

enter image description here

| improve this answer | |
5

You can define a new listing language

\lstdefinelanguage{mylang}
{
    moredelim=**[is][\color{green}]{@}{@}
}

and use it inside the listing options of your \newtcblisting

\newtcblisting{somebox}{
    listing only,
    listing options={style=tcblatex,language=mylang}
}

MWE

\documentclass[]{scrbook}
\usepackage[most]{tcolorbox}

\lstdefinelanguage{mylang}
{
    moredelim=**[is][\color{green}]{@}{@}
}

\newtcblisting{somebox}{
    listing only,
    listing options={style=tcblatex,language=mylang}
}

\begin{document}
\begin{somebox}
black @green@ black
\end{somebox}
\end{document} 

Output

enter image description here

| improve this answer | |
  • That works perfect and answers my question. What I have not asked for but I want to add here: If you do not want to define a completely new language but just want to extend an existing one, you can write e. g. \lstdefinelanguage{mylang}[]{python}. – Alexander Dec 29 '14 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.