Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

When I used listings package to import C# code snippet from within my LaTeX file, I got an output as follows.

alt text

At the first line, there is an unnecessary white space because of the BOM (Byte Order Mark) that is intentionally added by Microsoft (R) Visual Studio.

I really want to remove it from the output rather than from the C# file, how to do that?

Here is my LaTeX code:

\documentclass{article}
\usepackage{listings,xcolor}
\lstset{%
language={[Sharp]C},
backgroundcolor=\color{yellow!20},
basicstyle=\tiny,
keywordstyle=\color{blue},
identifierstyle=\color{magenta},
breaklines=true}


\begin{document}
\lstinputlisting{Program.cs}
\end{document}
share|improve this question
6  
Rather than just posting a hyperlink to your question, could you post your actual question here? Thanks. –  Loop Space Nov 25 '10 at 11:54
    
This issue only happens in MikTeX. Without using utf8x encoding and/or extendedchars option, this issue disappears if you use TeXLive. –  Please don't touch Mar 13 '12 at 4:30
add comment

3 Answers 3

up vote 12 down vote accepted
+100

There is actually a space only because you are using an OT1-font. With T1-encoding you would see the three chars coded by the three octetts of the BOM. Assuming that none of them is used in the listing and that your main document is 8-bit encoded you can replace them with literate:

 \documentclass{article}
 \usepackage[T1]{fontenc}
 \usepackage{listings}
 \usepackage{xcolor}
 \lstset{%
 language={[Sharp]C},
 backgroundcolor=\color{yellow!20},
 basicstyle=\tiny,
 keywordstyle=\color{blue},
 identifierstyle=\color{magenta},
 breaklines=true}

 \lstset{
   literate={ï}{}0
            {»}{}0
            {¿}{}0
 }
 \begin{document}
 \lstinputlisting{test-bom.txt}
 \end{document}
share|improve this answer
1  
Thanks for the solution. However, I think the BOM preceding the keyword "using" causes listings.sty not to interpret it as C# keyword. In addition, the result also contains unwanted space. Please see my EDIT 2 in my post above. –  xport Nov 26 '10 at 1:20
1  
The problem are the second set of braces in the literate definition I forgot to delete. I have now deleted them in the code. –  Ulrike Fischer Nov 26 '10 at 8:35
    
Thanks very much Ulrike Fischer. –  xport Nov 26 '10 at 10:19
1  
As I recall, the BOM, U+FEFF, (which is stupid for utf-8 since there is no byte order) is not a valid unicode character so there should be no danger of it appearing elsewhere. (Okay, that's not quite true. If it appears in the middle of the stream, it's supposed to be treated as a zero-width non-breaking space.) That said, Ulrike's comment just above this one is probably the way to go. –  TH. Nov 26 '10 at 14:08
1  
@xport: Why don't you try it yourself? And if it doesn't work ask if and how code to remove the BOM? –  Ulrike Fischer Jun 19 '11 at 10:07
show 4 more comments

Perhaps this is not a usable solution for you, but using either lualatex or xelatex instead of pdflatex fixes it.

share|improve this answer
1  
With all due respect to @UlrikeFischer, this is a much better solution, if you can go with the XeTeX or LuaTeX engines (it's the future knocking on your door!!). If your code, one day, ever needs some French quotes, or Spanish questions, you're going to be mystified. As in ¿« Maïz » en francés quiere decir "Maíz"? –  Brent.Longborough Mar 13 '12 at 9:01
add comment

I had the same problem, and found a simple solution.

\usepackage[utf8x]{inputenc}

\lstset{ 
  extendedchars=\true
}

gives output with correct colouring and no extra space.

share|improve this answer
    
Thank you for your answer. It is the final solution. –  xport Jan 27 '11 at 8:52
4  
As noted in the comments to tex.stackexchange.com/questions/19210/… this only works by accident is not a valid option for listings. –  Caramdir May 27 '11 at 19:14
    
I believe [utf8] is preferred over [utf8x], which is obsolescent. –  Brent.Longborough Mar 13 '12 at 9:02
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.