# 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?

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,
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
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}

-

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.

-
upvoted both for an excellent answer, but especially for the \patchcmd. – Yiannis Lazarides Dec 4 '10 at 12:46

I am not using a patch method. Instead I add \hspace{\dimexpr\fboxsep+\fboxrule\relax} to \SX@CodeArea. I am not sure whether it is safe or not. But it produces the required output.

\documentclass[10pt]{article}
\usepackage[paperwidth=15cm,paperheight=6cm,margin=5mm,showframe=true]{geometry}
\usepackage{xcolor}
\usepackage{showexpl}

\makeatletter
\renewcommand\SX@CodeArea[2]{%
\hspace{\dimexpr\fboxsep+\fboxrule\relax}\parbox{#1}{#2}
\rlap{\parbox{#1}{\SX@attachfile}}}
\makeatother

\lstdefinestyle{Common}
{
frame=single,
numbers=none,
rulecolor=\color{red},
breaklines=true,
basicstyle=\scriptsize\ttfamily,
keywordstyle=\color{blue},
backgroundcolor=\color{yellow!10},
tabsize=2,
columns=flexible,
}

\lstdefinestyle{LaTeX}
{
style=Common,
language={[LaTeX]TeX},
pos=r,
linewidth=\dimexpr0.5\textwidth-2\fboxsep-2\fboxrule\relax,
width=\dimexpr0.5\linewidth-1pt\relax,
explpreset={},
}

\lstdefinestyle{CSharp}
{
style=Common,
xleftmargin=\dimexpr\fboxsep+\fboxrule\relax,
xrightmargin=\dimexpr\fboxsep+\fboxrule\relax,
}

\begin{document}

\begin{LTXexample}[style=LaTeX]
$E\not=mc^2$
$E=E$ and $mc^2=mc^2$. $E$ is not equal to $mc^2$.
\end{LTXexample}

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

\end{document}


Edit:

I noticed the following defect.

-
The easiest solution: leave the numbers=left but change numberstyle=\color{white} to hide the number lines. – kiss my armpit Aug 6 '12 at 18:47