这段代码非常符合我的需要:
\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{lipsum}
\usepackage{hyperref}
\usepackage{lstlinebgrd}
\definecolor{myComment}{rgb}{0.0,0.5,0.0}
\definecolor{myKeyword}{rgb}{0.0,0.0,0.5}
\lstset
{
aboveskip=\bigskipamount,
belowskip=\bigskipamount,
frame=tb,
framesep=2pt,
basicstyle=\scriptsize\ttfamily,
keywordstyle=\color{myKeyword},
commentstyle=\color{myComment}\itshape,
captionpos=b,
showstringspaces=false,
fontadjust=true,
language=Delphi,
breaklines=true,
numbers=left,
numberstyle=\tiny,
linebackgroundcolor={\ifodd\value{lstnumber}\color{gray!25}\fi},
numbersep=5pt,
escapeinside={(*@}{@*)},
}
\newcommand\coderef[1]{%
$\Rightarrow$~\ref{#1}:~~%%
}%
\usepackage{hyperref}
\begin{document}
\begin{lstlisting}
program FileTest;
var
myFile: file of Integer;
begin
Assign(myFile, 'c:/test.ext'); (*@\label{FileTest-1}@*)
Rewrite (myFile); (*@\label{FileTest-2}@*)
Write(myFile, 1); (*@\label{FileTest-3}@*)
Write(myFile, 2);
Close(myFile); (*@\label{FileTest-4}@*)
end.
\end{lstlisting}
\coderef{FileTest-1} \lipsum[1]
\coderef{FileTest-2} \lipsum[2]
\coderef{FileTest-3} \lipsum[3]
\coderef{FileTest-4} \lipsum[4]
\end{document}
但在某些情况下,重复整行代码而不是行号可能会更好。我认为对于外部源文件来说,这在某种程度上是可行的,方法是使用
\lstinputlisting[firstline=x,lastline=x]
虽然我还没有进一步调查。但是,在lstlisting
环境中使用源是否可行?如何实现?
答案1
@FloDD,我稍微改变了你的解决方案,以获得适合你的正确数字。
您的宏\lst@PlaceNumber
不是必需的,我在下面的 MWE 中对其进行了评论。
为了获得每行的正确数字,您必须firstnumber=\thelstlinereffirst
在宏调用中添加\lstinputlisting
。
现在的结果是正确的数字。
新的 MWE 打印效果稍微漂亮一些:
\RequirePackage{filecontents}
\begin{filecontents*}{source.pas}
program FileTest;
var
myFile: file of Integer;
begin
Assign(myFile, 'c:/test.ext'); (*@\label{FileTest-1}@*)
Rewrite (myFile); (*@\label{FileTest-2}@*)
Write(myFile, 1); (*@\label{FileTest-3}@*)
Write(myFile, 2);
Close(myFile); (*@\label{FileTest-4}@*)
end.
\end{filecontents*}
\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{lstlinebgrd}
\usepackage{refcount,lipsum}
\usepackage{hyperref}
\definecolor{myComment}{rgb}{0.0,0.5,0.0}
\definecolor{myKeyword}{rgb}{0.0,0.0,0.5}
\lstset
{
aboveskip=\bigskipamount,
belowskip=\bigskipamount,
frame=tb,
framesep=2pt,
basicstyle=\scriptsize\ttfamily,
keywordstyle=\color{myKeyword},
commentstyle=\color{myComment}\itshape,
captionpos=b,
showstringspaces=false,
fontadjust=true,
language=Delphi,
breaklines=true,
numbers=left,
numberstyle=\tiny,
linebackgroundcolor={\ifodd\value{lstnumber}\color{gray!25}\fi},
numbersep=5pt,
escapeinside={(*@}{@*)},
}
\newcommand\coderef[1]{%
$\Rightarrow$~\ref{#1}:~~%%
}%
\newcounter{lstlinereffirst}
\newcounter{lstlinereflast}
\makeatletter
\let\rc@refused\refused% See http://tex.stackexchange.com/q/32807/5764
\newcommand{\lineref}[3]{{%
\let\label\@gobble%
\setcounterref{lstlinereffirst}{#1}%
\setcounterref{lstlinereflast}{#2}%
\lstinputlisting[%
firstline=\thelstlinereffirst,%
lastline=\thelstlinereflast,%
firstnumber=\thelstlinereffirst%
]{#3}
}}
%\def\lst@PlaceNumber{\llap{\normalfont% http://tex.stackexchange.com/q/110187/5764
%\lst@numberstyle{\the\lst@lineno}\kern\lst@numbersep}}%\lst@linebgrd}
\makeatother
\begin{document}
\lstinputlisting{source.pas}
\coderef{FileTest-1} \lipsum[1]
\coderef{FileTest-2} \lipsum[2]
\lineref{FileTest-1}{FileTest-3}{source.pas} \lipsum[3]
\coderef{FileTest-4} \lipsum[4]
\end{document}
结果:
答案2
我不完全确定这是否正是你想要的,但它提供了合理的输出。
\documentclass{article}
\usepackage{xcolor,listings,hyperref}% http://ctan.org/pkg/{xcolor,listings,hyperref}
\usepackage{filecontents,refcount,lipsum}% http://ctan.org/pkg/{filecontents,refcount,lipsum}
\lstset
{
aboveskip=\bigskipamount,
belowskip=\bigskipamount,
frame=tb,
framesep=2pt,
basicstyle=\scriptsize\ttfamily,
keywordstyle=\color{myKeyword},
commentstyle=\color{myComment}\itshape,
captionpos=b,
showstringspaces=false,
fontadjust=true,
language=Delphi,
breaklines=true,
numbers=left,
numberstyle=\tiny,
numbersep=5pt,
escapeinside={(*@}{@*)},
}
\definecolor{myComment}{rgb}{0.0,0.5,0.0}
\definecolor{myKeyword}{rgb}{0.0,0.0,0.5}
\newcommand{\coderef}[1]{%
$\Rightarrow$~\ref{#1}:~~%%
}%
\newcounter{lstlineref}
\makeatletter
\let\rc@refused\refused% See https://tex.stackexchange.com/q/32807/5764
\newcommand{\lineref}[2]{{%
\let\label\@gobble%
\setcounterref{lstlineref}{#1}%
\lstinputlisting[firstline=\thelstlineref,lastline=\thelstlineref]{#2}
}}
\def\lst@PlaceNumber{\llap{\normalfont% https://tex.stackexchange.com/q/110187/5764
\lst@numberstyle{\the\lst@lineno}\kern\lst@numbersep}}
\makeatother
\begin{document}
\begin{filecontents*}{source.p}
program FileTest;
var
myFile: file of Integer;
begin
Assign(myFile, 'c:/test.ext'); (*@\label{FileTest-1}@*)
Rewrite (myFile); (*@\label{FileTest-2}@*)
Write(myFile, 1); (*@\label{FileTest-3}@*)
Write(myFile, 2);
Close(myFile); (*@\label{FileTest-4}@*)
end.
\end{filecontents*}
\lstinputlisting{source.p}
\coderef{FileTest-1} \lipsum[1]
\coderef{FileTest-2} \lipsum[2]
\lineref{FileTest-3}{source.p} \lipsum[3]
\coderef{FileTest-4} \lipsum[4]
\end{document}
这个想法是将代码写入文件(使用filecontents
),然后输入整个列表 - 这提高了可移植性,源代码包含在源代码中,.tex
而不是作为独立文件包含在其他地方。此外,这允许使用新宏轻松提取一行。它打印包含引用的\lineref{<ref>}{<source>}
行。<source>
<ref>
refcount
提供引用计数器接口。此外,\label
还启动了本地无操作以避免产生重复标签。
当导入范围并希望与原始源保持一致的编号时,需要进行行号校正,这要归功于列出符合linerange
规范的行号。
根据您的发行日期,您可能缺少 的定义\rc@refused
。我已经包括了补丁仅当您遇到错误时才应使用它。
答案3
@Werner:为了更好地满足我的需要,我对你的答案做了一些修改:
\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{filecontents,refcount,lipsum}
\usepackage{hyperref}
\usepackage{lstlinebgrd}
\definecolor{myComment}{rgb}{0.0,0.5,0.0}
\definecolor{myKeyword}{rgb}{0.0,0.0,0.5}
\lstset
{
aboveskip=\bigskipamount,
belowskip=\bigskipamount,
frame=tb,
framesep=2pt,
basicstyle=\scriptsize\ttfamily,
keywordstyle=\color{myKeyword},
commentstyle=\color{myComment}\itshape,
captionpos=b,
showstringspaces=false,
fontadjust=true,
language=Delphi,
breaklines=true,
numbers=left,
numberstyle=\tiny,
linebackgroundcolor={\ifodd\value{lstnumber}\color{gray!25}\fi},
numbersep=5pt,
escapeinside={(*@}{@*)},
}
\newcommand\coderef[1]{%
$\Rightarrow$~\ref{#1}:~~%%
}%
\newcounter{lstlinereffirst}
\newcounter{lstlinereflast}
\makeatletter
\let\rc@refused\refused% See http://tex.stackexchange.com/q/32807/5764
\newcommand{\lineref}[3]{{%
\let\label\@gobble%
\setcounterref{lstlinereffirst}{#1}%
\setcounterref{lstlinereflast}{#2}%
\lstinputlisting[firstline=\thelstlinereffirst,lastline=\thelstlinereflast]{#3}
}}
\def\lst@PlaceNumber{\llap{\normalfont% http://tex.stackexchange.com/q/110187/5764
\lst@numberstyle{\the\lst@lineno}\kern\lst@numbersep}\lst@linebgrd}
\makeatother
\begin{document}
\begin{filecontents*}{source.pas}
program FileTest;
var
myFile: file of Integer;
begin
Assign(myFile, 'c:/test.ext'); (*@\label{FileTest-1}@*)
Rewrite (myFile); (*@\label{FileTest-2}@*)
Write(myFile, 1); (*@\label{FileTest-3}@*)
Write(myFile, 2);
Close(myFile); (*@\label{FileTest-4}@*)
end.
\end{filecontents*}
\lstinputlisting{source.pas}
\coderef{FileTest-1} \lipsum[1]
\coderef{FileTest-2} \lipsum[2]
\lineref{FileTest-1}{FileTest-3}{source.pas} \lipsum[3]
\coderef{FileTest-4} \lipsum[4]
\end{document}
现在我可以指定代码行的范围并绘制斑马风格的背景。谢谢你的帮助!