使用 linerange 選項時如何刪除前導不必要的空格?

使用 linerange 選項時如何刪除前導不必要的空格?

老實說,我正在尋找已經listings完全閱讀過文件的人。如果那些人也無法解決它,那麼文件就不會提及任何相關內容。對於由此帶來的不便,我們深表歉意。

但至少我可以透過為您提供最小的工作範例來展示我的努力。

\documentclass[preview,border=12pt,12pt]{standalone}
\usepackage{filecontents}

\begin{filecontents*}{Program.cs}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Delegate
{
    class Program
    {
        // start
        static void Main(string[] args)
        {
            for (int x = 0; x < 10; x++)
                Console.WriteLine(x);
        }
        // stop
    }
}
\end{filecontents*}


\usepackage{accsupp}
\newcommand*{\noaccsupp}[1]{\BeginAccSupp{ActualText={}}#1\EndAccSupp{}}

\usepackage{xcolor}
\usepackage{listings}


\lstdefinestyle{Common}
{   
    language={[Sharp]C},
    numbers=left,
    numbersep=1em,
    numberstyle=\tiny\noaccsupp,
    frame=single,
    framesep=\fboxsep,
    framerule=\fboxrule,
    rulecolor=\color{red},
    xleftmargin=\dimexpr\fboxsep+\fboxrule,
    xrightmargin=\dimexpr\fboxsep+\fboxrule,
    breaklines=true,
    breakindent=0pt,
    tabsize=2,
    columns=flexible,
    includerangemarker=false,
    rangeprefix=//\ ,
}


\lstdefinestyle{A}
{
    style=Common,
    backgroundcolor=\color{yellow!10},
    basicstyle=\scriptsize\ttfamily,
    keywordstyle=\color{blue}\bf,
    identifierstyle=\color{black},
    stringstyle=\color{red},
    commentstyle=\color{green}
}

\begin{document}
\section*{Full Code}
\lstinputlisting[style=A]{Program.cs}
\section*{Code Snippet}
\lstinputlisting[style=A,linerange=start-stop]{Program.cs}
\end{document}*

在此輸入影像描述

我的問題是如何在使用選項時刪除前導空格linerange

編輯

考慮下面的一些極端情況。

情況1

namespace Delegate
{
    class Program
    {
        // start
        static void Main(string[] args)
        {
            for (int x = 0; x < 10; x++)
                Console.WriteLine(x);
        }      
    }        
}
// stop

程式碼應呈現如下

includrangemarker=true

        // start
        static void Main(string[] args)
        {
            for (int x = 0; x < 10; x++)
                Console.WriteLine(x);
        }      
    }        
}
// stop

includerangemarker=false

        static void Main(string[] args)
        {
            for (int x = 0; x < 10; x++)
                Console.WriteLine(x);
        }      
    }        
}

案例2

namespace Delegate
{
    class Program
    {
    // start
        static void Main(string[] args)
        {
            for (int x = 0; x < 10; x++)
                Console.WriteLine(x);
        }      
    }        
}
    // stop

程式碼應呈現如下

includerangemarker=true

    // start
        static void Main(string[] args)
        {
            for (int x = 0; x < 10; x++)
                Console.WriteLine(x);
        }      
    }        
}
    // stop

includerangermarker=false

        static void Main(string[] args)
        {
            for (int x = 0; x < 10; x++)
                Console.WriteLine(x);
        }      
    }        
}

答案1

這是一個解決方案。更多詳細資訊如下。

在此輸入影像描述

此選項的限制gobble(由 提供listings

listings包提供了一個名為 的鍵gobble,它允許用戶指定固定的每行開頭要吞噬的字元數(空格或其他)。然而,gobble它受到以下限制。

  • 它缺乏自動化:使用者必須事先查看清單以確定應該佔用多少空間。
  • 最重要的是,該gobble金鑰僅與嵌入式清單相容(即使用環境排版lstlisting不是清單駐留在獨立文件中(即使用排版\lstinputlisting)。

此選項的限制autogobble(由 提供lstautogobble

lstautogobble軟體包提供了一個名為 的布林鍵autogobble,它可以自動吞噬前導空格;更具體地說,它測量領先空間在第一行(無論是否排版)整個清單並將該值傳遞給gobble鍵。然而,autogobble它受到以下限制。

  • 因為它計算清單第一行的前導空格,所以如果排版的行範圍不是從第 1 行開始,它可能不會刪除適當數量的前導空格。
  • 因為它是基於gobble密鑰的,所以它繼承了後者與\lstinputlisting.

用於刪除前導空格的新鍵:autounindent autodedent

下面的方法定義了一個布林鍵,稱為autounindent autodedent, 哪個,

  • 如果設置,即使僅排版了一系列行(即,如果使用了該firstline選項或該選項),也會刪除前導空白,linerange
  • 相容lstlisting\lstinputlisting

已知的限制

下面的程式碼還不能正確處理製表符。

此外,期望的行為極端情況您的編輯中涵蓋的內容似乎很難實現,因為它需要兩次傳遞。我已經超出我的深度了!我能做的最好的事情就是吞噬標記後面的行開頭處的空格(即程式碼中// start以 開頭的行)。static void

如有任何其他問題,請通知我。

更新:我已將密鑰重命名為autodedent(這比 更容易讀寫autounindent)。我還糾正了一個OP報告的問題, 根據Heiko Oberdiek 的回答。最後,為了方便起見,我在一個名為 ; 的小包中實現了該功能lstautodedent。 alpha 版本可在https://github.com/jubobs/lstautodedent

程式碼

\documentclass[preview,border=12pt,12pt]{standalone}

\usepackage{filecontents}

\begin{filecontents*}{Program.cs}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Delegate
{
    class Program
    {
        // start
        static void Main(string[] args)
        {
            for (int x = 0; x < 10; x++)
                Console.WriteLine(x);
        }
        // stop
    }
}
\end{filecontents*}


\usepackage{accsupp}
\newcommand*{\noaccsupp}[1]{\BeginAccSupp{ActualText={}}#1\EndAccSupp{}}

\usepackage{xcolor}
\usepackage{listings}
\usepackage{lstautodedent}


\lstdefinestyle{Common}
{   
    language={[Sharp]C},
    numbers=left,
    numbersep=1em,
    numberstyle=\tiny\noaccsupp,
    frame=single,
    framesep=\fboxsep,
    framerule=\fboxrule,
    rulecolor=\color{red},
    xleftmargin=\dimexpr\fboxsep+\fboxrule,
    xrightmargin=\dimexpr\fboxsep+\fboxrule,
    breaklines=true,
    breakindent=0pt,
    tabsize=2,
    columns=flexible,
    includerangemarker=false,
    rangeprefix=//\ ,
}


\lstdefinestyle{A}
{
    style=Common,
    backgroundcolor=\color{yellow!10},
    basicstyle=\scriptsize\ttfamily,
    keywordstyle=\color{blue}\bf,
    identifierstyle=\color{black},
    stringstyle=\color{red},
    commentstyle=\color{green}
}

\begin{document}
\section*{Full Code}
\lstinputlisting[style=A]{Program.cs}
\section*{Code Snippet}
\lstinputlisting[style=A,linerange=start-stop,autodedent]{Program.cs}
\end{document}*

相關內容