TeXnik - Tips TeX and LaTeX


Welcome to the TeXnik website (under construction)

Sourcecode Listings
TUG logo

Main page

Index
UK FAQ
Documentation
Software
CTAN
Search CTAN:

Statistics

Google c.t.t.

Google d.c.t.t.

 


 


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
0.21 now 0.21 now
first firstline _ numbers
last lastline labelstep stepnumber
stringspaces showstringspaces labelstyle numberstyle
visiblespaces showspaces \thelstlabel \thelstnumber
visibletabs showtabs labelsep numbersep
framerulewidth framerule firstlabel firstnumber
framerulesep rulesep advancelabel _
frametextsep framesep spread _
framespread superceded by
framexleftmargin
framexrightmargin
framextopmargin
framexbottommargin
indent xleftmargin
_ xrightmargin wholeline resetmargins
defaultclass classoffset stringtest _
framerulecolor rulecolor outputpos _
_ columns

Instead of changing all these options you can load listings as

\usepackage [0.21]{listings}
and you'll get the old behaviour.


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
To get a different linespacing in difference to the default text, create your own command.


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: Java demo file

If you use non-typewriterfont you'll get problems with "<<" and ">>", because they are the french quot chars. In this case write in preamble:
\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
}{}

local time: Fri Apr 26 00:03:51 CEST 2024 ; file is: 1768.56122685185 days old
contact webmaster _at_ TeXnik.de