8

I'm including a fair amount of inline C++ code in a beamer presentation I'm making, and I want to be able to use the short syntax for \lstinline. My code displays fine if I use, for example, the ! character as my delimiter, like

\lstMakeShortInline!
!cout << "This C++ displays fine";!

But when I try to use the @ character as my delimiter, because it will hardly ever appear in C++ code, the line is displayed unformatted, with literal @s surrounding it. As far as I know, @ is not a special character in LaTeX, so why is this happening?

Edit: I did not test the example I gave, and on further inspection, the only character that I can get to work as a delimiter is ~, as demonstrated with this example:

\documentclass{beamer}
\usepackage{listings}
\lstMakeShortInline[language=C++,basicstyle=\ttfamily]~
\begin{document}
\begin{frame}
~cout << "This C++ displays fine";~
\end{frame}
\end{document}

However, other delimiters (such as |) seem to work in the article documentclass, is this an issue with beamer?

6
  • 2
    Could you please make a complete example from \documentclass to \end{document}?
    – egreg
    Sep 27, 2013 at 21:24
  • @percusse what does it do? All I could find is that using it in a macro name usually indicates that it's internal. Sep 27, 2013 at 21:50
  • @ is a big deal in TeX. Using @ creates a name clash with the existing internal macro \lstMakeShortInline@ and I'm not sure whether it is worth of hacking it such that it works. (defined in listings.sty line 1932
    – percusse
    Sep 27, 2013 at 21:55
  • Ah, I didn't realize that there was an internal macro sharing that name. But that still doesn't explain why all the other delimiters are broken. Sep 27, 2013 at 22:03
  • It works nicely with a standard class such as article. It seems beamer is doing some catcode stuff. If you make @ active (\catcode`\@=\active) before \lstMakeShortInline and between \begin{document} and \begin{frame} things work as the should
    – cgnieder
    Sep 27, 2013 at 22:22

1 Answer 1

7

If you try

\documentclass{beamer}
\usepackage{listings}
\lstMakeShortInline[language=C++,basicstyle=\ttfamily]@
\lstMakeShortInline[language=C++,basicstyle=\ttfamily]|
\begin{document}

\begin{frame}
\show @
\show |
\end{frame}

\end{document}

the compilation stops with

> the character @.

and then with

> |=macro:
->\lstinline [language=C++,basicstyle=\ttfamily ]|.

showing the fundamental difference between the two cases: @ is normalized to have category code 12. This happens at begin document, so you can postpone your declaration:

\documentclass{beamer}
\usepackage{listings}
\lstMakeShortInline[language=C++,basicstyle=\ttfamily]|
\begin{document}
\lstMakeShortInline[language=C++,basicstyle=\ttfamily]@

\begin{frame}[fragile]
|cout << "This C++ displays fine";|

@cout << "This C++ displays fine";@
\end{frame}
\end{document}

will display both lines in the same way. Remember to declare fragile all frames where verbatim material is typeset.

enter image description here

You must log in to answer this question.

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