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

showexpl

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:

\documentclass[dvips,final,dvipsnames]{book}
\usepackage[showframe=true,a4paper,margin=20mm,twoside]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{xcolor}
\usepackage{showexpl}
\lstset{%
    breaklines=true,%default: false
    basicstyle=\ttfamily\scriptsize,
    keywordstyle=\color{blue}\sffamily\bfseries,                                                                
    commentstyle=\color{ForestGreen}\itshape,                                   
    stringstyle=\rmfamily,                                                      
    showstringspaces=false,%default: true
    backgroundcolor=\color{Yellow!25},
    %---------------------------------------------------------
    frame=single,%default: none 
    framerule=0.2pt,%expands outward 
    rulecolor=\color{red},
    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.
}

\begin{document}
\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.

\section{LTXexample}
\begin{LTXexample}[pos=b,language={PSTricks}]
\LaTeXe\ is fun! But \LaTeXe\ without PSTricks looks like a soup 
without salt.
\[
E\not=mc^2
\]
$E=E$ and $mc^2=mc^2$. $E$ is not equal to $mc^2$. 
\end{LTXexample}


\section{lstlisting}
\begin{lstlisting}[language={[Sharp]C}]
using System;
public static class Foo
{
        public static void Main()
        {
            Console.WriteLine("Hello Universe, I am E.T.");
        }
}
\end{lstlisting}

\end{document}

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:

\usepackage{etoolbox}
\makeatletter
\patchcmd{\SX@codeInput}{xleftmargin=0pt,xrightmargin=0pt}{}
  {\typeout{***Successfully patched \protect\SX@codeInput***}}
  {\typeout{***ERROR! Failed to patch \protect\SX@codeInput***}}
\makeatother

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