【问题标题】:Latex: Listings with monospace fontsLatex:等宽字体的列表
【发布时间】:2010-05-26 13:03:14
【问题描述】:

这就是代码在 Xcode 中的样子。

这在我用 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}

【问题讨论】:

  • 你能提供一个最小的工作示例吗?即,只有您的 listings 环境和重现错误代码所需的包。
  • 嗨 Nils,我认为你的代码很棒,我什至可以将它与 Fortran 90 一起使用。你介意我将它用于我的论文吗? ;)

标签: latex


【解决方案1】:

问题在于columns=flexible 选项。删除它,它看起来就像你想要的那样。至少,如果您没有实际理由使用它。如果有,则无法让您的 sn-p 的等宽性在 Xcode 中看起来像:)。

【讨论】:

  • 记得使用basicstyle=\ttfamily,否则columns=fixed不起作用。 (我的情况)
  • 使用basicstyle=\ttfamily时,columns=flexible可以保留,只要使用keepspaces=true选项即可。这将给出一个更“包装”的外观。类似于逐字逐句。
【解决方案2】:

当您删除\usepackage{microtype} 时会发生什么?它与字距调整等混淆,可能是问题的原因,我认为这是由于对齐不佳所致。

【讨论】:

  • 这不是很明显吗?我希望它使用等宽字体,这样它看起来就像在 Xcode 中(它使用等宽字体)。
  • 这不应该是问题所在,微型手册在第 9 章:提示和注意事项中指出,它可以与 fancyvrblistings 很好地配合使用。当他显示lstset 时,我认为他使用此代码的列表:)。
  • 那会是制表符与空格的问题吗?
  • 缩进行的前导空格似乎确实是问题所在。文本的其余部分似乎是想要的等宽。这些是空格还是制表符?
  • 文本只是空格,没有使用制表符。我检查了两次.. 删除缩微版没有效果。但是到目前为止,感谢您提供帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-29
  • 2013-01-09
  • 2020-12-17
  • 1970-01-01
  • 1970-01-01
  • 2014-06-09
相关资源
最近更新 更多