I have a custom .sty-file in cp1251 encoding which is defines \RequirePackage[cp1251]{inputenc}. I use it for compile with pdflatex in the same encoding, But when I try to compile some file with other encoding, say utf8 I, of course, get an errors. How my package can automatically check the file encoding and hence redefine parameters of \RequirePackage[<current file encoding>]{inputenc}?

Briefly, I need something like

  • 1
    Have a look at selinput – egreg Jan 3 '15 at 13:34
  • 1
    Unless it is a private package loading inputenc should be left to the user IMHO. Personally I wouldn't bother and would just leave it... – clemens Jan 3 '15 at 14:28
  • 1
    It is almost always better not to force the document encoding. The package can be written to just use classic ascii input with no input encoding dependencies. – David Carlisle Jan 3 '15 at 16:05

You can use selinput; for this particular case, a cyrillic letter can be used, together with a restricted set of encodings to look for.



If the file is encoded in UTF-8, compiling it will produce


If it's encoded in CP1251,


will be produced.

However, your problem is different and not really solvable.

If your package has, say


and is encoded in CP1251, there will be no way to use this in a UTF-8 encoded document, because TeX uses the input encoding only when expanding macros. It is completely irrelevant when TeX stores a definition.

In this case, \chaptername will expand to the sequence of characters


(each <..> denotes a character code) with category code 13, that will be invalid in a UTF-8 encoded document.

You could use


in the sergiokapone.sty file and


in the code, that would make LaTeX expand \chaptername to


which is independent of the encoding, and the main document would use

\inputencoding{utf8} % or cp1251

or, possibly, selinput as shown before.

This is however not what I'd recommend. The best way to proceed is to make .sty files encoding independent; the above \renewcommand should be


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.