lstlisting
몇 줄의 코드를 렌더링하는 것이 있습니다 . 코드는 들여쓰기당 4개의 공백으로 들여쓰기됩니다. 들여쓰기를 공백 2개로만 변경하는 설정이 있나요? 나는 그것이 있다는 것을 알고 있지만 tabsize
그것은 분명히 탭용이므로 코드 자체를 변경하는 것을 피하고 싶습니다.
어떤 제안이 있으십니까?
답변1
한 가지 가능한 접근 방식은 키를 사용하여 literate
두 개의 연속 공백이 발생할 때마다 하나의 공백으로 바꾸는 것입니다.
주의사항: 이 접근 방식은
- 코드 "내"에서 이러한 교체를 수행합니다(문자열 리터럴에서는 아님).
- 목록에서 선행 공백을 제거하는 데 사용되는
autogobble
옵션(패키지에서 제공 ) 을 방해합니다 .lstautogobble
\documentclass{article}
\setlength\parindent{0pt}
\usepackage{listings}
\usepackage{filecontents}
% code available at http://docs.python.org/2/tutorial/classes.html
\begin{filecontents*}{samplecode.py}
class MyClass:
"""A simple example class"""
i = 12345
def f(self):
return 'hello world'
\end{filecontents*}
\lstdefinestyle{Python2}
{
language=Python,
literate = *{\ \ }{\ }1, %replaces each occurence of two consecutive spaces by one
}
\begin{document}
Indent = 4 spaces (as in the original listing)
\lstinputlisting[language=Python,frame=single]{samplecode.py}
Indent = 2 spaces
\lstinputlisting[style=Python2,frame=single]{samplecode.py}
\end{document}