[Tex/LaTex] Caption for a listing

captionsformattinglistings

I have captions on each of my listings. Each caption is prefaced with "Listing # [text]" where # is the number of the listing. Each caption is centered over my listing. How can I modify the options of these captions? Here is a visual:enter image description here

  1. I want to get truncate the caption to only contain [text]
  2. I want to move the position of the caption from the center towards the left-side

Best Answer

  1. If a listing has a title, it will be used instead of caption, but without "Listing #" line.

  2. Unfortunately centered titles are hardcoded into listings, so you need to change the code. Here is a solution: I've just took the package defined \@makecaption and deleted the secong \hfil and added \bfseries (note that \lst@maketitle uses \@makecaption and drops the first part. If you want to keept captions centered but titles left-aligned, you need to patch \lst@maketitle rather than \@maketitle).

Here us the result:

\documentclass{article}
\usepackage{listings}
\makeatletter
\long\def\@makecaption#1#2{%
  \vskip\abovecaptionskip
    \bfseries #1: #2\par
  \vskip\belowcaptionskip}%
\makeatother

\begin{document}
\begin{lstlisting}[title={Some listing}]
a=b
\end{lstlisting}
\end{document}