Text Formattinglistings | Change the Listing Title Margins

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

listings | Change the Listing Title Margins

Post by Cham »

I need to fix the margins of my listings title. Currently, it's apparently using the global caption settings from the preamble. I don't want to change these, since it's fine for my figures. However, I need to change the title margins for all of my code listings. I'm using the listings package.

Here's a compilable example of the listing code I'm currently using to define my code listing :

Code: Select all

\documentclass[12pt,letterpaper]{book}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[utf8]{inputenc}
\usepackage{textcomp}
\usepackage{listings}
\usepackage{graphicx}
\usepackage{here}
\usepackage{array}
\usepackage{booktabs}
\usepackage{subfig}
\captionsetup{margin=1cm,format=hang,font=small,format=plain,labelfont={bf,up},textfont={it}}
\captionsetup[subfigure]{margin=0cm,font=small,format=plain,labelfont={bf,up},textfont={up}}

\begin{document}

\lstset{tabsize=4,frame=single,language=mathematica,basicstyle=\scriptsize\ttfamily,keywordstyle=\color{black}}

\begin{lstlisting}[title={Some pretty long code title, just to show its lenght over the box.  I need it to be of the same size as the box itself.  Currently, it is not !}]
Some code here...
\end{lstlisting}

\end{document}

Adding the command caption={margin=0cm} to the listing declaration doesn't change anything.

So how can I define a margin of 0 cm for the listing titles, without changing the figures and sub-figures captions ?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

listings | Change the Listing Title Margins

Post by localghost »

Instead of a title you could set a caption for your listings. Then you would be able to setup these captions by the caption package.

Code: Select all

\captionsetup[lstlisting]{…}
Section 6.7 of the package manual has the details.


Thorsten
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Re: listings | Change the Listing Title Margins

Post by Cham »

Excellent idea !

Thanks a lot ! :)
Post Reply