# listings and sidecap - Side captions for listings

I am using the package sidecap for captions which appear left or right of a picture/table, like so:

\documentclass[a4paper,twoside,11pt,openright]{scrbook}
\usepackage{graphicx}
\usepackage[wide]{sidecap}
\usepackage[font=footnotesize,
format=plain,
labelfont={bf,sf},
textfont={it},
width=10pt]{caption}

\newcommand{\fig}[4]{
\begin{SCfigure}
\centering
\includegraphics[width=\textwidth]{#1}
\caption[#2]{#3}
\label{fig:#4}
\end{SCfigure}
}


And the, within the document:

\begin{document}
\fig{background_flickr.png}{Flickr geotagging functionality}{Flickr geotagging functionality. By navigating and panning on a map, the user can place an image or video and specify the desired level of spatial granularity.}{background_flickr}
\end{document}


Which yields:

I am also using the package listings for code, and the settings look like this:

\usepackage{listings}
\lstset{
backgroundcolor=\color{lightgray},
extendedchars=true,
basicstyle=\footnotesize\ttfamily,
xleftmargin=20pt,
showstringspaces=false,
showspaces=false,
numbers=left,
numberstyle=\footnotesize,
tabsize=2,
breaklines=true,
showtabs=false,
captionpos=tb
}


Inserting a code listing like this

\begin{lstlisting}[caption={[JSON Example]A basic JSON example}, label=src:DataTwitterAPIJSON]
{
"firstName": "John",
"lastName": "Smith",
"age": 25,
"city": "New York",
"state": "NY",
"postalCode": 10021
},
"phoneNumbers": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "fax",
"number": "646 555-4567"
}
]
}
\end{lstlisting}


yields:

There are two problems: First of all, the caption does not get displayed correctly at all (seems to be very narrow). Secondly, I need it to be on the left of the listing on left pages and on the right of the listing on right pages (like the normal sidecap behavior above). I suspect that the "narrow" look somehow comes from the sidecap package. Is there no way to not only use sidecap for figures and tables but also for listings?

-
I revised my solution to use a new listing float environment –  Steven B. Segletes Jul 23 '13 at 15:20

A couple things seem to being going on here.

The first is that you're not taking advantage of the sidecap package for your listings environment. The second is that you're telling the listings environment that the caption should go on the top and bottom---so you're probably getting some kind of conflict.

The solution here is not optimal: it will not allow your listings environment to straddle page boundaries. But that doesn't seem too bad considering you want to treat it like a figure.

What I've done here is place the listings inside a minipage inside a SCfigure environment. The minipage is set to the width of the text for the current page.

\documentclass[a4paper,twoside,11pt,openright]{scrbook}
\usepackage{xcolor}
\usepackage{graphicx}

%% setting up new float
\usepackage{float}
\floatname{mylisting}{Listing}
\newfloat{mylisting}{tbhp}{lst}[chapter]
\newcommand{\listoflistings}{\listof{mylisting}{List of Listings}}

%% setting up side captions
\usepackage[wide]{sidecap}
\makeatletter
\@ifdefinable\SC@listings@vpos{\def\SC@listings@vpos{b}}
\newenvironment{SClisting}{\SC@float[\SC@listings@vpos]{mylisting}}{\endSC@float}
\newenvironment{SClisting*}{\SC@dblfloat[\SC@listings@vpos]{mylisting}}{\endSC@dblfloat}
%% sidecap and float access the name of the new float differently.
%% You can't rely on float to pass sidecap the correct name.  So,
%% here we manually feed the correct name for the new float in a manner
%% pleasing to sidecap.
\@namedef{mylistingname}{Listing}
\makeatother

%% setting up formatting for cpations
\usepackage
[
font=footnotesize,
format=plain,
labelfont={bf,sf},
textfont={it},
width=10pt
]{caption}

