
솔직히 말해서 나는 이미 문서 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
아래 접근 방식은 다음과 같은 부울 키를 정의합니다.autounindent
autodedent
, 어느,
- 설정되면 행 범위만 조판된 경우에도(즉, 옵션
firstline
이나linerange
옵션 중 하나가 사용된 경우) 선행 공백을 제거합니다. - ~이다
lstlisting
둘 다 와 호환 가능\lstinputlisting
.
알려진 제한사항
아래 코드는 아직 탭 문자를 제대로 처리하지 않습니다.
게다가, 원하는 동작은극단적인 경우편집 내용을 다루려면 두 번의 패스가 필요하기 때문에 구현하기가 매우 까다로워 보입니다. 난 너무 힘들어! 내가 할 수 있는 최선의 방법은 마커 바로 뒤의 줄 시작 부분(즉, 코드에서 // start
로 시작하는 줄 ) 에 있는 만큼의 공백을 집어삼키는 것입니다 .static void
다른 문제가 있으면 알려주시기 바랍니다.
업데이트autodedent
: 키 이름을 (보다 읽고 쓰기 쉬운 ) 로 변경했습니다 autounindent
. 나도 수정했어OP가 보고한 문제, 에 따르면Heiko Oberdiek의 답변. 마지막으로 편의를 위해 해당 기능을 다음과 같은 작은 패키지에 구현했습니다.lstautodedent
; 알파 버전은 다음에서 사용할 수 있습니다.https://github.com/jubobs/lstatautodedent
암호
\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}*