7

I include/\input automatically generated files (tables generated by Stata) into my main document. I would like to compile that document with LuaLaTeX. Unfortunately the generated files are ANSI-encoded (Stata does not support unicode). LuaLaTeX thus throws a "String contains an invalid utf-8 sequence" error at me. At the moment, the problem is only a single character, namely ¤ (U00A4 according to Windows Character Map). With XeLaTeX or without the character the document compiles fine.

I would prefer to not search and replace the character (with e.g. \textcurrency, though something that works in math-mode as well would be preferred), since it involves many included files that change often. For the same reason I would prefer to not convert the encoding of the included files by hand. I would also prefer a more robust solution, i.e. not only for single characters.

So is there a way to make LuaLaTeX use included files that are ANSI encoded, comparable to using \inputencoding with pdflatex?

Alternatively, is it possible to automatically search and replace when compiling?

Here is a MWE:

\documentclass{article}
\usepackage{luatextra, luaotfload}
\usepackage[vargreek-shape=unicode]{unicode-math} % 
\defaultfontfeatures{Renderer=Basic,Scale=MatchLowercase, Ligatures={TeX}} 
\setmainfont{Cambria}  
\setmathfont{Cambria Math} 

\begin{document}

A currency symbol in the main document: ¤ 

 \input{input.tex}

\end{document}

input.tex just has one line, but is ANSI-encoded.

A currency symbol in the included document: ¤ 
4
  • 1
    (A slightly late) Welcome to TeX.sx! Please add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. I suspect this may be a font-related problem, as I have managed to format an International Currency Symbol without the problem you mention. Feb 25, 2013 at 12:50
  • 2
    Have you tried luainputenc?
    – Alexander
    Feb 25, 2013 at 13:40
  • @Brent.Longborough: I added a MWE. The issue is not the currency symbol but the ANSI-encoded file. AFAIK LuaLaTeX only supports UTF-8 by default and the question basically is whether it is somehow possible to feed it an ANSI-encoded file nevertheless.
    – dpprdan
    Feb 25, 2013 at 21:15
  • @Alexander: I tried it, but apparently I am not able to figure out how to use it properly.
    – dpprdan
    Feb 25, 2013 at 21:17

1 Answer 1

6

To tell LuaTeX to accept other encodings than UTF8 you need an additional package. Similar to the inputenc for LaTeX luainputenc provides the necessary commands for LuaTeX. The basic syntax is just \usepackage[<encoding>]{luainputenc}. Where the most commonly used encodings are latin1 and cp1252. The slightly changed example compiles without errors:

\documentclass{article}

\usepackage[latin1]{luainputenc}

\usepackage{luatextra, luaotfload}
\usepackage[vargreek-shape=unicode]{unicode-math} % 
\defaultfontfeatures{Renderer=Basic,Scale=MatchLowercase, Ligatures={TeX}} 
\setmainfont{Cambria}  
\setmathfont{Cambria Math} 

\begin{document}

A currency symbol in the main document: ¤ 

\input{input.tex}

\end{document}

Alternatively you could use tools such as iconv to automatically convert the file.

4
  • Hm, simple. Makes me wonder why I did not try the [latin1] option. Anyway, two questions: First, I would like to only encode the included files as latin1, so what's the equivalent to \inputencoding (cf. here)? Second, if I want to be able to compile the document with xelatex or pdflatex (via ifxetex/ifluatex), how should I setup the document (also in combination with Q1)? I.e. is luainputenc compatible with xelatex/pdflatex?
    – dpprdan
    Feb 25, 2013 at 23:35
  • @dapperdan: \inputencoding should also work in luainputenc. I think it is also compatible with xelatex/pdflatex by simply calling inputenc instead.
    – Alexander
    Feb 26, 2013 at 9:03
  • 1
    Well, if I change \usepackage[latin1]{luainputenc} to \usepackage[utf8]{luainputenc} and insert \inputencoding{cp1252} before \input{input.tex} I still get a String contains an invalid utf-8 sequence. error for the included document. (Again note the different encodings of the two documents.)
    – dpprdan
    Feb 26, 2013 at 12:07
  • @dapperdan: \inputencoding switches the encoding mid-document but somehow not completely. As this is a workaround anyway I would suggest using a batch file to automatically convert the input document instead.
    – Alexander
    Feb 26, 2013 at 13:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .