我想在 Windows 控制台中显示来自 Python 解释器的文本。这应该是纯文本(就像您在命令行终端中看到的那样)。我还想显示带有突出显示的 Python 代码片段。为此,我将默认列表样式设置为着色 ( \lstset
),然后为命令行终端输出创建自定义列表样式 ( \lstdefinestyle
)。
当我尝试使用带框的框来显示终端输出时,我发现它会尝试完全对齐行并在单词之间添加额外的空格。如何全局设置所有带框的框为左对齐,这样我就不必对齐每个框了?以下是我所做的:
\documentclass[openright,12pt]{book}
%Include Packages
\usepackage{hyperref}
\usepackage{float}
\usepackage{longtable}
\usepackage{listings} %code snippets
\usepackage{color}
\usepackage{calc}
\usepackage{amsmath}
\usepackage{framed} %console output displays
\usepackage{graphicx}
\usepackage{tikz}
\pagestyle{headings}
%Syntax Coloring for code snippets
\lstset{
belowcaptionskip=1\baselineskip,
breaklines=true,
frame=l,
xleftmargin=\parindent,
language=Python,
showstringspaces=false,
basicstyle=\footnotesize\ttfamily,
keywordstyle=\bfseries\color{green!40!black},
commentstyle=\itshape\color{mygray},
identifierstyle=\color{blue},
stringstyle=\color{orange},
numbers=left,
numbersep=15pt,
numberstyle=\tiny\color{gray},
}
%Syntax coloring for inline code at the console
\lstdefinestyle{consolePy}{
breaklines=true,
language=Python,
showstringspaces=false,
xleftmargin=0pt,
frame=none,
numbers=none,
identifierstyle=\ttfamily\color{black},
basicstyle=\footnotesize\color{black},
commentstyle=\ttfamily\color{black},
stringstyle=\ttfamily\color{black},
keywordstyle=\ttfamily\color{black}
}
\begin{document}
\begin{framed}
\begin{lstlisting}[style=consolePy]
>>> MyName = “Jason”
>>> print MyName
Jason
>>> MyAge = 30
>>> print 'My name is', MyName, 'and I'm', MyAge, 'years old.'
My name is Jason and I’m 30 years old.
\end{lstlisting}
\end{framed}
\end{document}
我会展示结果的屏幕截图,但我是新手,所以无法发布图片。只知道结果是我的文本在框架列表中显示大片空白。感谢您提供的任何指导!
答案1
您想使用该选项
columns=fullflexible
这样字母就会像普通打字机字体一样粘在一起。
还要注意,您应该使用愚蠢的引号"
而不是“
,因为listings
它并不真正理解 UTF-8。
\documentclass[openright,12pt]{book}
%Include Packages
\usepackage{hyperref}
\usepackage{float}
\usepackage{longtable}
\usepackage{listings} %code snippets
\usepackage{color}
\usepackage{calc}
\usepackage{amsmath}
\usepackage{framed} %console output displays
\usepackage{graphicx}
\usepackage{tikz}
\pagestyle{headings}
%Syntax Coloring for code snippets
\lstset{
belowcaptionskip=1\baselineskip,
breaklines=true,
frame=l,
xleftmargin=\parindent,
language=Python,
showstringspaces=false,
basicstyle=\footnotesize\ttfamily,
keywordstyle=\bfseries\color{green!40!black},
commentstyle=\itshape\color{mygray},
identifierstyle=\color{blue},
stringstyle=\color{orange},
numbers=left,
numbersep=15pt,
numberstyle=\tiny\color{gray},
}
%Syntax coloring for inline code at the console
\lstdefinestyle{consolePy}{
breaklines=true,
language=Python,
showstringspaces=false,
xleftmargin=0pt,
frame=none,
numbers=none,
identifierstyle=\ttfamily\color{black},
basicstyle=\footnotesize\color{black},
commentstyle=\ttfamily\color{black},
stringstyle=\ttfamily\color{black},
keywordstyle=\ttfamily\color{black},
columns=fullflexible, % <--- added!
}
\begin{document}
\begin{framed}
\begin{lstlisting}[style=consolePy]
>>> MyName = "Jason"
>>> print MyName
Jason
>>> MyAge = 30
>>> print 'My name is', MyName, 'and I'm', MyAge, 'years old.'
My name is Jason and I’m 30 years old.
\end{lstlisting}
\end{framed}
\end{document}