|
|
Welcome to the TeXnik web site Sourcecode Listings |
![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Description |
Errors |
Examples |
Inline code |
Refs to single lines |
Singlespacing listings |
ListOfListings-Title in Chapter-Layout |
Uppercase Keywords |
Vertical Spacing |
Old - New Version of package listings.sty
Instead of changing all these options you can load listings as
Title in Chapter-Layout If you like the title for the List of Listings in Chapter-Layout than write in preamble: \renewcommand\lstlistoflistings{%
\if@twocolumn
\@restonecoltrue\onecolumn
\else
\@restonecolfalse
\fi
\chapter{\lstlistlistingname}
% \@mkboth{\MakeUppercase\lstlistlistingname}%
% {\MakeUppercase\lstlistlistingname}}
\@starttoc{lof}%
\if@restonecol\twocolumn\fi
}
Uppercase Keywords This is just impossible with user-level commands and options. The following option definition for keywordstyle uses internal control sequences and will work in many cases:
lstset{keywordstyle=\uppercase\expandafter{\expandafter\lst@token
\expandafter{\the\lst@token}}}
It's also possible to pass this as an option of \begin{lstlisting[...] ... \end{lstlisting}
Crossrefs to single lines of the listing It's possible to place labels in the code so that you can reference those line numbers using the \ref{} command (written in TeX-Mode). in preamble: \lstset{escapeinside={/*@}{@*/}}
In the text as an example:
\begin{lstlisting}[stepnumber=1,numberstyle=\tiny,numbersep=5pt]{}
void f(int i) {
int x; /*@\label{x-lbl}@*/
...
}
\end{lstlisting}
also in the text the reference
blah \ref{x-lbl} ...
Errors/Problems with Listings If you get an umwanted pagebreak inside a listing than try the following: comment out in the package listings.sty the lines 931-940 %\lst@AddToHook{Init}
%{\edef\lst@OrgOutput{\the\output}%
% \output{\global\setbox\lst@gtempboxa\box\@cclv
% \expandafter\egroup
% \lst@SaveToken
% \lst@InterruptModes
% \setbox\@cclv\box\lst@gtempboxa
% \bgroup\lst@OrgOutput\egroup
% \bgroup
% \aftergroup\lst@ReenterModes\aftergroup\lst@RestoreToken}}
Pay attention, because this can occur other problems.
Singlespacing Listings If you have a text without another spacing than single, but want to insert the listing in singlespacing, write: in preamble: \usepackage{setspace}
in the doc:
blah ...
\singlespacing
\lstinputlisting[...
\setstretch{
For soublespacing it's a \setstretch{2.0}
Vertical Spacing
General Description in latex preamble write \usepackage{listings}
\renewcommand\lstlistingname{Program}% default is Listing
\renewcommand\lstlistlistingname{List of Programs}% default is Listings
\renewcommand\thelstlisting{\thechapter .\arabic{lstlisting}}% captionstyle
The option breaklines enables breaking too long lines, otherwise they are cut by pagewidth.
In the text the macro is called with
\lstinputlisting[%
caption={my Caption text},%
label={prog1},%
showstringspaces=false,%
frame={tb},%
lineskip=-1pt,%
extendedchars=true,%
basicstyle=\footnotesize\ttfamily,%
numbers=left,%
stepnumber=1,%
numberstyle=\tiny,%
xleftmargin=2em,%
language=Java,%
breaklines]{Welcome.java}
All options are selfexplanatory, for other options have a look at the listings manual. Some of he supported languages are:Ada, C, C++, Cobol, Delphi, HTML, Java, Modula-2, Pascal, Perl, TeX :-), Algol, and a lot of others (see manual)
Demo: \lstset{MoreSelectCharTable=%
\lst@ReplaceInput{>}{\hspace{0.2em}\lst@ProcessOther{\hspace{-0.2em}>}}%
\lst@ReplaceInput{<}{\hspace{0.2em}\lst@ProcessOther{\hspace{-0.2em}<}}%
}
If you use the caption option for the listings and prefer left-justified write in preamble: \long\def\@makecaption#1#2{%
\vskip\abovecaptionskip
\sbox\@tempboxa{#1: #2}%
\ifdim \wd\@tempboxa >\hsize
#1: #2\par
\else
\global \@minipagefalse
\hb@xt@\hsize{\box\@tempboxa}%
\fi
\vskip\belowcaptionskip}
If your documentclass doesn't define the command abovecaptionskip than add to preamble:
\@ifundefined{abovecaptionskip}{%
\newlength\abovecaptionskip
\newlength\belowcaptionskip
\setlength{\abovecaptionskip}{10pt}% aus article.cls
\setlength{\belowcaptionskip}{0pt}% aus article.cls
}{}
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||