Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

Latex: Listings with monospace fonts

This is what the code looks in Xcode. xcode

And this in my listing created with texlive.

alt text

And yes I used basicstyle=tfamily . 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}

egin{document}
lstset{
language=C,                             % Code langugage
basicstyle=tfamily,                   % Code font, Examples: footnotesize, tfamily
keywordstyle=color{OliveGreen},        % Keywords font ('*' = uppercase)
commentstyle=color{gray},              % Comments font
numbers=left,                           % Line nums position
numberstyle=iny,                      % 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
}

egin{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}
question from:https://stackoverflow.com/questions/2913141/latex-listings-with-monospace-fonts

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 :).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...