# Is there a non-verbatim form of lstinline from the listings package?

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,
stringstyle=\color{brown}\ttfamily,
columns=fullflexible,
}

\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?

-
The job of \lstinline is to switch to a sort of verbatim mode in order to allow interpreting specially keywords, variable names, delimiters and whatnot. –  egreg Oct 3 '12 at 9:26
@egreg I understand why \lstinline switches to a verbatim-style mode, and I understand that normally that is a good thing. Are you suggesting that even for simple code, \lstinlinenoverb{code} is not achievable (or more likely, not worth the effort)? Is the verbatim-mode inseparable from the syntax highlighting etc? –  cyberSingularity Oct 3 '12 at 9:32
For single identifiers like cout your approach of defining a special macro is the way to go, IMO. There's no way of colorizing arbitrary constructs without switching to a special mode, which thus requires [fragile]. –  egreg Oct 3 '12 at 9:34
use \texttt{...} for small pieces of code. Then you do not need the fragile option. –  Herbert Oct 3 '12 at 12:07
@Herbert. Thanks, I had already considered that (similar to my \identifier definition), but it means that if I want the code to appear with the same syntax highlighting as proper listings output, I have to manually emulate what listings does. –  cyberSingularity Oct 3 '12 at 12:34

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,
stringstyle=\color{brown}\ttfamily,
columns=fullflexible,
}

\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}

-
Braces in the argument of \identifier will not appear. –  egreg Nov 4 '12 at 0:23
Thanks. I ended up using either this (but was for some reason not entirely satisfied with it, forgot why though, so am more accepting of it now!) or using fragile=singleslide (which I think prevents me using overlays, but I don't find the beamer manual very clear about fragile restrictions). @egreg's comments about having to ensure everything inside the argument to \identifier has the right catcode already is a useful reminder too, though I think having braces in the argument is more likely to produce odd errors than just disappearing characters. –  cyberSingularity Nov 4 '12 at 0:55
@egreg Obviously, \identifier is not a true verbatim command. But here, if I understand the question, the goal is to get a non verbatim command that produces the same colored and formated output that the \lstinline verbatim command (for simple code). –  Paul Gaborit Nov 4 '12 at 1:28

The workings of listings require changing the category code of characters and so the [fragile] option after \begin{frame} is necessary.

It all depends on what you really need to type: a small number of identifiers can be input as you do with \identifier. You can make other definitions for colorizing common constructs, so as to limit the need of [fragile].

-