%% creating figure command
\newcommand{\fig}[4]{%
\begin{SCfigure}
\centering
\includegraphics[width=\textwidth]{#1}
\caption[#2]{#3}
\label{fig:#4}
\end{SCfigure}%
}

%% setting up listings
\usepackage{listings}
\lstset
{
backgroundcolor=\color{gray!20},
extendedchars=true,
basicstyle=\footnotesize\ttfamily,
xleftmargin=20pt,
showstringspaces=false,
showspaces=false,
numbers=left,
numberstyle=\footnotesize,
tabsize=2,
breaklines=true,
showtabs=false,
}

\begin{document}
%% Uncomment next line to get a list of listings floats
%% \listoflistings
%%
%% The following was here just for testing how float and sidecap
%% handle labeling the caption.  You don't need these lines.
%% \begin{mylisting}
%%   \centering
%%     Hello
%%   \caption{Hello}
%% \end{mylisting}

\fig{example-image-a}
{Flickr geotagging functionality}
{Flickr geotagging functionality. By navigating and panning on a map,
the user can place an image or video and specify the desired level of
spatial granularity.}
{background_flickr}

\begin{SClisting}
\begin{minipage}[t]{\textwidth}
\begin{lstlisting}
{
"firstName": "John",
"lastName": "Smith",
"age": 25,
"city": "New York",
"state": "NY",
"postalCode": 10021
},
"phoneNumbers": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "fax",
"number": "646 555-4567"
}
]
}
\end{lstlisting}
\end{minipage}
\caption[JSON Example]{A basic JSON example}
\end{SClisting}

\end{document}


Another idea I had was to put the listings into a standalone file and import the results of compiling that. But, the results did not look quite as good.

-
Interesting approach, but there are two problems, just as in @Steven B. Segletes idea: The caption should be Listing 0.1 instead of Figure 0.2 - might this somehow be possible? This is also related to the problem that the listing itself should appear in the List of Listings and not in the List of Figures. The second problem is that code actually should be able to span several pages, but that is not so important since such long listings should go to the Appendix anyway (and there I don't need to use side captions). So do you have any idea about the caption problem? –  wnstnsmth Jul 23 '13 at 15:17
+1 because you make use of the existing two packages (sidecap and listings) –  wnstnsmth Jul 23 '13 at 15:18
Bad me. I failed to notice the "Figure" vs "Listing" matter. That should be easily repaired. I'll get on that. –  A.Ellett Jul 23 '13 at 15:27
Very well, thank you so far. The use case of "short", in-text listings is now implemented. But what I have these other, "regular" listings which span several pages in the Appendix? These don't need sidecaps, captions can be on top or on bottom, but they should appear in the List of Listings. To me it seems that this conflicts and is probably not possible? My knowledge of LaTeX macros is really limited, indeed. –  wnstnsmth Jul 24 '13 at 7:52
Well anyway, I resorted back to not using sidecap for listings, because of the issues with the list of listings and stuff in the appendix - I'll still accept the answer because it proposes an elegant solution to my very problem. –  wnstnsmth Jul 29 '13 at 8:37

EDITED to provide a separate listing environment (float).

Here is an answer (similar to output of A.Ellett's, but using the verbatimbox package, instead of listings. It also puts it into a sidecap figure box, so that it will not span page boundaries.

\documentclass[a4paper,twoside,11pt,openright]{scrbook}
\usepackage[demo]{graphicx}
\usepackage[wide]{sidecap}
\usepackage[font=footnotesize,
format=plain,
labelfont={bf,sf},
textfont={it},
width=10pt]{caption}
\usepackage{verbatimbox}
\usepackage{xcolor}
\newcommand{\fig}[4]{
\begin{SCfigure}
\centering
\includegraphics[width=\textwidth]{#1}
\caption[#2]{#3}
\label{fig:#4}
\end{SCfigure}
}
\usepackage{newfloat}
\DeclareFloatingEnvironment[placement={!ht},name=Listing]{listing}
\captionsetup[listing]{labelfont=bf}
\makeatletter
\makeatother

\begin{document}
\fig{background_flickr.png}{Flickr geotagging functionality}{Flickr
geotagging functionality. By navigating and panning on a map, the user
can place an image or video and specify the desired level of spatial
granularity.}{background_flickr}

\def\optarg{\colorbox{white}{%
\makebox[.3in][r]{\arabic{VerbboxLineNo}:\hspace{1ex}}}%
}
\begin{verbbox}[\optarg]
{
"firstName": "John",
"lastName": "Smith",
"age": 25,
"city": "New York",
"state": "NY",
"postalCode": 10021
},
"phoneNumbers": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "fax",
"number": "646 555-4567"
}
]
}
\end{verbbox}

\begin{SClisting}
\fboxsep=0pt\relax\colorbox{lightgray}{\theverbbox}%
\caption[JSON Example]{A basic JSON example}
\end{SClisting}

\end{document}


-
I will look into this tomorrow, thanks. –  wnstnsmth Jul 23 '13 at 15:22