GeneralListings package - book

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
nunolf
Posts: 14
Joined: Wed Jul 18, 2007 1:41 pm

Listings package - book

Post by nunolf »

Hi *

I'm a newbie here, and on \Latex too.

I'm inserting some Tcl code into my PhD thesis using the superb listings package.
I'm using book class, twoside as default.
Also, using geometry package, controlling the margins (I have a marginpar defined also).

The problem is that I want the source code (lines with 80 characters) to be printed in the
body + margins, such that I do not need to use a small font (I'm trying to avoid line
breakings). I was not successfull. Because the listings package always extends the listing to
the right, and thus, on even pages the listings goes out the body page.

By default, the base line width for listings is \linewidth. I tryed to change this to:
\textwidth + \marginparwidth + \marginparsep but, the listings package is not able to deal with the odd and
even pages correctly. It always extends the "listing" to the same side (right) (outer on odd
pages, and inner on even pages).

I tryed to play with the conditional "ifthen" package, to control variables xleftmargin and
xrightmargin, but Latex complains about it. On the other hand, I was successful changing the
background color in this way.

Also played a little bit with putting the listings environment inside a minipage, but minipages
do not cross pages. Listings can ;-)

Here is a minimal example of what I'm doing:

Code: Select all

\documentclass[11pt,pdftex]{book}

\usepackage[a4paper,twoside,inner=3cm,outer=6.15cm,marginparwidth=2cm,%
marginparsep=10pt,includeheadfoot]{geometry}

\usepackage{listings}
\usepackage{calc}
\usepackage{color}

%preamble
\newlength{\lstlength}
\setlength{\lstlength}{\textwidth + \marginparwidth + \marginparsep}

\begin{document}

\lstset{linewidth=\lstlength,numbers=left, numbersep=10pt,%
%backgroundcolor=\ifthenelse{\isodd{\thepage}}{\color{gray}}{\color{yellow}}%
backgroundcolor=\color{yellow}
}
\lstinputlisting[firstnumber=3,linerange=3-100]{some.file}

\end{document}
Do you guys have a workaround or any suggestions on this?
Cheers,
Nuno

P.S. Sorry if it was too confusing
Attachments
An odd and even output example. Odd pages are ok, but even pages are not.
An odd and even output example. Odd pages are ok, but even pages are not.
listings_eg.jpg (106.71 KiB) Viewed 2491 times

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Post Reply