[Tex/LaTex] non-verbatim form of lstinline from the listings package

beamerlistingsverbatim

I'm using the listings package with beamer and I'd like to try to avoid making frames fragile where possible. I know that many of my frames will need to be fragile but there are some where the contents of my lstinline code need not switch to a verbatim processing style. Let me illustrate this with an example:

\documentclass{beamer}
\usepackage{listings}
\lstset{language=C++}
\lstset{frame=,
  framesep=5pt,
  basicstyle=\footnotesize\ttfamily,
  keywordstyle=[1]\ttfamily\color{blue}\bfseries,
  identifierstyle=\ttfamily\color{purple}\bfseries,
  commentstyle=\normalfont\color{green},
  stringstyle=\color{brown}\ttfamily,
  columns=fullflexible,
  fontadjust=true,
}

\newcommand*{\identifier}[1]{{\footnotesize\ttfamily\color{purple}\bfseries #1}}

\begin{document}
    \begin{frame}[fragile]{1. Hello world}
        \begin{lstlisting}
        int main(int argc, char *argv[]) {
            std::cout << "Hello world!" << std::endl;
            return EXIT_SUCCESS;
        }
        \end{lstlisting}
    \end{frame}

    \begin{frame}[fragile]{2. Discussion of Hello world}
        Note that \lstinline!cout! in the previous example means ``console out''.
    \end{frame}

    \begin{frame}{3. Discussion of Hello world}
        Note that \identifier{cout} in the previous example means ``console out''.
    \end{frame}
\end{document}

There are three frames here, frame 1 is necessarily fragile. Frame 2 has to be made fragile otherwise there is an error, even though there is no content in that frame that needs to be processed verbatim. Frame 3 shows the same output as frame 2 without using fragile. However, this is very limited as it means controlling everything myself rather than getting listings to do it for me. This would be much harder when I start wanting to talk about expressions that contain both keywords and identifiers, for example.

Question Hence, I am wondering if there is a version of the lstinline command, that does not switch to a verbatim-style processing mode, and therefore frame 2 would not require use of fragile. If not, would this be extremely difficult to provide?

Best Answer

You can call \lstinline directly in your \identifier command:

\documentclass{beamer}
\usepackage{listings}
\lstset{language=C++}
\lstset{frame=,
  framesep=5pt,
  basicstyle=\footnotesize\ttfamily,
  keywordstyle=[1]\ttfamily\color{blue}\bfseries,
  identifierstyle=\ttfamily\color{purple}\bfseries,
  commentstyle=\normalfont\color{green},
  stringstyle=\color{brown}\ttfamily,
  columns=fullflexible,
  fontadjust=true,
}

\newcommand*{\identifier}[1]{\lstinline!#1!}

\begin{document}

\begin{frame}[fragile]{1. Hello world}
  \begin{lstlisting}
    int main(int argc, char *argv[]) {
      std::cout << "Hello world!" << std::endl;
      return EXIT_SUCCESS;
    }
  \end{lstlisting}
\end{frame}

\begin{frame}[fragile]{2. Discussion of Hello world}
  Note that \lstinline!cout << "Hello"! in the previous example means
  ``console out''.
\end{frame}

\begin{frame}{3. Discussion of Hello world}
  Note that \identifier{cout << "Hello"} in the previous example means
  ``console out''.
\end{frame}
\end{document}
Related Question