2

I'm typesetting some examples of a system which has this sort of look:

In:  2+2
Out: 4

and I'd like to emphasis the "In:" and "Out": This works:

\begin{lstlisting}[basicstyle=\ttfamily,emph={In,Out},emphstyle=\bfseries]
  In:  2+2
  Out: 4
\end{lstlisting}

but this:

\begin{lstlisting}[basicstyle=\ttfamily,emph={In:,Out:},emphstyle=\bfseries]
  In:  2+2
  Out: 4
\end{lstlisting}

where I want to include the colons in the emphasis, doesn't. I've experimented with including alsoletter={:} in the options, to no effect.

Is there a simple way to achieve this?

Note, I am in fact using listings as part of a new environment defined with the tcolorbox package. So:

\newenvironment{pyth}{%
   \tcblisting{listing only,colback=vlgray,colframe=vlgray,enhanced,
    overlay={\node[draw,fill=black,xshift=-10pt,yshift=4pt,left,text=white,
         anchor=east,font=\footnotesize\bfseries] 
         at (frame.south east) {Python};},%
    listing options={alsoletter={:},emph={In:,Out:},emphstyle=\bfseries,
    basicstyle=\small\ttfamily,breaklines=true,language=Python},}}
{\endtcblisting}

and then in my text:

\begin{pyth}
In:  2+2
Out: 4
\end{pyth}

In this particular environment alsoletter appears to have no effect.

4

alsoletter={:} does work:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{listings}
\usepackage{beramono}

\begin{document}

\begin{lstlisting}[basicstyle=\ttfamily,alsoletter={:},emph={In:,Out:},emphstyle=\bfseries]
  In:  2+2
  Out: 4
\end{lstlisting}

\end{document}

enter image description here

Update after the question edit:

Specify the language first:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{beramono}
\usepackage[many]{tcolorbox}
\tcbuselibrary{listings}

\colorlet{vlgray}{gray}% just for the example, since the original definition was not included

\newenvironment{pyth}
  {%
    \tcblisting{
      listing only,
      colback=vlgray,
      colframe=vlgray,
      enhanced,
      overlay={
        \node[
          draw,
          fill=black,
          xshift=-10pt,
          yshift=4pt,
          left,
          text=white,
          anchor=east,
          font=\footnotesize\bfseries
          ] 
           at (frame.south east) {Python};
       },%
      listing options={
        language=Python,
        alsoletter={:},
        emph={In:,Out:},
        emphstyle=\bfseries,
        basicstyle=\small\ttfamily,
        breaklines=true,
      },
    }
  }
  {\endtcblisting}

\begin{document}

\begin{pyth}
In:  2+2
Out: 4
\end{pyth}

\end{document}

enter image description here

I used the beramono package in my example just to have a mono-spaced boldfaced font. I used \colorlet{vlgray}{gray} for the example, since the original definition was not included in the question.

  • Thanks, I've added more info to my original post which may explain a bit better why I'm not getting the output I want. – Alasdair May 4 '14 at 4:43
  • @Alasdair please see my updated answer showing a possible solution using your settings. – Gonzalo Medina May 4 '14 at 5:03
  • Thank you! Putting the language first works perfectly. Thank you again for your time and trouble! – Alasdair May 4 '14 at 5:20
  • @Alasdair Be aware that making : a "letter" has consequences. For instance, if you've defined for to be a keyword, then listings won't highlight it in for:. – jub0bs May 4 '14 at 10:44

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.