Document Classeslisting codes get cropped ONLY in odd-numbered pages when ..

Information and discussion about specific document classes and how to create your own document classes.
Post Reply
fractal.generator
Posts: 66
Joined: Tue Oct 07, 2008 9:39 am

listing codes get cropped ONLY in odd-numbered pages when ..

Post by fractal.generator »

Dear all,

I am using listing package to typeset my C# tutorial.
I chose book documentclass with page layout configured by geometry package as follows.

Code: Select all

\documentclass{book}
\usepackage{listings}
\lstset{%
language=[Sharp]C,
frame=single,
numbers=left,
xleftmargin=-\marginparwidth,
xrightmargin=1em%
}

% page layout setting
\usepackage[
a4paper,
twoside,
outer=15mm,
inner=15mm,
includemp=true,
marginparwidth=30mm,
marginparsep=5mm%
]{geometry}

%to show the boundary of textarea, marginsep area, and marginpar area
\newcommand*{\MARKER}{%
{\noindent\strut\vrule\hrulefill~half~\hrulefill\vrule\hrulefill~half~\hrulefill\vrule}%
\marginpar{\strut\vrule\hrulefill~marginpar~\hrulefill\vrule}}


\begin{document}

\newpage
\MARKER
\begin{lstlisting}
using System;
public class Foo
{
	public static void Main()
	{
		Console.WriteLine("Hello World");
	}
}
\end{lstlisting}
\MARKER

\newpage
\MARKER
\begin{lstlisting}
using System;
public class Foo
{
	public static void Main()
	{
		Console.WriteLine("Hello World");
	}
}
\end{lstlisting}
\MARKER

\newpage
\MARKER
\begin{lstlisting}
using System;
public class Foo
{
	public static void Main()
	{
		Console.WriteLine("Hello World");
	}
}
\end{lstlisting}
\MARKER
\end{document}
The problem is :

When I spanned the listing codes accross marginpar using

Code: Select all

xleftmargin=-\marginparwidth,
the listing codes got cropped only in odd-numbered pages.
It seems the listings package did not work well for inner and outer margin setting when I let the listing codes fill the marginpar area.

I really have no idea which part caused this problem. Please help me.

Thank you in advance.


regards,


Hayashi

Recommended reading 2024:

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

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

Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

listing codes get cropped ONLY in odd-numbered pages when ..

Post by Juanjo »

Remove any settings of xleftmargin and xrightmargin and try this:

Code: Select all

\lstset{xleftmargin={\ifodd\value{page}1em\else-\marginparwidth\fi}}
\lstset{xrightmargin={\ifodd\value{page}-\marginparwidth\else1em\fi}}
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
fractal.generator
Posts: 66
Joined: Tue Oct 07, 2008 9:39 am

listing codes get cropped ONLY in odd-numbered pages when ..

Post by fractal.generator »

Juanjo wrote:Remove any settings of xleftmargin and xrightmargin and try this:

Code: Select all

\lstset{xleftmargin={\ifodd\value{page}1em\else-\marginparwidth\fi}}
\lstset{xrightmargin={\ifodd\value{page}-\marginparwidth\else1em\fi}}
Dear Mr. Juanjo,

Thanks for replying.

Your code works well only for listing codes that are not splitted across page.

For relative long listing codes that are splitted across pages, I still got cropped output.

Here is my minimal code snippet:

Code: Select all

\documentclass{book}
\usepackage{listings}
\lstset{%
language=[Sharp]C,
frame=single,
numbers=left,
breaklines=true,
breakautoindent=true,
xleftmargin={\ifodd\value{page}1em\else-\marginparwidth\fi},
xrightmargin={\ifodd\value{page}-\marginparwidth\else1em\fi}%
}

% page layout setting
\usepackage[
paperwidth=15cm,
paperheight=10cm,
twoside,
outer=15mm,
inner=15mm,
includemp=true,
marginparwidth=30mm,
marginparsep=5mm%
]{geometry}

%to show the boundary of textarea, marginsep area, and marginpar area
\newcommand*{\MARKER}{%
{\noindent\strut\vrule\hrulefill~half~\hrulefill\vrule\hrulefill~half~\hrulefill\vrule}%
\marginpar{\strut\vrule\hrulefill~marginpar~\hrulefill\vrule}}


\begin{document}

\MARKER
\begin{lstlisting}
using System;
public class Foo
{
   public static void Main()
   {
      Console.WriteLine("There are many bugs in Latex. It makes me stressed!");
      Console.WriteLine("There are many bugs in Latex. It makes me stressed!");
      Console.WriteLine("There are many bugs in Latex. It makes me stressed!");
      Console.WriteLine("There are many bugs in Latex. It makes me stressed!");
      Console.WriteLine("There are many bugs in Latex. It makes me stressed!");
      Console.WriteLine("There are many bugs in Latex. It makes me stressed!");
      Console.WriteLine("There are many bugs in Latex. It makes me stressed!");
      Console.WriteLine("There are many bugs in Latex. It makes me stressed!");
      Console.WriteLine("There are many bugs in Latex. It makes me stressed!");
      Console.WriteLine("There are many bugs in Latex. It makes me stressed!");
      Console.WriteLine("There are many bugs in Latex. It makes me stressed!");
      Console.WriteLine("There are many bugs in Latex. It makes me stressed!");
      Console.WriteLine("There are many bugs in Latex. It makes me stressed!");
      Console.WriteLine("There are many bugs in Latex. It makes me stressed!");
      Console.WriteLine("There are many bugs in Latex. It makes me stressed!");
      Console.WriteLine("There are many bugs in Latex. It makes me stressed!");
      Console.WriteLine("There are many bugs in Latex. It makes me stressed!");
      Console.WriteLine("There are many bugs in Latex. It makes me stressed!");
      Console.WriteLine("There are many bugs in Latex. It makes me stressed!");
      Console.WriteLine("There are many bugs in Latex. It makes me stressed!");
      Console.WriteLine("There are many bugs in Latex. It makes me stressed!");
   }
}
\end{lstlisting}
\MARKER

