Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am trying to cite a paper in the caption of listings. I have the following code:

\begin{lstlisting}[caption=Example of something (Adapted from \cite{abc})]
   example
\end{lstlisting}

But this is giving me error! It says ! Illegal parameter number in definition of \reserved@a. Can anyone help me with this?

share|improve this question
    
Did you try \protect\cite{abc}? –  egreg Jun 18 '12 at 22:42
    
Yes that worked thanks! –  Sadiksha Gautam Jun 18 '12 at 22:43
    
I've added an answer, but please add a minimal example to your question, so that we can get a better insight into the issue. –  egreg Jun 18 '12 at 22:53

1 Answer 1

up vote 9 down vote accepted

It seems a typical "fragile command in a moving argument problem". However the following document doesn't show any problem:

\documentclass{article}
\usepackage{listings}
\begin{document}
\lstlistoflistings
\begin{lstlisting}[caption=Example of something (Adapted from \cite{abc})]
   example
\end{lstlisting}
\begin{thebibliography}{1}
\bibitem{abc} x
\end{thebibliography}
\end{document}

So I suspect that you're using some package that kill the robustness of \cite in some way.

A way to solve the issue is writing

\protect\cite{abc}

in the caption, but probably it can be better addressed with a minimal example available.

As correctly remarks Gonzalo Medina in his comment, there's also the possibility of writing

\begin{lstlisting}[
  caption={[Example of something]Example of something (Adapted from \cite{abc})}
]

so that only "Example of something" will be sent to the list of listings (which is very important if the adopted bibliography style is "unsorted").

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.