11

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}
  • 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. – kiss my armpit Mar 13 '12 at 4:30
15
+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}
  • 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
  • @Ulrike, @xport: is this actually safe? I assume that the C# source code is utf-8 throughout? – Taco Hoekwater Nov 26 '10 at 10:39
  • 2
    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
11

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

  • 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
5

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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