I'm trying to, first and foremost, center a lstlisting just as the image is on this example (the working example source is included at end of this post):
and as I cannot find an example of that being done, I'm having trouble with the approach I'm trying in the example.
The next thing that I'd want to do, and where I anticipate problems, is:
- To put both the whole code listing and the caption inside a thin frame box (as visible on image, by default lstlisting frames only the code, not the caption)
- To put two such framed code boxes, side by side (possibly by using subfloat or minipage) - with their own subcaptions (and then possibly a main caption)

Many thanks for any replies,
Cheers!
Edit: Note that the following code:
Code: Select all
\begin{center}
%
%\fbox{ % \frame{ fail here..
\begin{minipage}{2.1764in}
\begin{lstlisting}
...
... // do whatever
...
\end{lstlisting}
\end{minipage}
%}
%
\begin{minipage}{2.1764in}
\begin{lstlisting}
...
... // do something
...
\end{lstlisting}
\end{minipage}
%
\end{center}
\bigskip
The minimal example file:
Code: Select all
% cropsizetest.tex
\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc} %support unicode chars direct without crashing :)
\usepackage{graphicx}
\usepackage{hyperref}
\usepackage[font=footnotesize,labelfont=bf]{caption} %many ways to customise the captions - text justification
\usepackage{subcaption} % so we can modify subcaptions - understands subfloat command
\usepackage{listings} % verbatim doesn't break lines - listings does
% we need a lstset for listings too - else fonts may not be typewriter
\lstset{breaklines=true,basicstyle=\footnotesize\ttfamily,commentstyle=\scriptsize\sffamily\raggedright}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\footnotesize
A figure (\ref{fig:XX}) can be centered easily:
\newline
\begin{figure}[!ht!b]
\centering
\includegraphics[width=4in]{imgtest.png}
\caption{A generic image.}
\label{fig:XX}
\end{figure}
However, listings (\ref{code:helloworld}) may have a problem with that; cannot put \verb=\centering= inside, it will simply be interpreted verbatim:
%\begin{minipage} % "! Missing \endcsname inserted." crash
%\begin{center} % no changes
\begin{lstlisting}[float=h,language=C,caption={A "Hello World" program.},label={code:helloworld},captionpos=b,abovecaptionskip=0em,belowcaptionskip=-2em,columns=flexible,frame=single,linewidth=0.6\linewidth]
\centering % just for fun
// a program
#include <stdio.h>
int main(void)
{
printf("hello, world\n");
return 0;
}
\end{lstlisting}
%\end{center} % the {center} will add an extra line below the caption though
%\end{minipage}
\ \newline% use when center is commented.
\begin{enumerate}
\item {Pretty much, cannot include any Latex command within \verb=\begin,\end{lstlisting}=, as it will be interpreted verbatim.}
\item {Wrapping lstlisting inside \verb=\begin,\end{center}=, results with no changes - the listings box is not centered}
\item {Wrapping lstlisting inside \verb=\begin,\end{minipage}=, results with \\ \verb=! Missing \endcsname inserted.= error/crash.}
\end{enumerate}
\ \newline
Finally, I'd like to use two of those code boxes, such that the caption is framed inside each box - and display them side by side. I've tried with the (commented) code below using \verb=\begin{figure}= and \verb=\subfloat*{=, but it crashes with \verb=! LaTeX Error: Not in outer par mode=.
%\begin{figure}
% \subfloat*{%
% \begin{lstlisting}[float=h,language=C,caption={A "Hello World" program 2.},label={code:helloworld2},captionpos=b,abovecaptionskip=0em,belowcaptionskip=-2em,columns=flexible,frame=single,linewidth=0.4\linewidth]
%// a program
%#include <stdio.h>
%
%int main(void)
%{
% printf("hello, world 2\n");
% return 0;
%}
%\end{lstlisting}
% }% end subfloat ; here crash: "! LaTeX Error: Not in outer par mode." .
% %
% \subfloat*{%
% \begin{lstlisting}[float=h,language=C,caption={A "Hello World" program 3.},label={code:helloworld3},captionpos=b,abovecaptionskip=0em,belowcaptionskip=-2em,columns=flexible,frame=single,linewidth=0.4\linewidth]
%// a program
%#include <stdio.h>
%
%int main(void)
%{
% printf("hello, world 3\n");
% return 0;
%}
%\end{lstlisting}
% }% end subfloat
%\caption{Two hello world programs. }
%\end{figure}
\end{document}