1

I am writing a text which includes lots of programming snippets which I've put into environments which use tcolorbox/tcblisting:

\usepackage[pdftex]{xcolor}
\definecolor{vlgray}{gray}{0.9}
\definecolor{lgray}{gray}{0.7}

\usepackage{tcolorbox,fancyvrb}
\tcbuselibrary{skins,breakable,listings,breakable}

\newenvironment{shk}{%
  \tcblisting{listing only,colback=vlgray,colframe=vlgray,enlarge
  top by=0mm,top=-2mm,bottom=2mm,enhanced,
  after={\par\vspace{0.5\baselineskip}\noindent},
  overlay={\node[draw,fill= black,yshift=4pt,xshift=-10pt,left,text=white,
         anchor=east,font=\footnotesize\bfseries] at (frame.south east)
         {Shakespeare};}, 
  listing options={basicstyle=\small\ttfamily,breaklines=true,
    language=HTML},}}
{\endtcblisting}

This means that in my document:

\begin{shk}
Now is the winter of our discontent
Made glorious summer by this sun of York;
\end{shk}

looks like this:

enter image description here

which is exactly what I want.

However! Suppose I want to box one word, say "winter", in my listing. How do I do that? I can't merely write \fbox{winter} as that will simply be listed, rather than escaped to LaTeX. The mathescape option of the listings package doesn't seem to work in tcblisting.

I know I could use tikzpicture with [remember picture,overlay] to position a box on the page, but that seems a little inelegant... is there a way of boxing text within tcblisting itself?

1

1 Answer 1

4

I don't know about mathescape, but presumably you don't want/need to enter math mode anyway, since you just want to make a box.

So use escapechar=<char> instead. I've turned your code snippets into an MWE to more clearly show the solution, but the same escapechar=| (or some other character not used anywhere in the listing) is equally valid inside listing options in your environment's definition.

\documentclass{article}

\usepackage{tcolorbox}
\tcbuselibrary{listings}

\begin{document}
\begin{tcblisting}{listing only,listing options={basicstyle=\ttfamily,escapechar=|}}
Now is the |\fbox{winter}| of our discontent
Made glorious summer by this sun of York;
\end{tcblisting}
\end{document}

enter image description here

Here's the full code made into a compilable example:

\documentclass[11pt]{article}

\usepackage[pdftex]{xcolor}
\definecolor{vlgray}{gray}{0.9}
\definecolor{lgray}{gray}{0.7}

\usepackage{tcolorbox}
\tcbuselibrary{skins,breakable,listings}

\newenvironment{shk}{%
  \tcblisting{listing only,colback=vlgray,colframe=vlgray,enlarge
  top by=0mm,top=-2mm,bottom=2mm,enhanced,
  after={\par\vspace{0.5\baselineskip}\noindent},
  overlay={\node[draw,fill= black,yshift=4pt,xshift=-10pt,left,text=white,
         anchor=east,font=\footnotesize\bfseries] at (frame.south east)
         {Shakespeare};}, 
  listing options={basicstyle=\small\ttfamily,breaklines=true,
    language=HTML,escapechar=|},}}
{\endtcblisting}


\begin{document}
\begin{shk}
Now is the |\fbox{winter}| of our discontent
Made glorious summer by this sun of York;
\end{shk}
\end{document}

enter image description here

3
  • Many thanks indeed! That's exactly what I was looking for. I was hunting through the tcolorbox manual for information on escaping, and couldn't find anything - it must be in the documentation of the listings package instead.
    – Alasdair
    Mar 21, 2015 at 10:39
  • Glad it helped you out, @Alasdair. You're right about needing to track down the listings documentation for all the available listing options settings. If I can answer anything else here, please let me know. Otherwise, please consider accepting my answer if you're happy with it. Cheers! :-) Mar 21, 2015 at 13:22
  • Thanks again Paul, yes I've accepted your answer, and indeed implemented it in my LaTeX file. And it works a treat!
    – Alasdair
    Mar 23, 2015 at 0:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .