我喜欢 verbatim 环境的样子,并且我想让我的 lstlistings 看起来像它。
我尝试将基本样式设置为\ttfamily
,但字距和行距似乎不同。有什么方法可以让它看起来像逐字一样吗?
这是一个完整的例子:
\documentclass[a4paper]{report}
\usepackage{listings}
\lstset{basicstyle=\ttfamily}
\begin{document}
This is lstlisting:
\begin{lstlisting}
: paxos.learn ( addr n v -- Ethernet packet )
2 paxos.pack32 ( addr n v -- addr payload )
paxos.eth_type.learn ( addr payload -- addr payload ethtype )
swap paxos.eth_packet ; ( addr payload ethtype -- ethernet_packet )
\end{lstlisting}
This is verbatim (and what I want):
\begin{verbatim}
: paxos.learn ( addr n v -- Ethernet packet )
2 paxos.pack32 ( addr n v -- addr payload )
paxos.eth_type.learn ( addr payload -- addr payload ethtype )
swap paxos.eth_packet ; ( addr payload ethtype -- ethernet_packet )
\end{verbatim}
\end{document}
看起来像:
编辑
我尝试设置该选项columns=flexible
,但列表将忽略源代码中的重要空间。
答案1
这是一种方法;我非常喜欢这种方法basewidth
,因为它不会将每个字符装箱,也不需要猜测等宽字体是否使用半个 em 字符宽度。
\documentclass[a4paper]{report}
\usepackage{listings}
\begin{document}
\lstset{
basicstyle=\ttfamily,
columns=fullflexible,
keepspaces=true,
}
\verb|basicstyle=\ttfamily, columns=fullflexible, keepspaces=true|
\begin{lstlisting}
: paxos.learn ( addr n v -- Ethernet packet )
2 paxos.pack32 ( addr n v -- addr payload )
paxos.eth_type.learn ( addr payload -- addr payload ethtype )
swap paxos.eth_packet ; ( addr payload ethtype -- ethernet_packet )
\end{lstlisting}
This is verbatim (and what I want):
\begin{verbatim}
: paxos.learn ( addr n v -- Ethernet packet )
2 paxos.pack32 ( addr n v -- addr payload )
paxos.eth_type.learn ( addr payload -- addr payload ethtype )
swap paxos.eth_packet ; ( addr payload ethtype -- ethernet_packet )
\end{verbatim}
\end{document}
答案2
fixed
使用 的默认设置columns
,listings
保留0.6em
字符;您需要将其调整为0.5em
使用basewidth
:
\documentclass[a4paper]{report}
\usepackage{listings}
\lstset{
basicstyle=\ttfamily,
columns=fixed,
fontadjust=true,
basewidth=0.5em
}
\begin{document}
This is lstlisting:
\begin{lstlisting}
: paxos.learn ( addr n v -- Ethernet packet )
2 paxos.pack32 ( addr n v -- addr payload )
paxos.eth_type.learn ( addr payload -- addr payload ethtype )
swap paxos.eth_packet ; ( addr payload ethtype -- ethernet_packet )
\end{lstlisting}
This is verbatim (and what I want):
\begin{verbatim}
: paxos.learn ( addr n v -- Ethernet packet )
2 paxos.pack32 ( addr n v -- addr payload )
paxos.eth_type.learn ( addr payload -- addr payload ethtype )
swap paxos.eth_packet ; ( addr payload ethtype -- ethernet_packet )
\end{verbatim}
\end{document}