7

If I create a package, is it correct to use \RequirePackage[utf8]{inputenc} to support utf8. I guess not, but how do I state that this package must (should) be used with inputenc utf8?

1
  • 1
    well inputenc is only required when using LaTeX or pdfLaTeX. if one uses the newer XeLaTeX or LuaLaTeX, UTF-8 is already required as the input and works natively.
    – ArTourter
    Nov 25, 2012 at 14:49

1 Answer 1

7

This is what I do in newunicodechar that requires the utf8 option either to inputenc or inputenx and wouldn't work with other options and without either of those packages:

\def\nuc@stop{\PackageWarningNoLine{newunicodechar}
  {This package won't work without loading\MessageBreak
   `inputenc' or `inputenx' with the `utf8' option}%
  \let\newunicodechar\@gobbletwo\endinput}
\@ifpackageloaded{inputenx}
  {\def\nuc@tempa{inputenx}}
  {\@ifpackageloaded{inputenc}{\def\nuc@tempa{inputenc}}{\nuc@stop}}
\@ifpackagewith{\nuc@tempa}{utf8}{}{\nuc@stop}
\@ifpackagewith{\nuc@tempa}{utf8x}{\nuc@stop}{}

The \let\newunicodechar\@gobbletwo is just to enable processing the file nonetheless. If you want to require usage of utf8 you can modify it like this:

\ProvidesPackage{xyz}
% Define an error message
\def\xyz@stop{\PackageError{xyz}
  {`inputenc' or `inputenx' loaded with wrong option.\MessageBreak
   This is a fatal error}
  {This package won't work if either `inputenc' or `inputenx'\MessageBreak
   are loaded with the `utf8' option. The LaTeX run will be terminated}%
   \fi\@@end} % the `\fi` is to match `\if@tempswa`
% Check for inputenx or inputenc
\@tempswafalse
\@ifpackageloaded{inputenx}
  {\def\xyz@tempa{inputenx}\@tempswatrue}
  {\@ifpackageloaded{inputenc}
    {\def\xyz@tempa{inputenc}\@tempswatrue}
    {\RequirePackage[utf8]{inputenc}}%
  }
% Check for the right option
\if@tempswa
\@ifpackagewith{\xyz@tempa}{utf8}{}{\xyz@stop}
\@ifpackagewith{\xyz@tempa}{utf8x}{\xyz@stop}{}
\fi

So if the user types

\usepackage[utf8]{inputenc}
\usepackage{xyz}

nothing will be done. But if inputenc (or inputenx) is not loaded before it, xyz will load it. If a wrong option is passed to inputenc, the package will terminate the LaTeX run.

You must log in to answer this question.

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