7

How to add square brackets as a caption in a listing? I tried putting caption inside of "{}", but then the output is quiet weird.

\documentclass{article}

\usepackage{listings}

\begin{document}
    \begin{lstlisting}[caption={Algorithm [1] - example}]
    example
    \end{lstlisting}
\end{document}
7
\begin{lstlisting}[caption={Algorithm \unexpanded{[1]} - example}]

works on me. or as Manuel said:

\begin{lstlisting}[caption={Algorithm {[1]} - example}]
  • 3
    Just {[1]} is enough. – Manuel May 29 '16 at 10:45
  • @Manuel I misunderstood, what he said. I thought, he meant, that this doesnt work. – MaestroGlanz May 29 '16 at 10:46
  • 1
    @Ivan You might have seen, that Latex used the [1] somehow. To prevent Latex from using stuff \noexpand or \unexpanded can help. If you have no output, it is one expansion to less or at the wrong point. But this happens only in rare cases. Apparently, in this case { } is enough. – MaestroGlanz May 29 '16 at 11:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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