
這段程式碼非常適合我的需求:
\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}
現在我可以指定一系列程式碼行並繪製斑馬風格的背景。感謝您的幫忙!