Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question

2 Answers 2

up vote 9 down vote accepted
+500

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.

share|improve this answer
1  
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.

enter image description here

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

\makeatletter
% Add \hspace{\dimexpr\fboxsep+\fboxrule\relax}
\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},
        commentstyle=\color[rgb]{0.13,0.54,0.13},
        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.

enter image description here

share|improve this answer
    
The easiest solution: leave the numbers=left but change numberstyle=\color{white} to hide the number lines. –  In PSTricks we trust Aug 6 '12 at 18:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.