0

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.

1
  • Can U+FFFD be typeset to begin with?
    – egreg
    Apr 18, 2018 at 22:14

2 Answers 2

3

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}
3
  • Regarding the width and height, I in the lat example on the DeclareUnicodeChar and therefor forgot / didn't see the missing of the setting (I will edit it into the question). The othe points I'll test.
    – albert
    Apr 19, 2018 at 7:34
  • 1
    @A.Donda The catcode? 13 is the number for "set active", so you should use it for everyone. Dec 11, 2019 at 20:00
  • Sorry, just after I asked I found it in 1 s with Google. Thanks!
    – A. Donda
    Dec 11, 2019 at 20:01
0

Based on the code as provided by @UlrikeFischer works in case of pdflatex and xelatex. I combined it together so it works in both cases (and also added the missing settings of height and width):

\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$}}}}
\usepackage{ifpdf}
\ifpdf
  \DeclareUnicodeCharacter{FFFD}{\ucr}
\else
  \catcode`\�=13
  \def�{\ucr}
\fi

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

You must log in to answer this question.

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