[Tex/LaTex] Undefined control sequence error at \DeclareUnicodeCharacter

document-classeserrorstemplatesunicode

I am trying to write a file using the document class file provided by the journal Semantics and Pragmatics at http://info.semprag.org/style. However, I can't even compile the example template they provide… Whenever I try to run LaTeX on it, I get the following error:

ERROR: Undefined control sequence.

--- TeX said ---
./sp.cls:107:
Undefined control sequence.
<recently read> \DeclareUnicodeCharacter 

l.107 \DeclareUnicodeCharacter
                              {2011}{\mbox{-}\nobreak\hskip0pt}

I tried both with auctex and on overleaf.com but the error remained the same.

Strangely, I couldn't find anything related to "undefined control sequence" error caused by \DeclareUnicodeCharacter by Googling. This doesn't make sense to me since apparently the TeX engines just think \DeclareUnicodeCharacter isn't a valid command at all, while the document class file as well as various examples on the Internet just use this command directly without any issue. Is there something wrong with the document class file when used with the newest version of TeX engine, or did I misconfigure something?


EDIT: The compile log: https://www.dropbox.com/s/49mkenzngua0we0/sp-template.log?dl=0

Best Answer

The macro \DeclareUnicodeCharacter is defined in the package inputenc when loaded with the utf8 encoding. So normally you need the following line before you use \DeclareUnicodeCharacter:

\usepackage[utf8]{inputenc}

In this case though, the sp.cls already contains the line \RequirePackage[utf8]{inputenc} on line 103, before it uses \DeclareUnicodeCharacter on line 107, so you should be able to compile sp-template.tex with pdflatex even without explicitly adding the line.

However, if you compile with the xelatex or lualatex engines (which are Unicode-based and default to utf8), then the inputenc package gets ignored, and (only) a warning is printed in the error log:

% xelatex sp-template.tex
This is XeTeX, Version 3.14159265-2.6-0.99998 (TeX Live 2017) (preloaded format=xelatex)
 restricted \write18 enabled.
entering extended mode
(./sp-template.tex
LaTeX2e <2017-04-15>
Babel <3.10> and hyphenation patterns for 84 language(s) loaded.
(./sp.cls
Document Class: sp 2015/01/04 v.3.0 Class for Semantics & Pragmatics
(/usr/local/texlive/2017/texmf-dist/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
(/usr/local/texlive/2017/texmf-dist/tex/latex/base/size12.clo)) (/usr/local/texlive/2017/texmf-dist/tex/latex/stmaryrd/stmaryrd.sty) (/usr/local/texlive/2017/texmf-dist/tex/latex/base/textcomp.sty (/usr/local/texlive/2017/texmf-dist/tex/latex/base/ts1enc.def)) (/usr/local/texlive/2017/texmf-dist/tex/latex/amsfonts/amssymb.sty (/usr/local/texlive/2017/texmf-dist/tex/latex/amsfonts/amsfonts.sty)) (/usr/local/texlive/2017/texmf-dist/tex/latex/base/fontenc.sty (/usr/local/texlive/2017/texmf-dist/tex/latex/base/t1enc.def) (/usr/local/texlive/2017/texmf-dist/tex/latex/lm/t1lmr.fd)) (/usr/local/texlive/2017/texmf-dist/tex/latex/psnfss/mathptmx.sty) (/usr/local/texlive/2017/texmf-dist/tex/latex/base/inputenc.sty

Package inputenc Warning: inputenc package ignored with utf8 based engines.

)
! Undefined control sequence.
<recently read> \DeclareUnicodeCharacter 

l.107 \DeclareUnicodeCharacter
                              {2011}{\mbox{-}\nobreak\hskip0pt}
? 

(Note the line that starts with "Package inputenc warning". IMO it makes sense for this to be an error instead, but probably the authors of inputenc chose to make it a warning because many files written with inputenc have a chance of working on Unicode-based engines if inputenc simply does nothing.)

So you need to either

  1. compile your file with the pdfTeX engine, or
  2. if you wish to compile with XeTeX/LuaTeX, hack sp.cls to remove the following line:

    \DeclareUnicodeCharacter{2011}{\mbox{-}\nobreak\hskip0pt}
    

    (note it was added to deal with a hyphenation problem which you are unlikely to have with a Unicode-based engine as presumably no one would define hyphenation after U+2011) and also these lines that use \ifpdf to mean “non-PostScript output” but unfortunately excludes XeTeX but not LuaTeX (they're ok to remove because breakurl is not needed):

    % If the author is using postscript (discouraged), then load the
    % breakurl package, else don't load it.
    \RequirePackage{ifpdf}
    \ifpdf
    \else
      \RequirePackage{breakurl}
    \fi
    

(Note that if you're sending the source .tex file to someone else, such as the journal, then you shouldn't choose the latter option: and in general if you're submitting papers to journals you shouldn't mess with their style files. But if you're not going to send the file to anyone and are simply typesetting it in the journal's style for your own amusement, you can do whatever you like.)