Graphics, Figures & Tables"Framing" and positioning \lstlisting ?

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
sdaau
Posts: 26
Joined: Fri Feb 19, 2010 2:08 pm

"Framing" and positioning \lstlisting ?

Post by sdaau »

Hi all,

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):
lsttest.png
lsttest.png (255.61 KiB) Viewed 14076 times
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)
Would anyone have any ideas on how this could be pursued? For the search engine's sake :) , typical errors I'm getting here are "! Missing \endcsname inserted." and "! LaTeX Error: Not in outer par mode".

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
does in fact work, and puts the code boxes side by side - but I cannot get outside framing to work (which I'd hope would encapsulate both the label and the code listing box)...


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}

Recommended reading 2024:

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

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

Post Reply