Latex:带有等宽字体的列表
这在我用 texlive 创建的列表中。
是的,我使用了 basicstyle=\ttfamily 。查看了列表手册后,我没有找到任何有关固定字体或等宽字体的信息。
复制示例
\documentclass[
article,
a4paper,
a4wide,
%draft,
smallheadings
]{book}
% Packages below
\usepackage{graphicx}
\usepackage{verbatim} % used to display code
\usepackage{hyperref}
\usepackage{fullpage}
\usepackage[ansinew]{inputenc} % german umlauts
\usepackage[usenames,dvipsnames]{color}
\usepackage{float}
\usepackage{subfig}
\usepackage{tikz}
\usetikzlibrary{calc,through,backgrounds}
\usepackage{fancyvrb}
\usepackage{acronym}
\usepackage{amsthm} % Uuhhh yet another package
\VerbatimFootnotes % Required, otherwise verbatim does not work in footnotes!
\usepackage{listings}
\definecolor{Brown}{cmyk}{0,0.81,1,0.60}
\definecolor{OliveGreen}{cmyk}{0.64,0,0.95,0.40}
\definecolor{CadetBlue}{cmyk}{0.62,0.57,0.23,0}
\definecolor{lightlightgray}{gray}{0.9}
\begin{document}
\lstset{
language=C, % Code langugage
basicstyle=\ttfamily, % Code font, Examples: \footnotesize, \ttfamily
keywordstyle=\color{OliveGreen}, % Keywords font ('*' = uppercase)
commentstyle=\color{gray}, % Comments font
numbers=left, % Line nums position
numberstyle=\tiny, % Line-numbers fonts
stepnumber=1, % Step between two line-numbers
numbersep=5pt, % How far are line-numbers from code
backgroundcolor=\color{lightlightgray}, % Choose background color
frame=none, % A frame around the code
tabsize=2, % Default tab size
captionpos=b, % Caption-position = bottom
breaklines=true, % Automatic line breaking?
breakatwhitespace=false, % Automatic breaks only at whitespace?
showspaces=false, % Dont make spaces visible
showtabs=false, % Dont make tabls visible
columns=flexible, % Column format
morekeywords={__global__, __device__}, % CUDA specific keywords
}
\begin{lstlisting}
As[threadRow][threadCol] = A[
threadCol + threadRow * Awidth // Adress of the thread in the current block
+ i * BLOCK_SIZE // Pick a block further left for i+1
+ blockRow * BLOCK_SIZE * Awidth // for blockRow +1 go one blockRow down
];
\end{lstlisting}
\end{document}
This is what the code looks in Xcode.
And this in my listing created with texlive.
And yes I used basicstyle=\ttfamily . Having looked at the manual of listings I haven't found anything about fixed-with or monospace fonts..
Example to reproduce
\documentclass[
article,
a4paper,
a4wide,
%draft,
smallheadings
]{book}
% Packages below
\usepackage{graphicx}
\usepackage{verbatim} % used to display code
\usepackage{hyperref}
\usepackage{fullpage}
\usepackage[ansinew]{inputenc} % german umlauts
\usepackage[usenames,dvipsnames]{color}
\usepackage{float}
\usepackage{subfig}
\usepackage{tikz}
\usetikzlibrary{calc,through,backgrounds}
\usepackage{fancyvrb}
\usepackage{acronym}
\usepackage{amsthm} % Uuhhh yet another package
\VerbatimFootnotes % Required, otherwise verbatim does not work in footnotes!
\usepackage{listings}
\definecolor{Brown}{cmyk}{0,0.81,1,0.60}
\definecolor{OliveGreen}{cmyk}{0.64,0,0.95,0.40}
\definecolor{CadetBlue}{cmyk}{0.62,0.57,0.23,0}
\definecolor{lightlightgray}{gray}{0.9}
\begin{document}
\lstset{
language=C, % Code langugage
basicstyle=\ttfamily, % Code font, Examples: \footnotesize, \ttfamily
keywordstyle=\color{OliveGreen}, % Keywords font ('*' = uppercase)
commentstyle=\color{gray}, % Comments font
numbers=left, % Line nums position
numberstyle=\tiny, % Line-numbers fonts
stepnumber=1, % Step between two line-numbers
numbersep=5pt, % How far are line-numbers from code
backgroundcolor=\color{lightlightgray}, % Choose background color
frame=none, % A frame around the code
tabsize=2, % Default tab size
captionpos=b, % Caption-position = bottom
breaklines=true, % Automatic line breaking?
breakatwhitespace=false, % Automatic breaks only at whitespace?
showspaces=false, % Dont make spaces visible
showtabs=false, % Dont make tabls visible
columns=flexible, % Column format
morekeywords={__global__, __device__}, % CUDA specific keywords
}
\begin{lstlisting}
As[threadRow][threadCol] = A[
threadCol + threadRow * Awidth // Adress of the thread in the current block
+ i * BLOCK_SIZE // Pick a block further left for i+1
+ blockRow * BLOCK_SIZE * Awidth // for blockRow +1 go one blockRow down
];
\end{lstlisting}
\end{document}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题在于
columns=flexible
选项。删除它,它看起来就像你想要的样子。至少,如果您没有实际理由使用它。如果存在,则无法使您的代码片段看起来像 Xcode 中那样:)。The problem is the
columns=flexible
option. Remove it and it looks like you want it to look. At least, if there is no actual reason for you to use it. If there is, there is no way to have the monospaceness of your snippet to look like in Xcode :).当您删除
\usepackage{microtype}
时会发生什么?它会扰乱字距调整等,可能是问题的原因,我认为这是由于对齐不良造成的。What happens when you drop
\usepackage{microtype}
? It messes with kerning and so forth and might be the cause of the problem, which I assume is due to the poor alignment.