General ⇒ ruled float style inside listings
ruled float style inside listings
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.
-----------------------
Learn LaTeX easily with newest books:
The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis
The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more
LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis
ruled float style inside listings
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}
Re: ruled float style inside listings
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
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.
Re: ruled float style inside listings
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
