如何控制 LTXexample 生成的格式化文本的左边距?

如何控制 LTXexample 生成的格式化文本的左边距?

我想在我的书中同时使用 LaTeX 代码片段LTXexample和 C# 代码片段lstlisting。如果您看到下面的屏幕截图,则lstlisting表现良好,但LTXexample实际上并非如此。如何防止LTXexample其左框架规则超出文档左边距?

替代文本

我的最小代码如下:

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

答案1

很奇怪。出于某种原因,包将的keyvalshowexpl重置为if (您使用的默认值)。由于这不是您想要的行为,您不妨删除有问题的代码。您可以使用包的命令来更改样式,而不是重写整个目标命令。只需在您的行后添加以下几行:listingxleftmargin0ptnumbers=noneetoolbox\patchcmd\usepackage{showexpl}

\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的设计者)可能有充分的理由设定xleftmargin=0pt何时numbers=none。这个补丁很可能会破坏一些在运行示例代码时没有有效清除的东西。如果这对你很重要,你可以给他发电子邮件,征求他的意见。

NB2,与任何修补工作一样,您要么需要围绕此代码编写一堆条件,要么需要密切关注事态,以防止将来的某些更新showexpl可能破坏所做的更改。

答案2

我没有使用补丁方法。而是添加\hspace{\dimexpr\fboxsep+\fboxrule\relax}\SX@CodeArea。我不确定它是否安全。但它产生了所需的输出。

在此处输入图片描述

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

编辑:

我注意到了以下缺陷。

在此处输入图片描述

相关内容