\end{document}

Thank you for your time and effort.


regards,

Hayashi
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

listing codes get cropped ONLY in odd-numbered pages when ..

Post by Juanjo »

I don't know of an automatic way to fix the issue, but it is not hard to find a semi-automatic one. Look the following example, which suitable modifications of yours:

Code: Select all

\documentclass{book}
\usepackage{listings}
\lstset{%
language=[Sharp]C,
frame=single,
numbers=left,
breaklines=true,
breakautoindent=true,
xleftmargin={\ifodd\value{page}1em\else-\marginparwidth\fi},
xrightmargin={\ifodd\value{page}-\marginparwidth\else1em\fi}%
}

% page layout setting
\usepackage[
paperwidth=15cm,
paperheight=10cm,
twoside,
outer=15mm,
inner=15mm,
includemp=true,
marginparwidth=30mm,
marginparsep=5mm%
]{geometry}

%to show the boundary of textarea, marginsep area, and marginpar area
\newcommand*{\MARKER}{%
{\noindent\strut\vrule\hrulefill~half~\hrulefill\vrule\hrulefill~half~\hrulefill\vrule}%
\marginpar{\strut\vrule\hrulefill~marginpar~\hrulefill\vrule}}

\newcommand\StartNewPage{\end{lstlisting}
   \newpage
   \begin{lstlisting}[name=Longlisting,frame=lr,escapechar=\%]
}
\newcommand\StartLastPage{\end{lstlisting}
   \newpage
   \begin{lstlisting}[name=Longlisting,frame=blr,escapechar=\%]
}

\begin{document}


\MARKER
\begin{lstlisting}[name=Longlisting,firstnumber=1,frame=tlr,escapechar=\%]
using System;
public class Foo
{
   public static void Main()
   {
      Console.WriteLine("There are many bugs in Latex. It makes me stressed!");
      Console.WriteLine("However, is there any better typesetting system?");
      Console.WriteLine("No, there isn't.");
      Console.WriteLine("There are many bugs in Latex. It makes me stressed!");
      Console.WriteLine("However, is there any better typesetting system?");
      Console.WriteLine("No, there isn't.");%\StartNewPage%
      
      Console.WriteLine("There are many bugs in Latex. It makes me stressed!");
      Console.WriteLine("However, is there any better typesetting system?");
      Console.WriteLine("No, there isn't.");
      Console.WriteLine("There are many bugs in Latex. It makes me stressed!");
      Console.WriteLine("However, is there any better typesetting system?");
      Console.WriteLine("No, there isn't.");
      Console.WriteLine("There are many bugs in Latex. It makes me stressed!");
      Console.WriteLine("However, is there any better typesetting system?");
      Console.WriteLine("No, there isn't.");%\StartNewPage%
      
      Console.WriteLine("There are many bugs in Latex. It makes me stressed!");
      Console.WriteLine("However, is there any better typesetting system?");
      Console.WriteLine("No, there isn't.");
      Console.WriteLine("There are many bugs in Latex. It makes me stressed!");
      Console.WriteLine("However, is there any better typesetting system?");
      Console.WriteLine("No, there isn't.");
      Console.WriteLine("There are many bugs in Latex. It makes me stressed!");
      Console.WriteLine("However, is there any better typesetting system?");
      Console.WriteLine("No, there isn't.");%\StartLastPage%
      
      Console.WriteLine("There are many bugs in Latex. It makes me stressed!");
      Console.WriteLine("However, is there any better typesetting system?");
      Console.WriteLine("No, there isn't.");
   }
   
}
\end{lstlisting}
\MARKER

\end{document}
As you can see, you have to insert \StartNewPage and \StartLastPage, followed by an empty line, at points where you want a page break. Start the lstlisting environment with the options used above. If % is a character included in the code, change it by other suitable character.

It is not an optimal solution, but at least it works.

Perhaps you may contact the author of the listings package and request a "twoside" option, so the values of xleftmargin and xrightmargin are reversed at each page break.
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
fractal.generator
Posts: 66
Joined: Tue Oct 07, 2008 9:39 am

Re: listing codes get cropped ONLY in odd-numbered pages when ..

Post by fractal.generator »

Dear Mr. Juanjo,

First of all, thanks for replying.

Your solution is good for creating static or rarely modified documents.

I will often modify my input file, so it is tedious if I use your semi-automatic approach.

I have posted this issue to persons in charge of listings package project.

Anyway, thank you very much SIR!


regards,


Hayashi
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

listing codes get cropped ONLY in odd-numbered pages when ..

Post by Juanjo »

fractal.generator wrote: I have posted this issue to persons in charge of listings package project.
Please, if you receive an answer, post it here. It may interest other people (and me too, of course).
Meanwhile, you can continue working in your book with the cropped listings. When you arrive to an almost final version, you can fine tune it with the above "semi-automatic" approach, since it has almost no influence in the distribution of pages, just moves lines right or left.
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
Post Reply