行番号ではなくコード行全体を参照する

行番号ではなくコード行全体を参照する

このコードは私のニーズに非常によく適合します:

\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重複したラベルが作成されないように、ローカライズされた no-op が開始されます。

範囲をインポートし、元のソースとの一貫した番号付けを維持したい場合は、行番号の修正が必要です。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}

ここに画像の説明を入力してください

これで、コード行の範囲を指定して、ゼブラスタイルの背景も描画できるようになりました。ご協力ありがとうございました!

関連情報