[Tex/LaTex] How to control the left margin for the formatted text produced by LTXexample


I want to use both LaTeX code snippet using LTXexample and C# code snippet using lstlisting in my book. If you see my screenshot below, lstlisting behaves well but LTXexample does not. How to prevent LTXexample from expanding its left frame rule beyond the document left margin?

alt text

My minimal code is given as follows:

    breaklines=true,%default: false
    showstringspaces=false,%default: true
    frame=single,%default: none 
    framerule=0.2pt,%expands outward 
    framesep=3pt,%expands outward
    %make the frame fits in the text area. 
    %not affect  LTX when numbers=none.
    xleftmargin=3.2pt,%does not affect LTXexample, why? I don't know!
    xrightmargin=3.2pt,%does not affect LTXexample, why? I don't know!
    tabsize=2,%default: 8 only influence the lstlisting and lstinline.
    explpreset={}%to remove the default setting of LTXexample.

\chapter{Bug or Feature?}
I don't like line numbers because they will make copying the codes
 no longer convenient for the readers---the readers will be forced 
 to do an  extra job to remove the line numbers later after pasting 
 the copied codes to their editors. Line numbers will be useful if 
 the author make references to code snippet. In my book, I will not 
 make references. Why? Because I believe you have a good sense to 
 understand the context.

\LaTeXe\ is fun! But \LaTeXe\ without PSTricks looks like a soup 
without salt.
$E=E$ and $mc^2=mc^2$. $E$ is not equal to $mc^2$. 

using System;
public static class Foo
        public static void Main()
            Console.WriteLine("Hello Universe, I am E.T.");


Best Answer

Odd. For one reason or another, the showexpl package resets listing's xleftmargin keyval to 0pt if numbers=none (the default that you use). Since this isn't the behaviour you want, you might as well delete the offending code. Rather than rewriting the whole target command, you can use the etoolbox package's \patchcmd command to make the change in style. Just add the following lines after your \usepackage{showexpl} line:

  {\typeout{***Successfully patched \protect\SX@codeInput***}}
  {\typeout{***ERROR! Failed to patch \protect\SX@codeInput***}}

NB1, Rolf Niepraschk (showexpl's designer) probably had a good reason to set xleftmargin=0pt when numbers=none. It's quite possible that this patch might break something that running it through your sample code has not effectively flushed out. If it's important to you, you might email him for his thoughts.

NB2, like any patching efforts, you'll either need to write a bunch of conditionals around this code or you'll need to keep a close eye on things against the possibility that some future update to showexpl could break the changes made.

Related Question