![如何定义列表示例环境](https://linux22.com/image/251466/%E5%A6%82%E4%BD%95%E5%AE%9A%E4%B9%89%E5%88%97%E8%A1%A8%E7%A4%BA%E4%BE%8B%E7%8E%AF%E5%A2%83.png)
我已阅读清单手册文档。我想使用该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 在他的回答中提到的那样,LTXexample
,这showexpl
很容易让你在生成的输出旁边显示源代码。以下是一个例子:
通常该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