views:

1234

answers:

3

Hello *,

I am currently working on some LaTeX document which embeds C# files generated by Visual Studio 2008. My problem is that these files are encoded in UTF-8 with BOM. This causes LaTeX to produce output similar to the output described in this post:
Invalid characters in generated latex sources in Doxygen?

I know that I can use a tool like Notepad++ to convert the file to ASCII or some other format without BOM. But my intention would be to:

  • either cause LaTeX to use correct input encoding (until now I failed doing it with the package imports like:

    \usepackage{ucs} % unicode functionality
    \usepackage[latin1]{inputenc}

  • or cause Visual Studio to save the files without BOM or in plain ASCII

Otherwise I might edit the file (compile it and save it in VC#) and unintentionally introduce BOM again, which would break the code listing in the document.

Many thanks,
Ovanes

A: 

I'm not sure I understand your scenario. But if you simply want to convert a file to ASCII from within Visual Studio select "File - Save As" and switch the encoding to ASCII.

JaredPar
JaredPar thanks for the answer. My scenario is that I want VS C# by default use ASCII and not UTF-8.
ovanes
+1  A: 

Visual Studio does not have this option, by design I believe, because .NET is built from the ground-up to use Unicode.

However, I don't believe Visual Studio is supposed to use the byte order marks. You said that Visual Studio is "generating" these files, but what process is really creating them? Is it the result of some sort of code generation tool? If so, that's the culprit and the place where you should focus.

I checked several of my code files and none of them contain the byte order marks.

EDIT: Changing Visual Studio Project Templates

In the comments the questioner said that these files were generated by the built-in Console Application project template. These are stored on your hard drive and can be modified if necessary.

Your installation path may vary, but on my system, I navigated to this directory:

C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ProjectTemplates\CSharp\Windows\1033

Here I find ConsoleApplication.zip. I copied this to my desktop (for safety) and unzipped, and inside you find 4 files - a .vstemplate file, and the 3 files that are created by the project: AssemblyInfo.cs, ConsoleApplication.csproj, and Program.cs.

If you want, you can edit these files to remove the byte order marks, zip it back up, and replace the file in the source directory.

OR, to be safer, you can change the name of the template to "Console Project - No BOM" or something like that. In the .vstemplate file, there is a Name attribute that uses a Package attribute to call in information from somewhere by a guid. You can replace this name line with a simple line that specifies the name.

<Name>Console Application - No BOM</Name>

Then rezip the files, and put the zip file in the following path:

(My Documents)\Visual Studio 2008\Templates\ProjectTemplates\Visual C#

New projects created from this template should not contain the byte order marks, but remember, Microsoft apparently wanted those byte order marks in there, so your mileage may vary.

Item templates (like Class) can be modified in the same way - it shouldn't take too much exploring to find the default and user ItemTemplates directory.

David
These are the first bytes of my .cs file generated by the VS 2009 code wizard (simple .NET console application): ef bb bf 75 73 69 6e 67 20 53 79 73 74 65 6d 3b which result in: using System;
ovanes
Which code wizard?
David
Visual C# project wizard which generates simple console application.
ovanes
Those project templates are stored on your hard drive, you can change them - I don't think I'll have enough space here, so I'll edit my answer.
David
Great Answer! Exactly what I was looking for. An option would be better, but this approach is also fine.
ovanes
A: 

have you tried \usepackage[UTF8]{inputenc}

Mica
yes, it did not help. I tried several encodings from the inputenc package.
ovanes
perhaps you should try and pump it through iconv?
Mica