[Tex/LaTex] UTF-8 (but not in current font) character in newcommand

charactersfontsmacrosunicode

I'm trying to define a new single-character LaTeX command (to insert a 1 pt space with \kern1pt) and want to use one of the characters obtained through the AltGr key on a US International keyboard layout. I've chosen the Unicode U+00A6 '¦', aka 'Broken Bar' character. This is my preamble

\documentclass[11pt,english]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{textcomp}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{babel}

and this is my newcommand definition

\expandafter\newcommand\csname u8:\detokenize{¦}\endcsname{\kern1pt}

and the error I get:

! LaTeX Error: Command \u8:¦ already defined.
.              Or name \end... illegal, see p.192 of the manual.

So, trying to understand what is going on, I replaced '¦' with '∙', the Unicode U+2219 'Bullet Operator' character. I first made sure that LaTeX complained, in insertions in normal text, as loudly with '∙' as it does with '¦', (it does) and then changed my newcommand definition thus:

\expandafter\newcommand\csname u8:\detokenize{∙}\endcsname{\kern1pt}

This time I got no errors, so I proceeded to use my new macro

a\∙b

and got this error:

! Undefined control sequence.
l.530 a\Ô
.        êÖb

Trying to fix that, I added this to my preamble,

\DeclareUnicodeCharacter{2219}{\textsurd}    % Recognise `∙' character in input

and got this error instead:

! LaTeX Error: Command \u8:ÔêÖ already defined.
.              Or name \end... illegal, see p.192 of the manual.

Can someone please save me hours sifting through the newunicodechar package manual? (And I remind that it's a '¦', not '∙' that I need to use.)

Best Answer

There are a couple of problems:

  1. There is already an action defined for ¦, precisely \IeC{\textbrokenbar}, which is kind of expected; thus \newcommand will give you the error.

  2. If you do

    \expandafter\newcommand\csname u8:\detokenize{∙}\endcsname{\kern1pt}
    

    you're not defining the macro \∙, but a meaning for the Unicode character . Since is represented in UTF-8 by the triple E2 88 99, TeX will see \^^e2 and the error message uses some representation of the three bytes.

With newunicodechar you don't have to do anything special:

% -*- coding: utf-8 -*-
\documentclass[11pt,english]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{textcomp}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{babel}
\usepackage{newunicodechar}
\newunicodechar{¦}{\kern20pt} % exaggerated to show the effect
\begin{document}
A¦A
\end{document}

The output is

enter image description here

and the log file will report

Package newunicodechar Warning: Redefining Unicode character on input line 11.

which would be

Package newunicodechar Warning: Redefining Unicode character; it meant
(newunicodechar)                ***  \IeC {\textbrokenbar }  ***
(newunicodechar)                before your redefinition on input line 11.

if the verbose option is used (\usepackage[verbose]{newunicodechar}).

Here's the relevant part from the documentation of newunicodechar.

The package provides only one command, \newunicodechar, which must be called with two arguments:

\newunicodechar{<char>}{<code>}

where <char> is the Unicode character to which we need to give a meaning and <code> is that meaning, that is the LaTeX code that will be substituted to the character.