如何定义列表示例环境

如何定义列表示例环境

我已阅读清单手册文档。我想使用该lstsample环境。

它如下所示:

例子

基本上,输出在左边,代码放在右边。

但是我没有找到这个环境在哪里定义。像这样定义一个新环境容易吗?

答案1

完整的软件包listings可以在 CTAN 找到。

http://www.ctan.org/pkg/listings

该包提供了一个巨大的dtx文件和一个Makefile。在 dtx 文件中,您可以找到定义(参见下面的代码)。

但是如果您使用make all编译生成的文件,listings-devel.pdf您可以在其中找到用户文档和开发人员文档。

如果这部分太难,请查看包装showexpl

% \subsection{The \texttt{lstsample} environment}
%
% \begin{environment}{lstsample}
% We store the verbatim part and write the source code also to file.
%    \begin{macrocode}
\lst@RequireAspects{writefile}
%    \end{macrocode}
%    \begin{macrocode}
\newbox\lst@samplebox
\lstnewenvironment{lstsample}[3][]
    {\global\let\lst@intname\@empty
     \gdef\lst@sample{#2}%
     \setbox\lst@samplebox=\hbox\bgroup
         \setkeys{lst}{language={},style={},tabsize=4,gobble=5,%
             basicstyle=\small\ttfamily,basewidth=0.51em,point={#1}}
         #3%
         \lst@BeginAlsoWriteFile{\jobname.tmp}}
    {\lst@EndWriteFile\egroup
%    \end{macrocode}
% Now |\lst@samplebox| contains the verbatim part.
% If it's too wide, we use atop and below instead of left and right.
%    \begin{macrocode}
     \ifdim \wd\lst@samplebox>.5\linewidth
         \begin{center}%
             \hbox to\linewidth{\box\lst@samplebox\hss}%
         \end{center}%
         \lst@sampleInput
     \else
         \begin{center}%
         \begin{minipage}{0.45\linewidth}\lst@sampleInput\end{minipage}%
         \qquad
         \begin{minipage}{0.45\linewidth}%
             \hbox to\linewidth{\box\lst@samplebox\hss}%
         \end{minipage}%
         \end{center}%
     \fi}
%    \end{macrocode}
% The new keyword class \keyname{point}.
%    \begin{macrocode}
\lst@InstallKeywords{p}{point}{pointstyle}\relax{keywordstyle}{}ld
%    \end{macrocode}
% \end{environment}
%
% \begin{environment}{lstxsample}
% Omitting |\lst@EndWriteFile| leaves the file open.
%    \begin{macrocode}
\lstnewenvironment{lstxsample}[1][]
    {\begingroup
         \setkeys{lst}{belowskip=-\medskipamount,language={},style={},%
             tabsize=4,gobble=5,basicstyle=\small\ttfamily,%
             basewidth=0.51em,point={#1}}
         \lst@BeginAlsoWriteFile{\jobname.tmp}}
    {\endgroup
     \endgroup}
%    \end{macrocode}
% \end{environment}
%
% \begin{macro}{\lst@sampleInput}
% inputs the `left-hand' side.
%    \begin{macrocode}
\def\lst@sampleInput{%
    \MakePercentComment\catcode`\^^M=10\relax
    \small\lst@sample
    {\setkeys{lst}{SelectCharTable=\lst@ReplaceInput{\^\^I}%
                                  {\lst@ProcessTabulator}}%
     \leavevmode \input{\jobname.tmp}}\MakePercentIgnore}
%    \end{macrocode}
% \end{macro}
%

答案2

正如 Marco Daniel 在他的回答中提到的那样,LTXexampleshowexpl很容易让你在生成的输出旁边显示源代码。以下是一个例子:

在此处输入图片描述

通常该LTXexample环境用于显示 LaTeX 源代码和相关输出:

在此处输入图片描述

代码:

\documentclass{article}
\usepackage{amsmath}
\usepackage{xcolor}
\usepackage{showexpl}

\lstdefinestyle{ListingSample}{
    basicstyle=\small\ttfamily,
    numbers=none,
    keywordstyle=\color{blue}\bfseries,
    morekeywords={begin,end,for,maxint,to,do},
    pos=l,
}


\lstdefinestyle{myLatexStyle}{
    language=TeX,
    basicstyle=\small\ttfamily,
    numbers=none,
    backgroundcolor=\color{yellow},
    numbers=left, numberstyle=\tiny, stepnumber=2, numbersep=5pt,
    commentstyle=\color{red},
    showstringspaces=false,
    keywordstyle=\color{blue}\bfseries,
    pos=l,
}

\begin{document}
\begin{LTXexample}[style=ListingSample]
\begin{lstlisting}
for i:=maxint to 0 do
begin
    { do nothing }
end;
\end{lstlisting}
\end{LTXexample}


\begin{LTXexample}[style=myLatexStyle]
\newcommand{\Command}[1]{%
   \texttt{\textbackslash#1}%
}%

Inline math is specified within 
a pair of \$ as in $E = mc^2$.

Display math uses \Command{[}
and \Command{]}.
For example: \[ E = mc^2 \]
\end{LTXexample}

\end{document}

答案3

这是一种粗鲁的破坏环境的方法minipage

\begin{figure}[H]
\begin{minipage}[b]{0.5\linewidth}
\begin{lstlisting}[frame=trBL]
for i:=maxint to 0 do
begin
{ do nothing }
end;
\end{lstlisting}
\end{minipage}
\hspace{0.5cm}
\begin{minipage}[b]{0.5\linewidth}
{\setstretch{.9} 
\begin{verbatim}
\begin{lstlisting}
for i:=maxint to 0 do
begin
{ do nothing }
end;
\end{lstlisting}
\end{verbatim}
}
\end{minipage}
\end{figure}

您可能还需要以下软件包才能使其工作:

\usepackage{setspace} %make the \setstrech 
\usepackage{listings} %lstlisting function
\usepackage[spanish]{babel} %i work under spanish documents so if it doesnt work put this
\usepackage{float} %this one is important so the text before and after the object whit [H] will work

相关内容