Generalruled float style inside listings

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
xlynx3
Posts: 6
Joined: Fri Jan 30, 2009 2:17 pm

ruled float style inside listings

Post by xlynx3 »

Hi, I'm new at latex,
I'm using listings package and writing algorithm. I want to want to put
ruled style captions(or ruled style floats) inside listings but i can
not do it. I'm putting the desired style below. Algorithm package create
captions in this style but i cannot figure it out how to create same appearance in listing package.
Thanks.

---------------------
algoritm1
----------------------
1 begin
2 etc
3
4.

-----------------------

Recommended reading 2024:

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

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

gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

ruled float style inside listings

Post by gmedina »

Hi,

you could proceed as the following example suggests:

Code: Select all

\documentclass{article}
\usepackage{caption}
\usepackage{listings}

%if the caption or title fits in a single line, do not center it
\captionsetup{singlelinecheck=false}

\begin{document}

\noindent\hrulefill
\begin{lstlisting}[title={\bfseries Algorithm 1},frame=tb,abovecaptionskip=-.8em,belowcaptionskip=.7em]
for i:=maxint to 0 do
begin
j:=square(root(i));
end;
\end{lstlisting}

\end{document}
Of course, you can adapt my example according to your needs; feel free to do so. Please refer to the documentation of the used packages for further information.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
xlynx3
Posts: 6
Joined: Fri Jan 30, 2009 2:17 pm

Re: ruled float style inside listings

Post by xlynx3 »

thanks for your answer. It works perfectly well for now.
I'm also reading manuals of packages, but I have been using LaTeX for 2 days so it's very hard to come up with a clever solution. ;)

\noindent\hrulefill
If i need a second algorithm box in a page, I should place the above line. I think, I have to create my own caption style(or similar type of thing, i haven't now the naming convention yet :) to get rid of that line. Then a title or caption code create this type of template easily. Could you show or refer me a way to do this. I really want to do it myself in order to learn LaTeX..

Thanks again..




----------
alg1 . First ex
-----
if
else
etc
-------------

-----------
alg2 . Second ex
-----------
if
else
etc
------------
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

ruled float style inside listings

Post by gmedina »

Take a look at the following code; I think that it will give you a starting point (the code itself contains some explanatory comments):

Code: Select all

\documentclass{article}
\usepackage{caption}
\usepackage{listings}

%this new format will add a horizontal rule before the caption,
%and will put the caption label (i.e., "Algorithm" and its number) in boldface 
%and use a period as separator
\DeclareCaptionFormat*{mystyle}{\noindent\hrulefill\par\bfseries#1.\space\normalfont#3\par}
%use the newly created format in lstlisting environments and if the caption 
%or title fits in a single line, do not center it
\captionsetup[lstlisting]{format=mystyle,singlelinecheck=false}

%change "Listing" to "Algorithm"
\renewcommand\lstlistingname{Algorithm}
%add two horizontal lines and increase the vertical space between the caption 
%and the listing
\lstset{frame=tb,belowcaptionskip=.7em}

\begin{document}

\begin{lstlisting}[caption={First example}]
for i:=maxint to 0 do
begin
j:=square(root(i));
end;
\end{lstlisting}

some text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text

\begin{lstlisting}[caption={Second example}]
for j:=1 to maxint do
begin
j:=i;
end;
\end{lstlisting}

\end{document}
After reading the documentation of the packages, feel free to ask any doubts or additional problems that you might encounter.

Remark: make sure that you are using a recent version of the caption package.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
xlynx3
Posts: 6
Joined: Fri Jan 30, 2009 2:17 pm

Re: ruled float style inside listings

Post by xlynx3 »

marvellous!! this is what i really want!!
I'm diving into the manuals now for other issues!! :P There is too much stuff that i have to read & learn 8-) Thanks one more time ;)

ps. thanks for warning me about the versions.. I have got an incomplete manual about captions. I just found a comprehensive one ;)
Post Reply