[Tex/LaTex] Defining a replacement of a unicode character

miktexpdftexunicodexetex

In my, automatically generated tex files there quite a few 'Replacement characters' 0xEFBFBD (see: http://www.utf8-chartable.de/unicode-utf8-table.pl?start=65280).
I have the following code:

\documentclass{book}

\usepackage[utf8]{inputenc}

\begin{document}
\ttfamily
Here follows the "Replacement character": � (EFBFBD).
\end{document}

(The display of the replacement character is depending on the chosen encoding, in this editor ANSI encoding I see �. When using vim I see �).

When running pdflatex (This is pdfTeX, Version 3.14159265-2.6-1.40.19 (MiKTeX 2.9.6650 64-bit)), on files with these characters I get the error message:

! Package inputenc Error: Unicode char � (U+FFFD)
(inputenc)                not set up for use with LaTeX.

See the inputenc package documentation for explanation.
Type  H <return>  for immediate help.
 ...

l.7 Here follows the "Replacement character": �
                                                  (EFBFBD).

When running xelatex (This is XeTeX, Version 3.14159265-2.6-0.99999 (MiKTeX 2.9.6650 64-bit)) a pdf is generated but nothing is shown:
enter image description here
In the answer from @StevenB.Segletes on the question Unicode Replacement Character in Standard TeX a nice character is shown (I've rescaled it slightly for my needs in the my output code):

\documentclass{book}

\usepackage[utf8]{inputenc}
\usepackage{adjustbox,amssymb,graphicx,stackengine,xcolor}
\newlength{\CodeWidthChar}
\newlength{\CodeHeightChar}
\def\ucr{\adjustbox{width=\CodeWidthChar,height=\CodeHeightChar}{\stackinset{c}{}{c}{-.2pt}{%
   \textcolor{white}{\sffamily\bfseries\small ?}}{%
   \rotatebox{45}{$\blacksquare$}}}}

\begin{document}
\ttfamily
  \settowidth{\CodeWidthChar}{?}%
  \settoheight{\CodeHeightChar}{?}%
Here follows the "Replacement character": \ucr
\end{document}

Now I want automatically replace the 'Replacement Character' from my first example with this scaled defined character, but I don't succeed.
I tried with xelatex:

\DeclareUnicodeCharacter{�}{\ucr}

resulting in:

! Undefined control sequence.
l.10 \DeclareUnicodeCharacter
                             {�}{\ucr}

and:

\DeclareUnicodeCharacter{EFBFBD}{\ucr}

resulting in:

! Undefined control sequence.
l.10 \DeclareUnicodeCharacter
                             {EFBFBD}{\ucr}

The code:

\documentclass{book}

\usepackage[utf8]{inputenc}
\usepackage{adjustbox,amssymb,graphicx,stackengine,xcolor}
\newlength{\CodeWidthChar}
\newlength{\CodeHeightChar}
\def\ucr{\adjustbox{width=\CodeWidthChar,height=\CodeHeightChar}{\stackinset{c}{}{c}{-.2pt}{%
   \textcolor{white}{\sffamily\bfseries\small ?}}{%
   \rotatebox{45}{$\blacksquare$}}}}
\DeclareUnicodeCharacter{EFBFBD}{\ucr}
\begin{document}
\ttfamily

%% edit:
\settowidth{\CodeWidthChar}{?}%
\settoheight{\CodeHeightChar}{?}%
%% end edit.
Here follows the "Replacement character": � (EFBFBD).
\end{document}

How to solve this problem (either in xelatex or pdflatex).

EDIT: added setting width and height in last code snippet.

Best Answer

With xelatex you can do this:

\documentclass{book}

\usepackage{adjustbox,amssymb,graphicx,stackengine,xcolor}
\newlength{\CodeWidthChar}
\newlength{\CodeHeightChar}
\def\ucr{\adjustbox{width=\CodeWidthChar,height=\CodeHeightChar}{\stackinset{c}{}{c}{-.2pt}{%
   \textcolor{white}{\sffamily\bfseries\small ?}}{%
   \rotatebox{45}{$\blacksquare$}}}}
%\DeclareUnicodeCharacter{EFBFBD}{\ucr}
\catcode`\�=13
\def�{\ucr}
\begin{document}
\ttfamily
Here follows the "Replacement character": � (EFBFBD).
\end{document}

It doesn't work with lualatex, it complains about an invalid utf8 char.

With pdflatex you must declare the FFFD character (but you will have to correct the height and width in \ucr definition, it doesn't output anything for me with pdflatex):

\documentclass{book}
\usepackage[utf8]{inputenc}
\usepackage{adjustbox,amssymb,graphicx,stackengine,xcolor}
\newlength{\CodeWidthChar}
\newlength{\CodeHeightChar}
\def\ucr{\adjustbox{width=\CodeWidthChar,height=\CodeHeightChar}{\stackinset{c}{}{c}{-.2pt}{%
   \textcolor{white}{\sffamily\bfseries\small ?}}{%
   \rotatebox{45}{$\blacksquare$}}}}
\DeclareUnicodeCharacter{FFFD}{XXX}
\begin{document}
\ttfamily \ucr
Here follows the "Replacement character": � (EFBFBD).
\end{document}
Related Question