In the past I asked a question about a replacement character for a unicode character (FFFD) in case of and xelatex (Defining a replacement of a unicode character), this works nicely. Now I need it for lualatex as well and found: Implementing \DeclareUnicodeCharacter in LuaLaTeX an XeLaTeX
So I tried to implement it (note there is no document yet as it crashes beforehand):
\documentclass{book}
\usepackage{ifluatex}
\newif\ifunicode
\ifluatex\unicodetrue\fi
\ifunicode
\usepackage{fontspec}
\usepackage{newunicodechar}
\newcommand{\DeclareUnicodeCharacter}[2]{%
\begingroup\lccode`|=\string"#1\relax
\lowercase{\endgroup\newunicodechar{|}}{#2}%
}
\else
\usepackage[utf8]{inputenc}
\fi
\def\ucr{\adjustbox{width=\CodeWidthChar,height=\CodeHeightChar}{\stackinset{c}{}{c}{-.2pt}{%
\textcolor{white}{\sffamily\bfseries\small ?}}{%
\rotatebox{45}{$\blacksquare$}}}}
\ifunicode
\DeclareUnicodeCharacter{FFFD}{\ucr}
\else
% some other stuff
\fi
\begin{document}
\end{document}
though this gives the error message:
! String contains an invalid utf-8 sequence.
\newunicodechar #1#2->\if \relax \detokenize {#1}
\relax \nuc@emptyargerr \el...
l.23 \DeclareUnicodeCharacter{FFFD}{\ucr}
Any solution?
EDIT, based on @DavidCarlisle (used edit because of readability):
I tried \newunicode{?}{\ucr} and get:
! Undefined control sequence.
l.24 \newunicode
{�}{\ucr}
(and with \newunicode{\ucr}{?} in get
! Undefined control sequence.
l.25 \newunicode
{\ucr}{�}.)
\newunicodecharcommand it works in luatex or pdftex. But your source should not have any U+FFFD characters in it so you shouldn't need to define that?? – David Carlisle Jul 08 '18 at 12:41\DeclareUnicodeCharacterwith\newunicodeand as first argument eitherFFFDor�but both failed. – albert Jul 08 '18 at 12:47\newunicodechar{�}{?}should work in both systems. But better would be to fix your generation system, U+FFD means that the input was corrupted and the original information could not be reconstructed so some system has given up and just replaced the original data by � – David Carlisle Jul 08 '18 at 12:54FFFDvalues I would directly write out\ucr. – albert Jul 08 '18 at 14:01