7

I write most of my documents with XeLaTeX. Sometimes, however, i need to collaborate with someone who does not. In these cases, it is generally easy for me to add a pdfLaTeX-specific header:

\ifPDFTeX
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}

The only issue is unicode characters:

Unicode char ⟨charcode⟩ not set up for use with LaTeX

Can I tell inputenc to silently drop these characters, or replace them by a box, or some fallback character? For now the only solution I have is to define

\def\unic#1{\textvisiblespace}

and to wrap all unicode characters in this macro to hide them from pdfLaTeX, which is a pain.

8

Macro \UTFviii@defined checks, whether the UTF-8 character is defined, it can be redefined. The following example replaces the undefined character by a box around its hexadecimal number (LaTeX/utf8.def should not be too old):

\documentclass{article}
\usepackage[utf8]{inputenc}

\makeatletter
\renewcommand*{\UTFviii@defined}[1]{%
  \ifx #1\relax
    % \PackageError{inputenc}{%
    %   Unicode\space char\space
    %   \expandafter\UTFviii@splitcsname\string#1\relax\MessageBreak
    %   not\space set\space up\space for\space use\space
    %   with\space LaTeX%
    % }\@eha
    \noindent
    \begingroup
      \setlength{\fboxsep}{1pt}%
      \setlength{\fboxrule}{.2pt}%
      \tiny
      \fbox{%
        \expandafter\UTFviii@splithex\string#1\relax
      }%
    \endgroup
  \else
    \expandafter #1%
  \fi
}
\ifx\numexpr\@undefined
  \gdef\UTFviii@splithex#1:#2\relax{#2}}
\else
  \gdef\UTFviii@splithex#1:#2\relax{%
    \expandafter\UTFviii@hexnumber\expandafter{%
      \the\numexpr\decode@UTFviii#2\relax
    }%
  }
\fi
\makeatother

\begin{document}
Euro symbol: €
\end{document}

Result

  • Wonderful. Thanks so much! Can you maybe explain how one finds about \UTFviii@defined? – Clément Aug 6 '16 at 5:22
  • @Clément It's a part of the internals of inputenc: you have to read the code to find it. – Joseph Wright Aug 6 '16 at 6:47
  • 1
    @Clément Another method for finding: \tracingmacros=1\relax before the undefined Unicode character, stopping the TeX job with x at the error message and analyzing the .log file backwards. – Heiko Oberdiek Aug 6 '16 at 6:52

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.