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 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
share|improve this question
add comment

1 Answer

up vote 5 down vote accepted
  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}
share|improve this answer
add comment

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.