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?
\lstinline
is to switch to a sort of verbatim mode in order to allow interpreting specially keywords, variable names, delimiters and whatnot.\lstinline
switches to a verbatim-style mode, and I understand that normally that is a good thing. Are you suggesting that even for simplecode
,\lstinlinenoverb{code}
is not achievable (or more likely, not worth the effort)? Is the verbatim-mode inseparable from the syntax highlighting etc?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]
.\texttt{...}
for small pieces of code. Then you do not need the fragile option.\identifier
definition), but it means that if I want the code to appear with the same syntax highlighting as properlistings
output, I have to manually emulate whatlistings
does.