Graphics, Figures & TablesSource Code Listings and Captions

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
ronnyma
Posts: 1
Joined: Wed Mar 09, 2011 8:00 pm

Source Code Listings and Captions

Post by ronnyma »

I am including source code in my document, however the caption and listing are sometimes separated, i.e. the caption is being displayed at the bottom of a page and the listing continues on the next page. I would like to have both the caption and the listing as one single entity. This means that the caption should never be displayed without being on top of the listing. It is ok if the listing is split because of a page break.

Please note that the produced listing will behave like I described when it is pushed towards the bottom. I did not include "fill-text" above it because it will mess up my MWE.

Code: Select all

\documentclass[a4paper,11pt]{report}

\usepackage{listings}
\usepackage{caption}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{gray}{\parbox{\textwidth}{#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white}
\usepackage{graphicx}
\usepackage{verbatim} % used to display code
\usepackage{hyperref}
\usepackage{fullpage}
\usepackage[ansinew]{inputenc} % german umlauts
\usepackage[usenames,dvipsnames]{color}
\usepackage{float}
\usepackage{subfig}
\usepackage{tikz}
\usetikzlibrary{calc,through,backgrounds}
\usepackage{fancyvrb}
\usepackage{acronym}
\usepackage{amsthm} % Uuhhh yet another package
\VerbatimFootnotes % Required, otherwise verbatim does not work in footnotes!



\definecolor{OliveGreen}{cmyk}{0.64,0,0.95,0.40}
\definecolor{CadetBlue}{cmyk}{0.62,0.57,0.23,0}
\definecolor{lightlightgray}{gray}{0.9}



\lstset{
language=Java,                          % Code langugage
basicstyle=\ttfamily,                   % Code font, Examples: \footnotesize, \ttfamily
keywordstyle=\color{OliveGreen},        % Keywords font ('*' = uppercase)
commentstyle=\color{gray},              % Comments font
numbers=left,                           % Line nums position
numberstyle=\tiny,                      % Line-numbers fonts
stepnumber=1,                           % Step between two line-numbers
numbersep=5pt,                          % How far are line-numbers from code
backgroundcolor=\color{lightlightgray}, % Choose background color
frame=none,                             % A frame around the code
tabsize=2,                              % Default tab size
captionpos=t,                           % Caption-position = bottom
breaklines=true,                        % Automatic line breaking?
breakatwhitespace=false,                % Automatic breaks only at whitespace?
showspaces=false,                       % Dont make spaces visible
showtabs=false,                         % Dont make tabls visible
columns=flexible,                       % Column format
morekeywords={__global__, __device__},  % CUDA specific keywords
}

%Preamble for source code formatting and listing end


\begin{document}

\begin{lstlisting}[label=some-code,caption=Some Code]
ClassFile getClassFile( String name, String suffix ) throws IOException {
    final File file = new File(dir + File.separatorChar
    final File file = new File(dir + -File.separatorChar//DELTA
                 + name.replace('.', File.separatorChar) + suffix);
                return file.exists() ? new ClassFile() {
                public InputStream getInputStream() throws IOException {
                return new FileInputStream(file);
             }

\end{lstlisting}
\end{document}


Thanks.

/Ronny Mandal
Last edited by ronnyma on Thu Mar 10, 2011 11:30 am, edited 1 time in total.

Recommended reading 2024:

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

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

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

Source Code Listings and Captions

Post by localghost »

Please insert relevant code directly in your post by using the »Code« environment of the forum (from the button list right above the input window) and abstain from external services in the future. External links can get lost with time and later make a problem incomprehensible for others.

On this occasion please complete your code to a minimal working example (MWE) that shows the undesired behaviour clearly. If you consider it necessary, upload the resulting output as attachment to the forum server and not to an external service. So, please edit your post and modify it in this regard.


Best regards and welcome to the board
Thorsten
Post Reply