tags:

views:

16556

answers:

6
+3  A: 

Take a look at algorithms package, especially the algorithm environment.

avakar
Thanks. This package seems to be very strong in more theoretical algirthm discussion, i know it from many math books. But i wont emphasize this so much (prerequsites, if, else), i would like to have a formatting like the one above.
Mork0075
I was only talking about the `algorithm` environment, not `algorithmic`. `algorithm` is a floating container, which looks pretty nice. You can put whatever you'd like inside, even the `listing` mentioned elsethread.
avakar
+10  A: 
Markus Lux
Thanks. I'am still aware of the listings package, but not able to format like my sample. This is the real question.
Mork0075
+6  A: 
Bastien Léonard
+3  A: 

And please, whatever you do, configure the listings package to use fixed-width font (as in your example; you'll find the option in the documentation). Default setting uses proportional font typeset on a grid, which is, IMHO, incredibly ugly and unreadable, as can be seen from the other answers with pictures. I am personally very irritated when I must read some code typeset in a proportional font.

zvrba
Can you please say how, cant find it.
Mork0075
Try with \lstset{basicstyle=\ttfamily}
zvrba
I personnally use columns=fullflexible with basicstyle=\small\sffamily, as in the example I posted above. The characters aren't vertically aligned, but I think they look better than with \ttfamily. Do you find the sample I posted above ugly?
Bastien Léonard
Your particular example looks fine. However, I would hate it with nested compound statements where a proper indentation (column alignment) is a *big* help to seeing the extent of a compound statement ({} block).
zvrba
I was thinking the same thing, but so far all my listings looked good.
Bastien Léonard
Ok, you've convinced me, I'll try your setup once :-)
zvrba
+27  A: 
Tormod Fjeldskår
I would like to redefine the caption format only for things in the \lstinputlisting section (something like myCaption). Have you got any hint how to do that?
Mork0075
Try /captionsetup[lstlisting]{your options}
Tormod Fjeldskår
This works great, thank you. Have you got an idea how to realize the gray background behind the caption (like in my initial posts sample)? Cant find anything in the documentation.
Mork0075
I think \colorbox{gray}{\parbox{\textwidth}{\textcolor{white}{text goes here}}} would be somewhere near your initial post sample.
Tormod Fjeldskår
Wow, if you can say how to apply this to the caption, you're my hero of the day :) \colorbox{gray}{\parbox{\textwidth}{\textcolor{white}{text goes here}}}\lstinputlisting[label=beispielcode,caption=Ein Beispiel]{sourceCode/HelloWorld.java}
Mork0075
Added some sample code in my answer to get you started.
Tormod Fjeldskår
Thats it, thanks a lot :) Have you also any idea how to indent the text inside the caption?
Mork0075
The margin option, explained in Section 2.4 of the manual, should do the trick.
Tormod Fjeldskår
No, this also indents the whole surrounding colorbox.
Mork0075
Ah, of course... Try adding \hspace{1cm} before #1#2#3 in the DeclareCaptionFormat statement.
Tormod Fjeldskår
You're my hero :)
Mork0075
+2  A: 

There are several other things you can do, such as selecting new fonts:

\documentclass[10pt,a4paper]{article}
% ... lots of packages e.g. babel, microtype, fontenc, inputenc &c.
\usepackage{color}    % Leave this out if you care about B/W printing, obviously.
\usepackage{upquote}  % Turns curly quotes in verbatim text into straight quotes. 
                      % People who have to copy/paste code from the PDF output 
                      % will love you for this. Or perhaps more accurately: 
                      % They will not hate you/hate you less.
\usepackage{beramono} % Or some other package that provides a fixed width font. q.v.
                      % http://www.tug.dk/FontCatalogue/typewriterfonts.html
\usepackage{listings} 
\lstset {                 % A rudimentary config that shows off some features.
    language=Java,
    basicstyle=\ttfamily, % Without beramono, we'd get cmtt, the teletype font.
    commentstyle=\textit, % cmtt doesn't do italics. It might do slanted text though.
    \keywordstyle=        % Nor does cmtt do bold text.
        \color{blue}\bfseries,
    \tabsize=4            % Or whatever you use in your editor, I suppose.
}
\begin{document} 
\begin{lstlisting}
public final int ourAnswer() { return 42; /* Our final answer */ }
\end{lstlisting} 
\end{document}
kahen