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.
-----------------------
General ⇒ ruled float style inside listings
NEW: TikZ book now 40% off at Amazon.com for a short time.

ruled float style inside listings
Hi,
you could proceed as the following example suggests:
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.
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}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
Re: ruled float style inside listings
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
------------
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

Thanks again..
----------
alg1 . First ex
-----
if
else
etc
-------------
-----------
alg2 . Second ex
-----------
if
else
etc
------------
ruled float style inside listings
Take a look at the following code; I think that it will give you a starting point (the code itself contains some explanatory comments):
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.
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}
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,...
Re: ruled float style inside listings
marvellous!! this is what i really want!!
I'm diving into the manuals now for other issues!!
There is too much stuff that i have to read & learn
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
I'm diving into the manuals now for other issues!!



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